其实KeepAlived在我之前关于nginx的学习帖子【nginx 高可用HA keepalived学习】就已经讲到过使用了,这里就算是重复的讲一下吧,另:在上几篇帖子中是有一个安装文档的,朋友们需要是可以去找一下
1.介绍
2、Keepalived安装
PS:下载地址:[http://www.keepalived.org/download.html](http://www.keepalived.org/download.html)
//安装所需软件包
yum install -y openssl openssl-devel
//下载
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
//解压、编译、安装
tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
make && make install
//将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local),安装完成之后,需要做一些修改工作
//首先创建文件夹,将keepalived配置文件进行复制:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
//然后复制keepalived脚本文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
//可以设置开机启动:chkconfig keepalived on,到此我们安装完毕!
chkconfig keepalived on
3、Keepalived配置
PS:修改keepalived.conf配置文件
vim /etc/keepalived/keepalived.conf
PS: 79节点(Master)配置如下
! Configuration File for keepalived
global_defs {
router_id bhz79 ##标识节点的字符串,通常为hostname
}
vrrp_script chk_haproxy {
script "/etc/keepalived/haproxy_check.sh" ##执行脚本位置
interval 2 ##检测时间间隔
weight -20 ##如果条件成立则权重减20
}
vrrp_instance VI_1 {
state MASTER ## 主节点为MASTER,备份节点为BACKUP
interface eth0 ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eth0)
virtual_router_id 79 ## 虚拟路由ID号(主备节点一定要相同)
mcast_src_ip 192.168.11.79 ## 本机ip地址
priority 100 ##优先级配置(0-254的值)
nopreempt
advert_int 1 ## 组播信息发送间隔,俩个节点必须配置一致,默认1s
authentication { ## 认证匹配
auth_type PASS
auth_pass bhz
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.11.70 ## 虚拟ip,可以指定多个
}
}
PS: 80节点(backup)配置如下
! Configuration File for keepalived
global_defs {
router_id bhz80 ##标识节点的字符串,通常为hostname
}
vrrp_script chk_haproxy {
script "/etc/keepalived/haproxy_check.sh" ##执行脚本位置
interval 2 ##检测时间间隔
weight -20 ##如果条件成立则权重减20
}
vrrp_instance VI_1 {
state BACKUP ## 主节点为MASTER,备份节点为BACKUP
interface eno16777736 ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eno16777736)
virtual_router_id 79 ## 虚拟路由ID号(主备节点一定要相同)
mcast_src_ip 192.168.11.80 ## 本机ip地址
priority 90 ##优先级配置(0-254的值)
nopreempt
advert_int 1 ## 组播信息发送间隔,俩个节点必须配置一致,默认1s
authentication { ## 认证匹配
auth_type PASS
auth_pass bhz
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.1.70 ## 虚拟ip,可以指定多个
}
}
4、执行脚本编写
PS:添加文件位置为/etc/keepalived/haproxy_check.sh(79、80两个节点文件内容一致即可)
#!/bin/bash
COUNT=`ps -C haproxy --no-header |wc -l`
if [ $COUNT -eq 0 ];then
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
sleep 2
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
5、执行脚本赋权
PS:haproxy_check.sh脚本授权,赋予可执行权限.
chmod +x /etc/keepalived/haproxy_check.sh
#6、启动keepalived
PS:当我们启动俩个haproxy节点以后,我们可以启动keepalived服务程序:
//启动两台机器的keepalived
service keepalived start | stop | status | restart
//查看状态
ps -ef | grep haproxy
ps -ef | grep keepalived
7、高可用测试
PS:vip在27节点上
![](https://img.haomeiwen.com/i8553141/224d04c213dfe480.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
PS:27节点宕机测试:停掉27的keepalived服务即可。
![](https://img.haomeiwen.com/i8553141/7550ddc185b682f9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
PS:查看28节点状态:我们发现VIP漂移到了28节点上,那么28节点的haproxy可以继续对外提供服务!
![](https://img.haomeiwen.com/i8553141/2efea724b66a1657.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
网友评论