#系统:Centos 7
#服务版本:nginx1.20.1、keepalived1.3.5
#LVS是linux内核自带的服务,不用安装,只需安装LVS管理工具,第一种叫ipvsadm,第二种叫keepalived。ipvsadm是通过命令行管理,而keepalive读取配置文件管理
[root@lvs01 ~]# yum -y install ipvsadm
[root@lvs02 ~]# yum -y install ipvsadm
[root@lvs01 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs01 ~]# lsmod | grep ip_vs
ip_vs 145497 0
nf_conntrack 133095 1 ip_vs
libcrc32c 12644 3 xfs,ip_vs,nf_conntrack
[root@lvs02 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs02 ~]# lsmod | grep ip_vs
ip_vs 145497 0
nf_conntrack 133095 1 ip_vs
libcrc32c 12644 3 xfs,ip_vs,nf_conntrack
[root@lvs01 ~]# yum install -y keepalived
[root@lvs02 ~]# yum install -y keepalived
[root@lvs01 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs01 #router_id 机器标识,通常为hostname,但不一定非得是hostname。故障发生时,邮件通知会用到。
}
vrrp_instance VI_1 { #vrrp实例定义部分
state MASTER #设置lvs的状态,MASTER和BACKUP两种,必须大写
interface ens32 #设置对外服务的接口
virtual_router_id 100 #设置虚拟路由标示,这个标示是一个数字,同一个vrrp实例使用唯一标示
priority 100 #定义优先级,数字越大优先级越高,在一个vrrp——instance下,master的优先级必须大于backup
advert_int 1 #设定master与backup负载均衡器之间同步检查的时间间隔,单位是秒
authentication { #设置验证类型和密码
auth_type PASS #主要有PASS和AH两种
auth_pass 1111 #验证密码,同一个vrrp_instance下MASTER和BACKUP密码必须相同
}
virtual_ipaddress { #设置虚拟ip地址,可以设置多个,每行一个
10.0.0.192
}
}
virtual_server 10.0.0.192 80 { #设置虚拟服务器,需要指定虚拟ip和服务端口
delay_loop 6 #健康检查时间间隔
lb_algo wrr #负载均衡调度算法
lb_kind DR #负载均衡转发规则
persistence_timeout 0 #设置会话保持时间,对动态网页非常有用
protocol TCP #指定转发协议类型,有TCP和UDP两种
real_server 10.0.0.107 80 { #配置服务器节点1,需要指定real server的真实IP地址和端口
weight 1 #设置权重,数字越大权重越高
TCP_CHECK { #realserver的状态监测设置部分单位秒
connect_timeout 10 #连接超时为10秒
retry 3 #重连次数
delay_before_retry 3 #重试间隔
connect_port 80 #连接端口为80,要和上面的保持一致
}
}
real_server 10.0.0.100 80 { #配置服务器节点2,需要指定real server的真实IP地址和端口
weight 1 #设置权重,数字越大权重越高
TCP_CHECK { #realserver的状态监测设置部分单位秒
connect_timeout 10 #连接超时为10秒
retry 3 #重连次数
delay_before_retry 3 #重试间隔
connect_port 80 #连接端口为80,要和上面的保持一致
}
}
}
[root@lvs02 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs02 #标识
}
vrrp_instance VI_1 {
state BACKUP #lvs标识,BACKUP
interface ens32
virtual_router_id 100
priority 99 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.192
}
}
virtual_server 10.0.0.192 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 10.0.0.107 80 {
weight 1
TCP_CHECK {
connect_timeout 10
retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.0.0.100 80 {
weight 1
TCP_CHECK {
connect_timeout 10
retry 3
delay_before_retry 3
connect_port 80
}
}
}
[root@lvs01 ~]# systemctl start keepalived
[root@lvs02 ~]# systemctl start keepalived
#lvs01是主节点,有vip10.0.0.192
#lvs02是备用节点,所以没有
[root@nginx01 ~]# yum install -y nginx net-tools
[root@nginx02 ~]# yum install -y nginx net-tools
[root@nginx01 ~]# systemctl start nginx
[root@nginx02 ~]# systemctl start nginx
vi /etc/rc.d/init.d/realserver.sh
#!/bin/bash
SNS_VIP=10.0.0.192 #虚拟ip地址
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:0
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/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1
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/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
[root@nginx01 ~]# chmod u+x /etc/rc.d/init.d/realserver.sh
[root@nginx02 ~]# chmod u+x /etc/rc.d/init.d/realserver.sh
[root@nginx01 ~]# /etc/rc.d/init.d/realserver.sh start
/etc/rc.d/init.d/realserver.sh: line 3: /etc/rc.d/init.d/functions: Permission denied
RealServer Start OK
[root@nginx02 ~]# /etc/rc.d/init.d/realserver.sh start
/etc/rc.d/init.d/realserver.sh: line 3: /etc/rc.d/init.d/functions: Permission denied
RealServer Start OK
echo "nginx01" > /usr/share/doc/HTML/index.html
echo "nginx02" > /usr/share/doc/HTML/index.html
[root@lvs01 ~]# systemctl stop keepalived
#可以发现,虚拟ip地址已经漂到lvs02上了
lvs01:
lvs01:
#服务停止
lvs02:
#可以看到从backup转换为master状态
[root@master ~]# systemctl start keepalived
#lvs01服务恢复,虚拟ip地址就会自动漂回原master
lvs01:
lvs02:
#lvs01恢复后会自动抢占master状态
#后端web服务故障,keepalived会检查后端web服务状态,异常就从服务器列表移除,客户端访问只有nginx01会响应
[root@nginx02 ~]# systemctl stop nginx