实现基于keepalived高可用集群
keepalived主要模块是VRRP Stack和Cheackers,实现HA集群中失败切换(Failover)功能。Keepalived通过VRRP功能能再结合LVS负载均衡软件即可部署一个高性能的负载均衡集群系统。,Cheackers主要实现对服务器运行状态检测和故障隔离。其中ipvs和realserver健康状态检查通过配置文件配置就可以实现,而其他服务高可用则需要通过自己编写脚本,然后配置keepalived调用来实现。
Keepalived运行有3个守护进程。父进程主要负责读取配置文件初始化、监控2个子进程等;然后两个子进程,一个负责VRRP,另一个负责Cheackers健康检查。其中父进程监控模块为WacthDog,工作实现:每个子进程打开一个接受unix域套接字,父进程连接到那些unix域套接字并向子进程发送周期性(5s)hello包。
VRRP(Virtual Router Redundancy Protocol,虚拟路由冗余协议)可以认为是实现路由器高可用的协议,简单的说,当一个路由器故障时可以由另一个备份路由器继续提供相同的服务。
VRRP根据优先级来确定虚拟路由器中每台路由器的角色(Master路由器或Backup路由器)。VRRP优先级的取值范围为0到255(数值越大表明优先级越高),可配置的范围是1到254,优先级0为系统保留给路由器放弃Master位置时候使用,255则是系统保留给IP地址拥有者使用。优先级越高,则越有可能成为Master路由器。当两台优先级相同的路由器同时竞争Master时,比较接口IP地址大小。接口地址大者当选为Master。
实验一、主从模式的集群服务(基于DR集群工作模式)
1、准备四台主机:都是桥接模式
一台Director-master,DIP:172.17.111.117 VIP:172.17.111.111
一台Director-backup,DIP:172.17.222.220 VIP:172.17.111.111
一台后台服务器,RIP:172.17.111.234 VIP:172.17.111.111
一台后台服务器,RIP:172.17.111.222 VIP:172.17.111.111
2、Director-master上操作:
①安装keepalived服务:
yum install keepalived
②修改配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost 邮件接收者
}
notification_email_from [email protected] 发件人
smtp_server 127.0.0.1 发邮件的服务器
smtp_connect_timeout 2 连接超时时间
router_id LVS_DEVEL 路由器的标识
}
vrrp_instance VI_1 { 配置虚拟路由器的实例
state MASTER 初始状态
interface eth0 所用端口
virtual_router_id 50 虚拟路由的ID号
# nopreempt 非抢占式,当多个BACKUP时,其中有个BACKUP的优先级高但是为非抢占式,那么它就不去抢VIP。
priority 100 优先级
advert_int 1 VRRP通告间隔
authentication { 认证机制
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.111.111
}
}
virtual_server 172.17.111.111 80 { 虚拟服务
delay_loop 6 服务轮询的时间间隔
lb_algo wrr LVS调度算法
lb_kind DR LVS集群模式
nat_mask 255.255.255.255
persistence_timeout 50 会话保持时间
protocol TCP 健康检查用的协议
real_server 172.17.111.222 80 { 后端真实节点主机
weight 1 权重
HTTP_GET {
url {
path /
}
connect_timeout 3 连接超时时间
nb_get_retry 3 重连次数
delay_before_retry 3 重连间隔
}
}
real_server 172.17.111.234 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
③开启转发功能
echo 1 >/proc/sys/net/ipv4/ip_forward
④启动服务
systemctl start keepalived
3、Director-backup上操作:
①安装keepalived服务:
yum install keepalived
②修改配置文件:
vim /etc/keepalived/keepalived.conf
同master的配置文件,只是修改一下初始状态及优先级即可
state BACKUP
priority 90
③开启转发功能
echo 1 >/proc/sys/net/ipv4/ip_forward
④启动服务
systemctl start keepalived
4、两个后台服务器上操作:
①配置回环网卡:
ifconfig lo:0 172.17.111.111 broadcast 172.17.111.111 netmask 255.255.255.255 up
route add -host 172.17.111.111 lo:0
②忽略ip与mac不匹配的问题:
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
5、测试:刚启动服务查看ip是在MASTER上,当关闭MASTER上的keepalived服务,那么ip就被BACKUP拿走了
实验二、双主模式的集群服务(基于DR集群工作模式):
1、准备四台主机:都是桥接模式
一台Director-master,DIP:172.17.111.117 VIP1:172.17.111.111
VIP2:172.17.111.112
一台Director-backup,DIP:172.17.222.220
VIP1:172.17.111.111
VIP2:172.17.111.112
一台后台服务器,RIP:172.17.111.234
VIP1:172.17.111.111
VIP2:172.17.111.112
一台后台服务器,RIP:172.17.111.222
VIP1:172.17.111.111
VIP2:172.17.111.112
2、Director-master1上操作:
①安装keepalived服务:
yum install keepalived
②修改配置文件(具体代码含义请参看上面的实验):
vim /etc/keepalived/keepalived.cong
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 50
# nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.111.111
}
}
virtual_server 172.17.111.111 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.255
persistence_timeout 50
protocol TCP
real_server 172.17.111.222 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.17.111.234 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 51
# nopreempt
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.111.112
}
}
virtual_server 172.17.111.112 443 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.255
persistence_timeout 50
protocol TCP
real_server 172.17.111.222 443 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.17.111.234 443 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
③开启转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
④启动服务:
systemctl start keepalived
3、Director-master2上操作:
①安装keepalived服务:
yum install keepalived
②修改配置文件:
vim /etc/keepalived/keepalived.conf
内容与上面基本一致,只需修改vrrp_instance VI_1中
state BACKUP
priority 90 #小于100
修改vrrp_instance VI_2中
state MASTER
priority 100
③开启转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
④启动服务:
systemctl start keepalived
4、两个后台服务器上操作:
①配置回环网卡:
ifconfig lo:0 172.17.111.111 broadcast 172.17.111.111 netmask 255.255.255.255 up
route add -host 172.17.111.111 lo:0
ifconfig lo:1 172.17.111.112 broadcast 172.17.111.111 netmask 255.255.255.255 up
route add -host 172.17.111.112 lo:1
②忽略ip与mac不匹配的问题:
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
5、测试:刚启动服务时MASTER1主机上有172.17.111.111无172.17.111.112
MASTER2主机上有172.17.111.112无172.17.111.111
当关闭MASTER2主机时,两个VIP都会在MASTER1主机上,而MASTER2主机上无VIP
实验三、基于keepalived的使用NAT模型的主从模式集群
1、准备四台主机:一台master(桥接与仅主机):DIP:192.168.199.145 VIP1:172.17.111.10 VIP2:192.168.199.111(作为可漂移的DIP)
一台backup(桥接与仅主机):DIP:192.168.199.234 VIP1:172.17.111.10
VIP2:192.168.199.111(作为可漂移的DIP)
一台后台服务器(仅主机):RIP:192.168.199.146 可实现网站访问
一台后台服务器(仅主机):RIP:192.168.199.143 可实现网站访问
2、在master上操作:
①安装keepalived服务:
yum install keepalived
②修改配置文件(具体代码含义请参看实验一):
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 2
router_id LVS_DEVEL2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 33
# nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 111111
}
virtual_ipaddress {
172.17.111.10
192.168.199.111
}
}
virtual_server 172.17.111.10 80 {
delay_loop 6
lb_algo wrr
lb_kind NAT
nat_mask 255.255.255.255
persistence_timeout 50
protocol TCP
real_server 192.168.199.146 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.199.143 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
③开启转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
④启动服务:
systemctl start keepalived
3、backup上操作:
①安装keepalived服务:
yum install keepalived
②修改配置文件
vim /etc/keepalived/keepalived.conf
与上面配置一样,只需要修改state状态为BACKUP
③开启转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
④启动服务:
systemctl start keepalive
4、两台后台服务器上操作:添加网关,指向同一个VIP2
route add default gw 192.168.199.111
5、测试:
测试效果同实验一
感谢浏览,如有疑问,欢迎留言。