keepalived高可用集群服务
程序员文章站
2022-04-09 20:42:47
...
一、简介
keepalived 是专门用来监控LVS集群系统中各个节点的状态,后来又加入了VRRP(虚拟路由器冗余协议)功能
二、keepalived的两大功能
1、对节点做健康检查 healthcheck
2、高可用对之间的失败接管(接管的有VIP资源及相应的服务) failover
三、keepalived工作原理
1、VRRP(虚拟路由器冗余协议),它的出现就是为了解决静态路由的单点故障。
2、VRRP是通过一种竞选协议机制来将路由任务交给某台VRRP路由器。
3、VRRP通信是通过IP多播的方式实现通信。
4、主发包,备接收包。备接不到包的时候,接管主的资源。备可以有多个,通过优先级竞选
5、VRRP使用了加密协议,如果是web服务的接管,可以接管的快一点,1~2秒即可。
四、keepalived安装布署
#下载并安装
[root@linux-node1 ~]# yum -y install libnl* popt* #安装依赖包
[root@linux-node1 ~]# cd /usr/local/src/
[root@linux-node1 src]# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
[root@linux-node1 src]# tar -xf keepalived-1.2.2.tar.gz
[root@linux-node1 src]# cd keepalived-1.2.2
[root@linux-node1 keepalived-1.2.2]# ./configure
[root@linux-node1 keepalived-1.2.2]# make && make install
#拷贝配置文件
[root@linux-node1 ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
#启动脚本
[root@linux-node1 ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#配置启动脚本参数
[root@linux-node1 ~]# mkdir -p /etc/keepalived
#创建配置文件目录
[root@linux-node1 ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#配置文件模板
[root@linux-node1 ~]# cp /usr/local/sbin/keepalived /usr/sbin/
#启动服务
[root@linux-node1 ~]# /etc/init.d/keepalived start
Reloading systemd: [ OK ]
Starting keepalived (via systemctl): [ OK ]
[root@linux-node1 ~]# ps -ef | grep keepalived
root 5072 1 0 14:15 ? 00:00:00 keepalived -D
root 5074 5072 0 14:15 ? 00:00:00 keepalived -D
root 5076 1724 0 14:15 pts/0 00:00:00 grep --color=auto keepalived
备注:另一台安装方式完全一样
五、配置keepalived单实例
#主负载均衡器配置如下
[root@linux-node1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
111111@qq.com
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id LVS_7
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 7
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.23.100/24
}
}
#备负载均衡器配置
[root@linux-node2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
111111@qq.com
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id LVS_8
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 7
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.23.100/24
}
}
扩展:配置文件说明
global_defs { #全局定义
notification_email { #通知的email,可以不配
111111@qq.com #收件人是谁,可以不配
}
notification_email_from Alexandre.Cassen@firewall.loc #发件人是谁,可以不配
smtp_server 192.168.23.1 #发邮件服务器,可以不配
smtp_connect_timeout 30 #超时,可以不配
router_id LVS_7 #keepalive的id,不同机器不一样
}
vrrp_instance VI_1 { #实例名
state MASTER #状态,标记路由器的状态
interface eth0 #提供服务端口或绑定vip的端口
virtual_router_id 55 #虚拟路由ID,主备必须一样
priority 150 #优先级,判断主备机,以这个为主
advert_int 1 #高可用对之间接管的间隔
authentication {
auth_type PASS #认证的类型
auth_pass 1111 #认证密码
}
virtual_ipaddress {
192.168.23.100/24 #提供服务的VIP
}
}
六、重启服务并测试
#先启动主node1,后启动备机node2
[root@linux-node1 ~]# /etc/init.d/keepalived restart
#检查主备负载均衡器VIP情况
[root@linux-node1 ~]# ip add | grep 192.168.23.100
inet 192.168.23.100/24 scope global secondary ens33
[root@linux-node2 ~]# ip add | grep 192.168.23.100
备服务器上没有出现VIP表明配置正确
七、指定文件接收日志
1、在全局配置文件中追加
echo KEEPALIVED_OPTIONS="-D -d -S 0" >> /etc/sysconfig/keepalived
2、在日志配置文件中追加
echo "local0.* /var/log/keeplived.log" >> /etc/rsyslog.conf
3、重启服务(日志服务和keepalived服务)