当前服务器的IP为192.168.79.128
1、Keepalived下载
https://www.keepalived.org/download.html
我这里下载的是Version 2.0.18
2、Keepalived安装
cd /home/tools/
tar -zxvf keepalived-2.0.18.tar.gz
cd keepalived-2.0.18
./configure --prefix=/usr/loacl/keepalived --sysconf=/etc
make & make install
3、keepalived核心配置文件
cd /etc/keepalived
vi keepalived.conf
修改global_defs
计算机节点配置
4、启动keeepalived
启动前的IP
启动keepalived
cd /usr/local/keepalived/sbin/
./keepalived
启动后的IP
可以发现虚拟IP 192.168.79.161 已经生效
5、将keepalived注册为系统服务
一般要停止Keepalived,需要kill该进程。如何更优雅停止keepalived?可以将keepalived注册为系统服务
cd /home/tools/keepalived-2.0.18/keepalived/etc
cp init.d/keepalived /etc/init.d/
cp sysconfig/keepalived /etc/sysconfig/
使服务生效: systemctl daemon-reload
这样系统服务就可以使用keepalived了
使用systemctl start keepalived.service启动keepalived。
使用systemctl stop keepalived.service停止keepalived。
使用systemctl restart keepalived.service重启keepalived。
6、keepalived实现双机主备
备用节点服务器的IP为192.168.79.129
keepalived配置如下
然后启动keepalived
这里没有192.168.79.161, 说明主节点192.168.79.128运行正常。
window的host配置如下
www.128.com 指向192.168.79.128 主Nginx节点
www.129.com 指向192.168.79.129 备用Nginx节点
www.home.com 指向虚拟ip 192.168.79.161 虚拟IP现在绑定的是主Nginx节点: 192.168.79.128
现在假设主节点挂了,我们停止Keepalived,
使用ip addr,此时已经没有了虚拟IP
进入备用节点,查看IP。可以发现备用节点192.168.79.129多了一个虚拟IP 192.168.79.161
此时www.home.com访问的是129
当主节点Keepalived重新启动后,虚拟IP又跟主节点Niginx绑定了。这样就实现了Nginx双机主备配置了。
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!