1.下载、解压、编译、安装、查看版本:
官网源码下载地址:https://www.keepalived.org/download.html,选择最新版本
yum install -y wget gcc openssl-devel libnl-devel
wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz
tar zxvf keepalived-2.0.19.tar.gz
cd keepalived-2.0.19/
./configure --prefix=/usr/local/keepalived
make
make install
/usr/local/keepalived/sbin/keepalived -v
备用:yum安装及启动命令:
yum install -y keepalived
systemctl start keepalived //启动keepalived
systemctl enable keepalived //加入开机启动keepalived
systemctl restart keepalived //重新启动keepalived
systemctl status keepalived //查看keepalived状态
安装完成会在以下路径生成:
/usr/local/keepalived
/usr/local/etc/keepalived/keepalived.conf
/usr/local/etc/sysconfig/keepalived
/usr/local/sbin/keepalived
2.初始化及启动:
# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
# 将keepalived主程序加入到环境变量(安装目录下)
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用
cp /root/keepalived-2.0.19/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
# 将配置文件放到默认路径下
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
- 加为系统服务:chkconfig --add keepalived
- 启动、关闭、重启service keepalived start|stop|restart
3.主从节点的配置:
[root@centos102 keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id centos102 #标识本节点的字条串,通常为hostname
}
vrrp_instance VI_1 {
state MASTER #角色是master
interface eth0 #vip 绑定端口
virtual_router_id 51 #让master 和backup在同一个虚拟路由里,id 号必须相同;
priority 150 #优先级,谁的优先级高谁就是master ;
advert_int 1 #心跳间隔时间
authentication {
auth_type PASS #认证
auth_pass 1111 #密码
}
virtual_ipaddress {
192.168.56.11 #虚拟ip
}
}
[root@centos103 keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id centos103 #标识本节点的字条串,通常为hostname
}
vrrp_instance VI_1 {
state BACKUP #角色是BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
nopreempt #非抢占式,MASTER从挂掉到恢复,不再将服务抢占过来
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.56.11
}
}