主机规划
主机 |
服务 |
192.168.158.128 |
nginx keepalived |
192.168.158.129 |
Nginx keepalived |
虚IP : 192.168.158.50
前置
服务器上已经有nginx,可以访问80端口。
架构
下载及准备
https://www.keepalived.org/
1
| wget --no-check-certificate https://www.keepalived.org/software/keepalived-1.2.18.tar.gz
|
编译并安装
1 2 3 4
| tar -xvzf keepalived-1.2.18.tar.gz cd /usr/local/keepalived-1.2.18 ./configure --prefix=/usr/local/keepalived make && make install
|
Keepalived安装成Linux服务
1 2 3 4 5
| mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
|
设置开机启动
创建nginx状态检查脚本
脚本描述:如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程
1
| vi /etc/keepalived/nginx_check.sh
|
内容:
1 2 3 4 5 6 7 8 9 10
| #!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /usr/local/nginx/sbin/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then systemctl stop keepalived fi fi
|
权限:
1
| chmod +x /etc/keepalived/nginx_check.sh
|
配置keepalived.conf
1 2
| cd /etc/keepalived/ vim keepalived.conf
|
keepalived.conf解释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| ! Configuration File for keepalived global_defs { router_id TS1 }
vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 }
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 mcast_src_ip 192.168.150.132 priority 100 nopreempt advert_int 1
track_script { chk_nginx } virtual_ipaddress { 192.168.150.138/24 dev eth0 label eth0:2 } }
|
192.168.158.128 主机配置keepalived.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| ! Configuration File for keepalived
global_defs { router_id LVS_DEVEL }
vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight 20 }
vrrp_instance VI_1 { state MASTER interface ens160 virtual_router_id 51 priority 100 advert_int 1 mcast_src_ip 192.168.158.128 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.158.50 } track_script { chk_nginx } }
|
192.168.158.129 主机配置keepalived.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| ! Configuration File for keepalived
global_defs { router_id LVS_DEVEL }
vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight 20 }
vrrp_instance VI_1 { state BACKUP interface ens160 virtual_router_id 51 priority 90 advert_int 1 mcast_src_ip 192.168.158.128 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.158.50 } track_script { chk_nginx } }
|
防火墙设置
需要vrrp 的组播(多播)
1 2 3 4 5
| firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens160 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
firewall-cmd --direct --get-all-rules
|
ens160 为网卡名称。
启动
1 2 3 4
| service keepalived start service keepalived status service keepalived stop service keepalived restart
|
访问http://192.168.158.50/
可以看到nginx的index.html
测试
修改两台nginx的index.html添加IP标识