生产级(keepalived)部署
keepalived 安装(需要有yum源)
-
ip规划
主机名 ip地址 VIP地址 keepalived模式 test01 192.168.127.5 192.168.127.10 单播模式 test02 192.168.127.6 192.168.127.10 单播模式 -
单播和组播说明
- 在组播模式下,keepalived将全部的心跳包信息都会向默认的224.0.0.18的组播地址发送,这样会产生众多的无用信息,这对于有多个keepalived实例时甚至会产生干扰和冲突,因此须要将组播模式改成单播默认是一种安全的做法,可以避免局域网内有大量的keepalived形成虚拟路由id的冲突。
-
keepalived 安装(依次在1921.68.127.5、1921.68.127.6)进行操作
[root@test01 ~]# yum -y install openssl-devel gcc gcc-c++ libnl libnl-devel wget
[root@test01 ~]# wget https://www.keepalived.org/software/keepalived-2.2.1.tar.gz --no-check-certificate
[root@test01 ~]# tar xvf keepalived-2.2.1.tar.gz
[root@test01 ~]# cd keepalived-2.2.1/
[root@test01 keepalived-2.2.1]# ./configure --prefix=/usr/local/keepalived
[root@test01 keepalived-2.2.1]# make -j 2
[root@test01 keepalived-2.2.1]# make install
[root@test01 keepalived-2.2.1]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@test01 keepalived-2.2.1]# mkdir -p /etc/keepalived/
[root@test01 keepalived-2.2.1]# cp keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@test01 keepalived-2.2.1]# cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
[root@test01 keepalived-2.2.1]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@test01 keepalived-2.2.1]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@test01 keepalived-2.2.1]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: inactive (dead)
[root@test01 keepalived-2.2.1]# keepalived -v
Keepalived v2.2.1 (01/17,2021)
Copyright(C) 2001-2021 Alexandre Cassen, <acassen@gmail.com>
Built with kernel headers for Linux 3.10.0
Running on Linux 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022
Distro: CentOS Linux 7 (Core)
configure options: --prefix=/usr/local/keepalived
Config options: SYSTEMD LVS VRRP VRRP_AUTH OLD_CHKSUM_COMPAT FIB_ROUTING
System options: PIPE2 SIGNALFD INOTIFY_INIT1 VSYSLOG EPOLL_CREATE1 IPV6_ADVANCED_API LIBNL3 RTA_ENCAP RTA_EXPIRES RTA_PREF FRA_SUPPRESS_PREFIXLEN FRA_TUN_ID RTAX_CC_ALGO RTAX_QUICKACK RTA_VIA FRA_OIFNAME IFA_FLAGS IP_MULTICAST_ALL IPTABLES NET_LINUX_IF_H_COLLISION LIBIPVS_NETLINK VRRP_VMAC IFLA_LINK_NETNSID CN_PROC SOCK_NONBLOCK SOCK_CLOEXEC O_PATH GLOB_BRACE GLOB_ALTDIRFUNC INET6_ADDR_GEN_MODE SO_MARK SCHED_RESET_ON_FORK
keepalived配置文件
- 主配置文件(A服务器)
[root@test01 keepalived]# more keepalived.conf
! Configuration File for keepalived
global_defs {
router_id web
}
vrrp_script chk_jincheng {
script "/etc/keepalived/jincheng_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 40
priority 100
nopreempt
advert_int 1
unicast_peer {
192.168.127.5 #对端单播ip
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.127.10 dev eth0
}
track_script {
chk_jincheng
}
}
- 检查脚本(A服务器)
[root@test01 keepalived]# more jincheng_check.sh
#!/bin/bash
#nginx
B=`ps -C nginx --no-header |wc -l`
if [ $B -eq 0 ];then
service keepalived stop #启动失败,将keepalived服务杀死。将vip漂移到其它备份节点
fi
- 主配置文件(B服务器)
[root@test02 keepalived]# more keepalived.conf
! Configuration File for keepalived
global_defs {
router_id web
}
vrrp_script chk_jincheng {
script "/etc/keepalived/jincheng_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 40
priority 100
nopreempt
advert_int 1
unicast_peer {
192.168.127.4 #对端单播ip
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.127.10 dev eth0
}
track_script {
chk_jincheng
}
}
验证keepalived
- 检查脚本(B服务器)
[root@test02 keepalived]# more jincheng_check.sh
#!/bin/bash
#nginx
B=`ps -C nginx --no-header |wc -l`
if [ $B -eq 0 ];then
service keepalived stop #启动失败,将keepalived服务杀死。将vip漂移到其它备份节点
fi
- 查看vip是否成功监听
[root@test01 sbin]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2024-01-12 17:21:57 CST; 7s ago
Process: 361207 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 361208 (keepalived)
Tasks: 3
CGroup: /system.slice/keepalived.service
├─361208 /usr/sbin/keepalived -D
├─361209 /usr/sbin/keepalived -D
└─361210 /usr/sbin/keepalived -D
1月 12 17:21:57 test01 Keepalived_vrrp[361210]: VRRP_Script(chk_jincheng) succeeded
1月 12 17:22:00 test01 Keepalived_vrrp[361210]: VRRP_Instance(VI_1) Transition to MASTER STATE
1月 12 17:22:01 test01 Keepalived_vrrp[361210]: VRRP_Instance(VI_1) Entering MASTER STATE
1月 12 17:22:01 test01 Keepalived_vrrp[361210]: VRRP_Instance(VI_1) setting protocol VIPs.
1月 12 17:22:01 test01 Keepalived_vrrp[361210]: Sending gratuitous ARP on eth0 for 192.168.127.10
- 停止nginx服务,keepalived检测停止keepalived进程
[root@test01 sbin]# ./nginx -s stop
您在 /var/spool/mail/root 中有新邮件
[root@test01 sbin]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 五 2024-01-12 17:23:46 CST; 755ms ago
Process: 361207 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 361208 (code=exited, status=0/SUCCESS)
Tasks: 0
CGroup: /system.slice/keepalived.service
1月 12 17:22:06 test01 Keepalived_vrrp[361210]: Sending gratuitous ARP on eth0 for 192.168.127.10
1月 12 17:22:06 test01 Keepalived_vrrp[361210]: Sending gratuitous ARP on eth0 for 192.168.127.10
1月 12 17:23:45 test01 systemd[1]: Stopping LVS and VRRP High Availability Monitor...
1月 12 17:23:45 test01 Keepalived[361208]: Stopping
1月 12 17:23:45 test01 Keepalived_vrrp[361210]: VRRP_Instance(VI_1) sent 0 priority
1月 12 17:23:45 test01 Keepalived_vrrp[361210]: VRRP_Instance(VI_1) removing protocol VIPs.
1月 12 17:23:45 test01 Keepalived_healthcheckers[361209]: Stopped
1月 12 17:23:46 test01 Keepalived_vrrp[361210]: Stopped
1月 12 17:23:46 test01 Keepalived[361208]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
1月 12 17:23:46 test01 systemd[1]: Stopped LVS and VRRP High Availability Monitor.
- vip是否漂移到B服务器
[root@test01 sbin]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2024-01-12 17:23:46 CST; 7s ago
Process: 361207 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 361208 (keepalived)
Tasks: 3
CGroup: /system.slice/keepalived.service
├─361208 /usr/sbin/keepalived -D
├─361209 /usr/sbin/keepalived -D
└─361210 /usr/sbin/keepalived -D
1月 12 17:23:46 test02 Keepalived_vrrp[361210]: VRRP_Script(chk_jincheng) succeeded
1月 12 17:23:46 test02 Keepalived_vrrp[361210]: VRRP_Instance(VI_1) Transition to MASTER STATE
1月 12 17:23:46 test02 Keepalived_vrrp[361210]: VRRP_Instance(VI_1) Entering MASTER STATE
1月 12 17:23:46 test02 Keepalived_vrrp[361210]: VRRP_Instance(VI_1) setting protocol VIPs.
1月 12 17:23:46 test02 Keepalived_vrrp[361210]: Sending gratuitous ARP on eth0 for 192.168.127.10
评论区