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上

相关推荐
布鲁格若门4 分钟前
CentOS 7 桌面版安装 cuda 12.4
linux·运维·centos·cuda
Eternal-Student9 分钟前
【docker 保存】将Docker镜像保存为一个离线的tar归档文件
运维·docker·容器
DC_BLOG16 分钟前
Linux-Apache静态资源
linux·运维·apache
码农小丘18 分钟前
一篇保姆式centos/ubuntu安装docker
运维·docker·容器
耗同学一米八43 分钟前
2024 年河北省职业院校技能大赛网络建设与运维赛项样题二
运维·网络·mariadb
淡水猫.1 小时前
Fakelocation Server服务器/专业版 ubuntu
运维·服务器·ubuntu
wenyue11211 小时前
Ease Monitor 会把基础层,中间件层的监控数据和服务的监控数据打通,从总体的视角提供监控分析
运维·中间件·监控
时光の尘2 小时前
C语言菜鸟入门·关键字·float以及double的用法
运维·服务器·c语言·开发语言·stm32·单片机·c
我们的五年2 小时前
【Linux课程学习】:进程描述---PCB(Process Control Block)
linux·运维·c++
运维老司机2 小时前
Jenkins修改LOGO
运维·自动化·jenkins