本文共 3327 字,大约阅读时间需要 11 分钟。
在本次实验中,我们准备了三台服务器:
vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 17 priority 150 virtual_ipaddress 192.168.40.17}vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 17 priority 99 virtual_ipaddress 192.168.40.17}Tomcat8081: 192.168.40.16:8081Tomcat8082: 192.168.40.16:8082
yum -y install curl gcc openssl-devel libnl3-devel net-snmp-devel libnfnetlink-devel
mkdir /software
(此步骤省略具体上传命令)
tar -zxvf keepalived-2.1.5.tar.gz
cd keepalived-2.1.5./configure --prefix=/data/keepalived --sysconf=/etc
make && make install
ln -s sbin/keepalived /sbin/ln -snf sbin/keepalived /sbin
cp /software/keepalived-2.1.5/keepalived/etc/init.d/keepalived /etc/init.d/chkconfig --add keepalived
chkconfig keepalived onservice keepalived start
vim /etc/keepalived/
global_defs { router_id NGINX}vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 17 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.40.17 }} vim /etc/keepalived/
global_defs { router_id NGINX}vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 17 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.40.17 }} /usr/local/nginx/sbin/nginx
service keepalived start
验证虚拟 IP 是否正常:
ip a
/usr/local/nginx/sbin/nginx -s stop
验证网络状态:
netstat -nnetstat -nult
验证网页状态:
curl http://192.168.40.17
service keepalived stop
验证网页是否恢复正常:
curl http://192.168.40.17
#!/bin/bashA=$(ps -C nginx --no-header | wc -l)if [ $A -eq 0 ]; then sleep 2 if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then killall keepalived fifi
vim /etc/keepalived/
global_defs { router_id NGINX}vrrp_script chk_nginx { script "/root/chk_nginx.sh" interval 2 weight -5}vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 17 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.40.17/24 } track script { chk_nginx }} vim /etc/keepalived/
global_defs { router_id NGINX}vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 17 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.40.17/24 }} /usr/local/nginx/sbin/nginx -s stop
验证网络接口信息:
ip a
验证网页状态:
curl http://192.168.40.17
usr/local/nginx/sbin/nginx
验证 Keepalived 是否自动切换:
service keepalived status
通过以上步骤,可以实现 Nginx 和 Keepalived 的高可用性部署,确保服务的稳定性和可靠性。
转载地址:http://vocfk.baihongyu.com/