keepalived
介绍
keepalived 用于服务的高可用,解决单点故障。基于网络的 VRRP 协议。
demo
env
三台虚拟机,分别有2张网卡,对应地址如下:
ha1
- eth1: 172.16.101.4/24
ha1
- eth1: 172.16.101.5/24
ha1
- eth1: 172.16.101.6/24
选择一个 vip,172.16.101.10/24
setting
ha1
配置文件 /etc/keepalived/keepalived.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| vrrp_script check_running { script “/etc/keepalived/check_http.sh” # 执行检查的甲本 interval 2 weight -20 # priority 减少20 }
vrrp_instance http { state BACKUP priority 100 # 可理解为权重,高的会被选为master,1-255 interface eth1 # 用来发送VRRP的网卡 virtual_router_id 47 # 用来区分多个instance的VRRP组播,0-255,同个集群中的主备这里设置成一样 advert_int 3 # 发送VRRP的时间间隔,即进行一次检查 authentication { # 认证 auth_type PASS auth_pass 1234 } nopreempt # 设置当故障了重启之后不抢占现在master virtual_ipaddress { # VIP 172.16.101.10/24 } virtual_routes{} # 当IP漂过来之后需要添加的路由 track_script { check_running }
|
该配置文件有多个配置区域
global_defs
、
主要配置故障时通知信息,可忽略
static_ipaddress
、
本节点的ip配置,服务器一般都会有配置,可忽略
static_routes
、
本节点的route配置,服务器一般都会有配置,可忽略
vrrp_script
、
健康检查
vrrp_instance
、
配置vip机器属性
vrrp_sync_group
配置vrrp_instance组,当某个vrrp_instance切换时,组内都会切换
检查脚本/etc/keepalived/check_http.sh
1 2 3 4 5 6 7 8 9 10
| #!/usr/bin/env bash count=netstat -tnlp | grep <span class="number">80</span> | wc <span class="operator">-l</span> if [ $count -gt 0 ] then exit 0 else systemctl stop keepalived exit 1 fi
|
1
| # systemctl start keepalived
|
ha2、ha3
与 ha1 不同的是 /etc/keepalived/keepalived.conf
1 2 3 4
| vrrp_instance http { … priority 90 …
|
result
查看三台系统的 ip,可发现其中ha1 eth1 绑定了 vip
1 2 3 4 5 6 7 8 9
| # ip a eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:6f:0b:78 brd ff:ff:ff:ff:ff:ff inet 172.16.101.4/24 brd 172.16.101.255 scope global eth1 valid_lft forever preferred_lft forever inet 172.16.101.10/24 scope global secondary eth1 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe6f:b78/64 scope link valid_lft forever preferred_lft forever
|
停掉 httpd
在另外两台有一台被选举为 master,绑定了 vip