Keepalived介绍
Keepalived是一个基于VRRP协议来达成的服务高可用策略,可以借助其来防止IP单点问题。但它一般不会单独出现,而是与其它负载均衡技术(如nginx)一块工作来达到集群的高可用。
安装nginx和keepalived命令
yum install nginx |y
yum install keepalived |y
配置nginx服务器
master:
192.168.1.10
修改 文件 /usr/share/nginx/html/index.html 内容为nginx:192.198.1.10
slave:
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
成都网站制作:https://www.cdcxhl.com/
目前文章:成都网站制作|nginx+keepalive达成双机热备(主从模式)推荐网址:https://www.cdcxhl.com/news/77144.html
网站制作、网络营销推广公司|博久网络,是专注品牌与成效的网站建设,网络推广营销网站优化公司;服务项目有网站制作等