一、准备环境
linux系统:CentOS7
keepalived版本:keepalived-1.3.5.tar.gz
keepalived下载地址:http://www.keepalived.org/download.html
在CentOS7系统中安装keepalived(主、从服务器都要安装):
- 关闭CentOS7防火墙
- 关闭selinux策略
- 下载keepalived依赖包gcc、openssl
yum install gcc
yum install openssl-devel
二、安装和配置keepalived(只对keepalived主程序配置,LVS则不进行配置)
- 将源码keepalived-1.3.5.tar.gz文件解压缩
命令:[root@test3 keepalived]# tar zxvf keepalived-1.3.5.tar.gz
2.进入keepalived-1.3.5文件夹,配置keepalived源文件并指定路径。
创建指定路径:(此路径随意指定)
[root@test3 keepalived-1.3.5]# mkdir /usr/local/keepalived
配置源文件:
[root@test3 keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
(出现下图表示配置源文件成功)
编译keepalived:
[root@test3 keepalived-1.3.5]# make
安装keepalived:
[root@test3 keepalived-1.3.5]# make install
3.keepalived启动读取配置文件的路径为/etc/keepalived
路径创建命令:[root@test3 keepalived-1.3.5]# mkdir /etc/keepalived
将/usr/local/keepalived/etc/keepalived下的keepalived.conf复制到/etc/keepalived路径下
复制命令:[root@test3 keepalived-1.3.5]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
4.编辑/etc/keepalived/keepalived.conf文件
编辑命令:[root@test3 keepalived-1.3.5]# vi /etc/keepalived/keepalived.conf
主服务器具体配置如下图:
5.经过上述步骤主服务器配置已完成,从服务器只需更改keepalived.conf配置文件中的两处即可其余与主服务器的配置信息一样:
state处改为 BACKUP
priority处改为 50
6.启动keepalived服务:systemctl start keepalived.service
重启keepalived服务:systemctl restart keepalived.service
停止keepalived服务:systemctl stop keepalived.service
设置为开机自启动:systemctl enable keepalived.service
取消开机自启动:systemctl disable keepalived.service
查看错误日志:cat /var/log/messages
如何检查keepalived正常运行:
1) 查看keepalived三个进程是否都启动,命令:ps aux | grep keepalived
Keepalived 正常运行时,共启动 3 个进程,其中一个进程是父进程,负责监控其子进程;一个是 vrrp 子进程;另外一个是 checkers 子进程。
2) 查看浮动ip是否绑定到网卡(下图中IP已绑定到网卡,本机ip为192.168.1.9),命令:ip a 或 ip addr s
3) 在keepalived服务所在局域网中,ping浮动ip查看是否可以ping通
至此keepalived安装配置已完成。
但有一个问题,正常情况下A(master)主服务器运行时浮动ip(vip)绑定在A服务器的网卡上,当A服务器宕机时,B(backup)从服务器自动接替工作浮动ip绑定到B服务器网卡上,若修复好A服务器从新上线,A服务器会抢占浮动ip,强制重新绑定到A服务器网卡上。
如何避免抢占浮动ip,A宕机时B接替工作,当A恢复上线时自动当从机,B继续工作?
修改A(master)主服务的/etc/keepalived/keepalived.conf文件中state的值为BACKUP,在参数中添加nopreempt命令,B服务器的配置文件不改变。
【这个配置只能设置在state为BACKUP的主机上,而且这个主机的priority必须比另外一台高】