keepalived
keepalived,虚拟ip(vip)实践。
keepalived介绍
keepalived是一个是集群管理中保证集群高可用的一个服务软件,它具备心跳检测的功能。比如运用在redis、nginx、greenplum上,则是具备主备切换的控制功能。
keepalived的主备切换方案的设计思想如下:
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
版本
keepalived-1.3.9-1.el7.x86_64.rpm
流程图
搭建
基础设置
主机名
1.vi /etc/hostname (已设置可跳过) -----设置主机名
selinux
2.vi /etc/selinux/config (已设置可跳过) ------- SELINUX=disabled
关闭防火墙(所有机器)
设置开机启用防火墙:systemctl enable firewalld.service
设置开机禁用防火墙:systemctl disable firewalld.service
启动防火墙:systemctl start firewalld
关闭防火墙:systemctl stop firewalld
检查防火墙状态:systemctl status firewalld
hosts映射
vi /etc/hosts
keepalived设置
vip
192.168.60.221 keepalived主节点
192.168.60.222 keepalived备节点
192.168.60.100 虚拟ip(vip)
准备rpm包
两台机器目录/home/soft/keepalived/分别包含rpm包,位置自定义
rpm包文件位置:/home/soft/keepalived/keepalived-1.3.9-1.el7.x86_64.rpm
yum install keepalived-1.3.9-1.el7.x86_64.rpm
安装过程缺少依赖,补充依赖:http://rpmfind.net/linux/rpm2html/search.php?query=libverto-devel%28x86-64%29&submit=Search+…&system=&arch=
补充依赖包需要耐心,强烈建议使用在线yum安装,或者有离线的镜像yum
配置
yum install keepalived-1.3.9-1.el7.x86_64.rpm
安装完以后 默认在/etc/keepalived,不建议更换位置
备份一份原有的配置
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf-bnk
配置文件有很多 暂时用不到的 我就先删除掉了
vi /etc/keepalived/keepalived.conf
主
代码属于最初搭建使用的配置
图片是集成greenplum 高可用后的配置
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.60.221
smtp_connect_timeout 30
router_id LVS_DEVEL
script_user root
enable_script_security
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 78
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.60.100
}
}
备
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.60.222
smtp_connect_timeout 30
router_id LVS_DEVEL
script_user root
enable_script_security
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 78
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.60.100
}
}
参数简述:
state MASTER :当前节点为master(会存在抢vip)
nopreempt : 配合state BACKUP (解决抢占vip)
notify:keepaclived启动执行,我自己测试 出发了两次
notify_master:keepaclived启动,成为master后触发
notify_backup:keepaclived启动,成为backup触发
当第一个keepalived启动,会先执行一次notify_backup的脚本,然后 执行一次notify_master的脚本
启动:systemctl start keepalived
停止:systemctl stop keepalived
查看:ps -ef|grep [k]eepalived
ip a
日志监控:
tail -f /var/log/messages
ps -ef|grep [k]eepalived
ping 192.168.60.100
虚拟ip成功
报错处理
‘keepalived_script’ for script execution does not exist
WARNING - default user ‘keepalived_script’ for script execution does not exist - please create.
这个错误的原因在于配置文件的错误,在global_defs中加入如下两行即可。
script_user root
enable_script_security
.sh exited due to signal 15
Keepalived_vrrp[29368]: /etc/keepalived/nginx_check.sh exited due to signal 15
vrrp_script{}中interval的间隔时间要大于脚本中的sleep时间
Unsafe permissions found
Unsafe permissions found for script ‘/etc/keepalived/XXXXXXX’
查看脚本权限,改成755.
如果还是报错Unsafe permissions,将脚本所有者改为root
配置详解
可参考
1.配置keepalived ping不通 解决办法
2.nginx+keepalived集群高可用详细部署,keepalived安装启动报错问题(超详细)
3.Greenplum集群Master与Standby相互切换
4.Postgresl选择正确的关闭模式
5.实现主备路由自动切换 – keepalived
6.单台主机(CentOS7.3)安装Greenplum(5.20.1)
7.解决Keepalived脚本启动时warning、Unsafe