主机 |
IP |
作用 |
主机1 |
10.100.1.10 |
|
主机2 |
10.100.1.11 |
Nginx2 |
无 |
10.100.1.5 |
VIP |
[root@nginx1 ~]# yum install -y keepalived
[root@nginx2 ~]# yum install -y keepalived
配置文件路径:
/etc/keepalived/keepalived.conf
nginx1配置(主节点)
global_defs { #全局配置
router_id lb01 #标识身份->名称
}
vrrp_instance VI_1 {
state MASTER #标识角色状态(主节点)
interface ens33 #网卡绑定接口
virtual_router_id 50 #虚拟路由id
priority 150 #优先级
advert_int 1 #监测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.100.1.5 #虚拟的VIP地址
}
}
nginx2配置(备节点)
global_defs { #全局配置
router_id lb02 #标识身份->名称
}
vrrp_instance VI_1 {
state BACKUP #标识角色状态(备节点)
interface ens33 #网卡绑定接口
virtual_router_id 50 #虚拟路由id
priority 100 #优先级
advert_int 1 #监测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.100.1.5 #虚拟的VIP地址
}
}
[root@nginx1 ~]# systemctl start keepalived
[root@nginx1 ~]# systemctl enable keepalived
[root@nginx2 ~]# systemctl start keepalived
[root@nginx2 ~]# systemctl enable keepalived
在nginx1上查看是否有VIP
[root@nginx1 ~]# ip addr | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
.NET 10.100.1.10/8 brd 10.255.255.255 scope global ens33
inet 10.100.1.5/32 scope global ens33
关闭nginx1的keeplived,查看VIP是否会主动飘到nginx2
[root@nginx1 ~]# systemctl stop keepalived
[root@nginx2 ~]# ip addr | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.100.1.11/8 brd 10.255.255.255 scope global ens33
inet 10.100.1.5/32 scope global ens33
启动nginx1的keepalived,查看nginx1是否会抢占VIP
[root@nginx1 ~]# systemctl start keepalived
[root@nginx1 ~]# ip addr | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.100.1.10/8 brd 10.255.255.255 scope global ens33
inet 10.100.1.5/32 scope global ens33
配置完keepalived之后,存在一个问题,就是如果挂的不是keepalived而是这台电脑上的Nginx的话,keepalived的转发就不会生效,因为它无法识别Nginx服务是否正常
解决方法:在keepalived.conf文件中配置对Nginx的定时检查,如果发现Nginx挂了,调用脚本重启Nginx
vim /etc/keepalived/check_nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l`#判断nginx是否为启动,否,则启动nginx
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx #启动nginx
sleep 3 #等待3秒
if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then #启动失败的话,则停止keepalived服务
systemctl stop keepalived
fi
fi
[root@nginx1 ~]# chmod a+x /etc/keepalived/check_nginx.sh
vrrp_script check_nginx {
script "
/etc/keepalived/check_nginx.sh" #脚本
interval 2 #每隔两秒运行上一行的脚本
weight 10 #如果脚本运行失败,则升级权重+10
}
在vrrp_instance中新增监控的脚本
track_script {
check_nginx #追踪nginx脚本
}
安装依赖包
[root@nginx1 ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
上传源码包到tmp目录
解压该源码文件
[root@nginx1 tmp]# tar -zxvf nginx-1.20.0.tar.gz
进入到nginx-1.12.0目录,指定安装目录/usr/local/nginx
[root@nginx1 nginx-1.20.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
编译安装
[root@nginx1 nginx-1.20.0]# make && make install
进入到/usr/local/nginx/sbin,执行./nginx启动nginx服务
[root@nginx1 sbin]# /usr/local/nginx/sbin/nginx
查看nginx是否启动成功
[root@nginx1 sbin]# ps -ef | grep nginx
关闭nginx服务,查看keepalived是否会将nginx启动
1)、执行命令/usr/local/nginx/sbin -s stop停止nginx服务
[root@nginx1 keepalived]# /usr/local/nginx/sbin/nginx -s stop
2)、执行命令ps -ef | grep nginx查看nginx服务是否启动
[root@nginx1 keepalived]# ps -ef | grep nginx
修改nginx配置,让nginx服务启动失败,查看keepalived是否会切换到备节点
1)、修改nginx配置
2)、执行命令停止nginx服务
[root@nginx1 keepalived]# ps -ef | grep nginx | grep -v nginx | awk '{print "kill -9",$2}' | sh
3)、执行命令ps -ef | grep nginx查看nginx服务是否启动,否,查看keepalived的状态
4)、查看keepalived的服务状态
[root@nginx1 keepalived]# systemctl status keepalived
5)、登录nginx2查看VIP是否飘到该服务器
[root@nginx2 ~]# ip addr | grep ens33
修改为正确的nginx配置,重新启动nginx服务,启动keepalived服务,查看VIP是否会重新飘到nginx1上面
1)、修改为正确的nginx配置后,启动nginx服务
[root@nginx1 keepalived]# /usr/local/nginx/sbin/nginx
2)、启动keepalived服务
[root@nginx1 keepalived]# systemctl start keepalived
3)、查看VIP是否重新飘到原先的服务器
[root@nginx1 keepalived]# ip addr | grep ens33
主机 |
IP |
作用 |
主机1 |
10.100.1.10 |
Keepalived Master |
主机2 |
10.100.1.11 |
Keepalived Backup |
主机3 |
10.100.1.12 |
Nginx1 |
主机4 |
10.100.1.13 |
Nginx2 |
无 |
10.100.1.5 |
VIP |
[root@nginx1 ~]# yum install -y keepalived
[root@nginx2 ~]# yum install -y keepalived
配置文件路径:
/etc/keepalived/keepalived.conf
lvs1配置(主节点)
global_defs { #全局配置
router_id lb01 #标识身份->名称
}
vrrp_instance VI_1 {
state MASTER #标识角色状态(主节点)
interface ens33 #网卡绑定接口
virtual_router_id 50 #虚拟路由id
priority 150 #优先级
advert_int 1 #监测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.100.1.5 #虚拟的VIP地址
}
}
# 定义对外提供服务的LVS的VIP以及port
virtual_server 10.100.1.5 80 {
delay_loop 6 # 设置健康检查时间,单位是秒
lb_algo rr # 设置负载调度的算法为wlc
lb_kind DR # 设置LVS实现负载的机制,有NAT、TUN、DR三个模式
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 10.100.1.12 80 { # 指定real server1的IP地址
weight 3 # 配置节点权值,数字越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.100.1.13 80 { # 指定real server2的IP地址
weight 3 # 配置节点权值,数字越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
lvs2配置(备节点)
global_defs { #全局配置
router_id lb02 #标识身份->名称
}
vrrp_instance VI_1 {
state BACKUP #标识角色状态(备节点)
interface ens33 #网卡绑定接口
virtual_router_id 50 #虚拟路由id
priority 100 #优先级
advert_int 1 #监测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.100.1.5 #虚拟的VIP地址
}
}
# 定义对外提供服务的LVS的VIP以及port
virtual_server 10.100.1.5 80 {
delay_loop 6 # 设置健康检查时间,单位是秒
lb_algo rr # 设置负载调度的算法为wlc
lb_kind DR # 设置LVS实现负载的机制,有NAT、TUN、DR三个模式
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 10.100.1.12 80 { # 指定real server1的IP地址
weight 3 # 配置节点权值,数字越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.100.1.13 80 { # 指定real server2的IP地址
weight 3 # 配置节点权值,数字越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
[root@nginx1 ~]# systemctl start keepalived
[root@nginx1 ~]# systemctl enable keepalived
[root@nginx2 ~]# systemctl start keepalived
[root@nginx2 ~]# systemctl enable keepalived
[root@lvs1 ~]# yum install -y ipvsadm
[root@lvs2 ~]# yum install -y ipvsadm
安装依赖包
[root@nginx1 ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
上传源码包到tmp目录
解压该源码文件
[root@nginx1 tmp]# tar -zxvf nginx-1.20.0.tar.gz
进入到nginx-1.12.0目录,指定安装目录/usr/local/nginx
[root@nginx1 nginx-1.20.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
编译安装
[root@nginx1 nginx-1.20.0]# make && make install
进入到/usr/local/nginx/sbin,执行./nginx启动nginx服务
[root@nginx1 sbin]# /usr/local/nginx/sbin/nginx
查看nginx是否启动成功
[root@nginx1 sbin]# ps -ef | grep nginx
Nginx的两台机器都需要配置
1)、进入到init文件夹
[root@nginx1 ~]# cd /etc/init.d/
2)、编辑脚本realserver.sh
[root@nginx1 init.d]# vim realserver.sh
#虚拟的vip 根据自己的实际情况定义
SNS_VIP=10.100.1.5
/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
3)、设置脚本权限
[root@nginx1 init.d]# chmod +x realserver.sh
[root@nginx1 init.d]# chmod +x /etc/rc.d/init.d/functions
4)、执行脚本
[root@nginx1 init.d]# service realserver.sh start
5)、查看执行结果
[root@nginx1 init.d]# ip a
如果有显示VIP,则说明成功
1)、测试VIP
ping 10.100.1.5
2)、网页测试VIP
在lvs1上查看是否有VIP
[root@lvs1 ~]# ip addr | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.100.1.10/8 brd 10.255.255.255 scope global ens33
inet 10.100.1.5/32 scope global ens33
关闭lvs1的keeplived,查看VIP是否会主动飘到nginx2
[root@lvs1 ~]# systemctl stop keepalived
[root@lvs2 ~]# ip addr | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.100.1.11/8 brd 10.255.255.255 scope global ens33
inet 10.100.1.5/32 scope global ens33
启动lvs1的keepalived,查看nginx1是否会抢占VIP
[root@lvs1 ~]# systemctl start keepalived
[root@lvs1 ~]# ip addr | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.100.1.10/8 brd 10.255.255.255 scope global ens33
inet 10.100.1.5/32 scope global ens33
1)、查看最新的VIP对应的realserver的情况
[root@lvs1 keepalived]# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP lvs1:http rr
-> 10.100.1.12:http Route 3 0 0
-> 10.100.1.13:http Route 3 0 0
查看到10.100.1.112和10.100.1.113两台服务器都在
2)、测试停掉10.100.1.112
[root@nginx1 ~]# /usr/local/nginx/sbin/nginx -s stop
3)、查看最新的VIP对应的realserver的情况
[root@lvs1 keepalived]# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP lvs1:http rr
-> 10.100.1.13:http Route 3 0 0
可以查看到10.100.1.12这台服务器已经被移除了
4)、恢复10.100.1.12
lvs又会自动监控并加入10.100.1.12
[root@nginx1 ~]# /usr/local/nginx/sbin/nginx
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP lvs1:http rr
-> 10.100.1.12:http Route 3 0 0
-> 10.100.1.13:http Route 3 0 0