Keepalived介绍
Keepalived是一个基于VRRP协议来达成的服务高可用策略,可以借助其来防止IP单点问题。但它一般不会单独出现,而是与其它负载均衡技术(如nginx)一块工作来达到集群的高可用。
安装nginx和keepalived命令
yum install nginx -y
yum install keepalived -y
配置nginx服务器
master宁夏网站制作|nginx+keepalive达成双机热备192.168.1.11
修改 文件 /usr/share/nginx/html/index.html 内容为nginx:192.198.1.11
测试nginx脚本
Vim /opt/chk_nginx.sh
#!/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
/etc/init.d/keepalived sTOP
fi
fi
配置keepalived服务
修改master(192.168.1.10)配置文件 /etc/keepalived/keepalived.conf:
vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface em1
mcast_src_ip 192.168.1.10
virtual_router_id 51
priority 101
advert_int 1
virtual_ipaddress {
192.168.1.20
}
track_script {
chk_http_port
}
}
修改slave(192.168.1.11)配置文件 /etc/keepalived/keepalived.conf:
vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface em1
mcast_src_ip 192.168.1.11
virtual_router_id 51
priority 100
advert_int 1
virtual_ipaddress {
192.168.1.20
}
track_script {
chk_http_port
}
}
启动服务
在两台服务器上运行如下命令
/usr/local/nginx/sbin/nginx
/etc/init.d/keepalived start
在浏览器中打开 http://192.168.1.20,内容为nginx:192.198.1.10
停止master(192.168.1.10)上的nginx,
再在浏览器中打开 http://192.168.1.20,内容为nginx:192.198.1.11