Keepalived+LVS实现Nginx集群配置

Nginx1和Nginx2组成集群,为了实现负载均衡,在集群的前端配置了LVS服务,但是一台LVS容器产生单点故障,因此需要过Keepalived实现LVS的高可用集群

|----------------|-------|------------|
| 192.168.136.55 | node1 | keepalived |
| 192.168.136.56 | node2 | keeplived |
| 192.168.136.57 | node3 | nginx1 |
| 192.168.136.58 | node4 | nginx2 |

写nginx1的前端页面

bash 复制代码
[root@node3 ~]# echo "web test page,node1" > /usr/share/nginx/html/index.html
bash 复制代码
[root@openEuler-node4 ~]# echo "web test page,node2" > /usr/share/nginx/html/index.html

基于LVS的DR模式,这两个节点要做ARP抑制,绑定VIP

bash 复制代码
[root@node3 ~]# vi lvs_rs
#!/bin/sh
#
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server for DR
#
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
# available server built on a cluster of real servers, with the load
# balancer running on Linux.
# description: start LVS of DR-RIP
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.136.100
. /etc/rc.d/init.d/functions
start() {
	PID=`ifconfig | grep lo:10 | wc -l`
	if [ $PID -ne 0 ];
	then
		echo "The LVS-DR-RIP Server is already running !"
	else
		/sbin/ifconfig lo:10 $VIP netmask 255.255.255.255 broadcast $VIP up
		/sbin/route add -host $VIP dev lo:10
		echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
		echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
		echo "1" >/proc/sys/net/ipv4/conf/ens160/arp_ignore
		echo "2" >/proc/sys/net/ipv4/conf/ens160/arp_announce
		echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
		echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
		/bin/touch $LOCK
		echo "starting LVS-DR-RIP server is ok !"
	fi
}
stop() {
	/sbin/route del -host $VIP dev lo:10
	/sbin/ifconfig lo:10 down >/dev/null
	echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
	echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
	echo "0" >/proc/sys/net/ipv4/conf/ens160/arp_ignore
	echo "0" >/proc/sys/net/ipv4/conf/ens160/arp_announce
	echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
	echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
	rm -rf $LOCK	
	echo "stopping LVS-DR-RIP server is ok !"
}
status() {
	if [ -e $LOCK ];
	then
		echo "The LVS-DR-RIP Server is already running !"
	else
		echo "The LVS-DR-RIP Server is not running !"
	fi
}
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	status)
		status
		;;
	*)
		echo "Usage: $1 {start|stop|restart|status}"
		exit 1
esac
exit 0
[root@node3 ~]# chmod +x lvs_rs 
[root@node3 ~]# ./lvs_rs start
starting LVS-DR-RIP server is ok !

VIP已经绑定好

主机路由也有

node4上也一样

keepalived中TCP的健康检查方式

在node1和node2上 使得LVS高可用

bash 复制代码
[root@openEuler-node1 ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived
global_defs {
    router_id LVS1
}


vrrp_instance LVS {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        192.168.136.100
    }
}

virtual_server 192.168.136.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.136.57 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.136.58 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
                       

node2与上面类似稍作修改

为了看效果 安装ipvsadm的管理 yum install ipvsadm

bash 复制代码
[root@openEuler-node1 ~]# watch ipvsadm -Ln

自己就添加了node3 node4

测试

实现负载均衡

当node4的nginx停止之后 查看转发池队列

bash 复制代码
[root@openEuler-node4 ~]# systemctl stop nginx.service 

没有node4了

node4开启nginx

bash 复制代码
[root@openEuler-node4 ~]# systemctl start nginx.service 

又重新修复好了

当node1和node2其中一台keepalived停了之后 不影响客户端访问

node2上

相关推荐
_.Switch2 小时前
高级Python自动化运维:容器安全与网络策略的深度解析
运维·网络·python·安全·自动化·devops
2401_850410832 小时前
文件系统和日志管理
linux·运维·服务器
JokerSZ.2 小时前
【基于LSM的ELF文件安全模块设计】参考
运维·网络·安全
芯盾时代3 小时前
数字身份发展趋势前瞻:身份韧性与安全
运维·安全·网络安全·密码学·信息与通信
心灵彼岸-诗和远方4 小时前
DevOps业务价值流:架构设计最佳实践
运维·产品经理·devops
一只哒布刘4 小时前
NFS服务器
运维·服务器
苹果醋35 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
二十雨辰5 小时前
[linux]docker基础
linux·运维·docker
Jason-河山5 小时前
【自动化更新,让商品信息跳舞】——利用API返回值的幽默编程之旅
运维·自动化
雨雪飘零5 小时前
Windows系统使用OpenSSL生成自签名证书
nginx·证书·openssl