通过使用Keepalived使两台服务器实现主备故障切换,当其中一台服务器故障时,另一台服务器可接替。
方法/步骤
-
先从官方网下载Keepalived,然后登陆Centos系统安装必要的环境:gcc、openssl、pcre。
-
将下载好的软件包拷贝到Centos根目录下。
解压软件包、编译安装:
tar zxvf keepalived-1.2.18.tar.gz
cd keepalived-1.2.18
./configure
make
make install
-
安装完成后配置文件:/usr/local/eth/keepalived/keepalived.conf
主节点:
-
备节点:
-
修改配置,允许绑定未知IP:
/etc/sysctl.conf net.ipv4.ip_nonlocal_bind = 1
sysctl -p
最后复制配置文件到相应的目录:
cp /usr/local/sbin/keepalived /usr/sbin/
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp -r /usr/local/etc/keepalived/ /etc/
重启服务:
service keepalived restart
查看绑定状态:
-
测试,用机器长ping虚拟地址,进行倒换测试:关闭主的keepalived时备用机会接管,这时可能会丢一个包。