简述
1集群类型
LB:Load Balance 负载均衡 LVS/HAProxy/nginx(http/upstream, stream/upstream) HA:High Availability 高可用集群 数据库、Zookeeper、Redis SPoF: Single Point of Failure,解决单点故障 HPC:High Performance Computing 高性能集群2keepalived 介绍
vrrp 协议的软件实现,原生设计目的为了高可用 ipvs服务 功能: 基于vrrp协议完成地址流动 为vip地址所在的节点生成ipvs规则(在配置文件中预先定义) 为ipvs集群的各RS做健康状态检测 基于脚本调用接口完成脚本中定义的功能,进而影响集群事务,以此支持nginx、haproxy等服务用户空间核心组件: vrrp stack:VIP消息通告 checkers:监测real server system call:实现 vrrp 协议状态转换时调用脚本的功能 https://keepalived.org/doc/ http://keepalived.org/documentation.html SMTP:邮件组件 IPVS wrapper:生成IPVS规则 Netlink Reflector:网络接口 WatchDog:监控进程 控制组件:提供keepalived.conf 的解析器,完成Keepalived配置 IO复用器:针对网络目的而优化的自己的线程抽象 内存管理组件:为某些通用的内存管理功能(例如分配,重新分配,发布等)提供访问权限
3安装keepalived(centos7自带是1.35版本,没有特殊需求可以使用此版本)
[root@keepalived1 ~]# yum -y install keepalived
[root@keepalived1 ~]# systemctl enable --now keepalived
编译安装
[root@keepalived1 ~]# yum -y install gcc curl openssl-devel libnl3-devel net-snmp-devel [root@keepalived1 ~]# wget https://keepalived.org/software/keepalived-2.0.20.tar.gz [root@keepalived1 ~]# tar xvf keepalived-2.0.20.tar.gz -C /usr/local/src [root@keepalived1 ~]# cd /usr/local/src/keepalived-2.0.20/ [root@keepalived1 ~]# ./configure --prefix=/usr/local/keepalived --disable-fwmark 防止生成iptables规则,无法访问vip [root@keepalived1 ~]# make && make install
[root@keepalived1 ~]# mkdir /etc/keepalived
[root@keepalived1 ~]#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
[root@keepalived1 ~]#systemctl enable --now keepalived.service
4配置简述
#/etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost #keepalived 发生故障切换时邮件发送的目标邮箱,可以按行区分写多个 } notification_email_from keepalived@localhost #发邮件的地址 smtp_server 127.0.0.1 #邮件服务器地址 smtp_connect_timeout 30 #邮件服务器连接timeout router_id ka1.example.com #每个keepalived主机唯一标识,建议使用当前主机名,但多节点重名 不影响 vrrp_skip_check_adv_addr #对所有通告报文都检查,会比较消耗性能,启用此配置后,如果收到的通告报文和上一个报文是同一个路由器,则跳过检查,默认值为全检查 vrrp_strict #严格遵守VRRP协议,启用此项后以下状况将无法启动服务:1.无VIP地址 2.配置了单播邻居 3.在VRRP版本2中有IPv6地址,开启动此项并且没有配置vrrp_iptables时会自动开启iptables防火 墙规则,默认导致VIP无法访问,建议不加此项配置 vrrp_garp_interval 0 #gratuitous ARP messages 报文发送延迟,0表示不延迟 vrrp_gna_interval 0 #unsolicited NA messages (不请自来)消息发送延迟 vrrp_mcast_group4 224.0.0.18 #指定组播IP地址范围:224.0.0.0到239.255.255.255,默认值:224.0.0.18 vrrp_iptables #此项和vrrp_strict同时开启时,则不会添加防火墙规则,如果无配置vrrp_strict项,则无需启用此项配置 }vrrp_instance <STRING> { #<String>为vrrp的实例名,一般为业务名称 配置参数 ...... } #配置参数: state MASTER|BACKUP#当前节点在此虚拟路由器上的初始状态,状态为MASTER或者BACKUP interface IFACE_NAME #绑定为当前虚拟路由器使用的物理接口,如:eth0,bond0,br0,可以和VIP不在一个网卡 virtual_router_id VRID #每个虚拟路由器惟一标识,范围:0-255,每个虚拟路由器此值必须唯一,否则服务无法启动,同属一个虚拟路由器的多个keepalived节点必须相同,务必要确认在同一网络中此值必须唯一 priority 100 #当前物理节点在此虚拟路由器的优先级,范围:1-254,值越大优先级越高,每个keepalived主机节点此值不同 advert_int 1 #vrrp通告的时间间隔,默认1s authentication { #认证机制 auth_type AH|PASS #AH为IPSEC认证(不推荐),PASS为简单密码(建议使用) auth_pass <PASSWORD> #预共享密钥,仅前8位有效,同一个虚拟路由器的多个keepalived节点必须一样 } virtual_ipaddress { #虚拟IP,生产环境可能指定上百个IP地址 <IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL> 192.168.200.100 #指定VIP,不指定网卡,默认为eth0,注意:不指定/prefix,默认为/32 192.168.200.101/24 dev eth1 #指定VIP的网卡,建议和interface指令指定的岗卡不在一个网卡 192.168.200.102/24 dev eth2 label eth2:1 #指定VIP的网卡label } track_interface { #配置监控网络接口,一旦出现故障,则转为FAULT状态实现地址转移 eth0 eth1 … } virtual_server IP port { #VIP和PORT delay_loop <INT> #检查后端服务器的时间间隔 lb_algo rr|wrr|lc|wlc|lblc|sh|dh #定义调度方法 lb_kind NAT|DR|TUN #集群的类型,注意要大写 persistence_timeout <INT> #持久连接时长 protocol TCP|UDP|SCTP #指定服务协议,一般为TCP sorry_server <IPADDR> <PORT> #所有RS故障时,备用服务器地址 real_server <IPADDR> <PORT> { #RS的IP和PORT weight <INT> #RS权重 notify_up <STRING>|<QUOTED-STRING> #RS上线通知脚本 notify_down <STRING>|<QUOTED-STRING> #RS下线通知脚本 HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK { ... } #定义当前主机健康状态检测方法 } } #注意:括号必须分行写,两个括号写在同一行,如: }} 会出错
5启用keepalived日志功能
[root@keepalived1 ~]#vim /etc/sysconfig/keepalived KEEPALIVED_OPTIONS="-D -S 6" [root@keepalived1 ~]#vim /etc/rsyslog.conf local6.* /var/log/keepalived.log [root@keepalived1 ~]#systemctl restart keepalived.service rsyslog.service
[root@keepalived1 ~]# ll /var/log/keeepalived.log
-rw------- 1 root root 4742 1Ղ 7 12:55 /var/log/keeepalived.log
6实现子配置文件
[root@keepalived1 ~]#mkdir /etc/keepalived/conf.d/ [root@keepalived1 ~]#vim /etc/keepalived/keepalived.confglobal_defs { notification_email { xxx@xxx.com } notification_email_from xxxxx@xxx.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka1 vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 } include /etc/keepalived/conf.d/*.conf #将VRRP相关配置放在子配置文件中
7 总结配置概览
主配置文件
[root@keepalived1 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc xxxxx@xxxx.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka1 另一个节点为ka2 vrrp_skip_check_adv_addr # vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } include /etc/keepalived/conf.d/*.conf
子配置文件
[root@keepalived1 ~]# cat /etc/keepalived/conf.d/v1.conf
vrrp_instance VI_1 {
state MASTER 另一个为BACKUP
interface ens33 网卡根据自己机器来
virtual_router_id 1 另一个节点也是1
priority 100 另一个节点为80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.200
}
unicast_src_ip 192.168.10.31 此机器地址为31,ka2配置就成32
unicast_peer {
192.168.10.32 另一个节点为32,ka2就配置成31
}
notify_master "/etc/keepalived/notify.sh master" keepalived节点状态切换脚本
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.10.200 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.10.33 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.10.34 80 {
weight 1
TCP_CHECK {
connect_timeout 5
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
-------------------------------------------------------------------
[root@keepalived1 ~]# cat /etc/keepalived/notify.sh
#!/bin/bash
#
contact='xxx@xxxx' 和主配置的邮箱一致
notify() {
mailsubject="$(hostname) to be $1, vip floating"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
8实验环境
ka1(192.168.10.31)
ka2(192.168.10.32)
vip(192.168.10.200)
web1(192.168.10.33)
web2(192.168.10.34)
在web1和web2执行
[root@web1 ~]# cat lvs_dr_rs.sh #!/bin/bash vip=192.168.10.200 mask='255.255.255.255' dev=lo:1 rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null service httpd start &> /dev/null && echo "The httpd Server is Ready!" echo "<h1>`hostname`</h1>" > /var/www/html/index.html case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #route add -host $vip dev $dev echo "The RS Server is Ready!" ;; stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac
看到后端都获得了vip
在ka1和ka2上把第七步的配置写入,进行测试。
1.所有节点都正常
结论:正常的轮询访问
2.ka1的keepalived服务停止,后端服务正常
结论:ka2接管vip,并正常提供服务,邮箱收到了ka2成为了master的信息
3后端服务器都宕机,ka1和ka2正常(这个页面由ka1和ka2提供,所以需要装个web服务)
4后端服务器都宕机,ka1也宕机,ka2正常