这里主要是写怎么配置,不过多的介绍怎么安装和原理性以及理论的东西,如果需要学习这些知识:推荐博文:https://www.cnblogs.com/clsn/p/8052649.html
0、这里使用的版本是:keepalived-2.0.18
#网口信息如下: [root@keepalived-2 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.154 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::ba2e:3184:84f:b768 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:1b:ac:7a txqueuelen 1000 (Ethernet) RX packets 18030 bytes 1326828 (1.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 16786 bytes 1284842 (1.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.186.141 netmask 255.255.255.0 broadcast 192.168.186.255 inet6 fe80::c37c:f811:9ee4:15bd prefixlen 64 scopeid 0x20<link> ether 00:0c:29:1b:ac:84 txqueuelen 1000 (Ethernet) RX packets 711 bytes 69470 (67.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 334 bytes 28146 (27.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1、主-keepalived配置
[root@keepalived-1 ~]# cat /usr/local/keepalived-2.0.18/etc/keepalived/keepalived.conf ! Configuration File for keepalived ! extra script call demonstration ! scripts are supported in Instance and groups ! declarations. global_defs { router_id K_1 script_user root } ! 定义监控脚本位置 vrrp_script chk_nginx { script "/usr/local/keepalived-2.0.18/script/check_nginx.sh" interval 1 weight 2 fall 3 rise 1 } ! 注意 virtual_router_id必须一致,priority数值越高,越优先 vrrp_instance VI_1 { state MASTER interface ens33 ens37 smtp_alert virtual_router_id 1 priority 150 advert_int 1 authentication { auth_type PASS auth_pass iVZWO } ! 配置虚拟IP地址 virtual_ipaddress { 192.168.2.200/24 dev ens33 192.168.186.200/24 dev ens37 } ! 这里是上面检测脚本的名称 track_script{ chk_nginx } }
2、备-keepalived配置
[root@keepalived-2 ~]# cat /usr/local/keepalived-2.0.18/etc/keepalived/keepalived.conf ! Configuration File for keepalived ! extra script call demonstration ! scripts are supported in Instance and groups ! declarations. global_defs { router_id K_2 script_user root } ! 定义监控脚本位置 vrrp_script chk_nginx { script "/usr/local/keepalived-2.0.18/script/check_nginx.sh" interval 1 weight 2 fall 3 rise 1 } ! 注意 virtual_router_id不能重复,priority优先级比主的低 vrrp_instance VI_1 { state BACKUP interface ens33 ens37 smtp_alert virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass iVZWO } ! 配置虚拟IP地址 virtual_ipaddress { 192.168.2.200/24 dev ens33 192.168.186.200/24 dev ens37 } ! 这里是上面检测脚本的名称 track_script{ chk_nginx } }
3、配置完成后,重启keepalived的服务
#主的服务器 [root@keepalived-1 ~]# /etc/init.d/keepalived restart #备的服务器 [root@keepalived-2 ~]# /etc/init.d/keepalived restart
4、检查VIP是生成
[root@keepalived-1 ~]# ip addr | grep "200" inet 192.168.2.200/24 scope global secondary ens33 inet 192.168.186.200/24 scope global secondary ens37 [root@keepalived-2 ~]# ip addr | grep "200"