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上