Linux下keepalived配置
阿新 • • 發佈:2020-08-21
一、環境 主庫(node01):192.168.12.35 埠:3306 從庫(node02):192.168.12.36 埠:3306 vip:192.168.12.38 主庫,主從資料庫伺服器已搭建MySQL主從。 2、配置keepalived實現MySQL主從高可用 keepalived配置如下: node01: ! Configuration File for keepalived global_defs { router_id node01 vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER interface ens32 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.12.38/24 } } virtual_server 192.168.12.38 3306 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 192.168.12.35 3306 { weight 1 notify_down /root/kill_keepalived.sh TCP_CHECK { connect_timeout 3 connect_port 3306 } } real_server 192.168.12.36 3306 { weight 1 TCP_CHECK { connect_timeout 3 connect_port 3306 } } } 指令碼/root/kill_keepalived.sh內容如下: #!/bin/bash systemctl stop keepalived node02: ! Configuration File for keepalived global_defs { router_id node02 vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP interface ens32 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.12.38/24 } } virtual_server 192.168.12.38 3306 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 192.168.12.35 3306 { weight 1 TCP_CHECK { connect_timeout 3 connect_port 3306 } } real_server 192.168.12.36 3306 { weight 1 TCP_CHECK { connect_timeout 3 connect_port 3306 } } } 啟動主從資料庫服和keepalived服務,檢視IP [root@node01 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:30:cc:38 brd ff:ff:ff:ff:ff:ff inet 192.168.12.35/24 brd 192.168.12.255 scope global noprefixroute ens32 valid_lft forever preferred_lft forever inet 192.168.12.38/24 scope global secondary ens32 valid_lft forever preferred_lft forever inet6 fe80::df6c:9284:28fd:7480/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@node02 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:25:89:7e brd ff:ff:ff:ff:ff:ff inet 192.168.12.36/24 brd 192.168.12.255 scope global noprefixroute ens32 valid_lft forever preferred_lft forever inet6 fe80::a547:b48:6f4c:6d64/64 scope link noprefixroute valid_lft forever preferred_lft forever inet6 fe80::df6c:9284:28fd:7480/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever 3、vip切換測試 node01: [root@node01 ~]# /etc/init.d/mysqld stop Shutting down MySQL.. SUCCESS! [root@node01 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:30:cc:38 brd ff:ff:ff:ff:ff:ff inet 192.168.12.35/24 brd 192.168.12.255 scope global noprefixroute ens32 valid_lft forever preferred_lft forever inet6 fe80::df6c:9284:28fd:7480/64 scope link noprefixroute valid_lft forever preferred_lft forever node02: [root@node02 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:25:89:7e brd ff:ff:ff:ff:ff:ff inet 192.168.12.36/24 brd 192.168.12.255 scope global noprefixroute ens32 valid_lft forever preferred_lft forever inet 192.168.12.38/24 scope global secondary ens32 valid_lft forever preferred_lft forever inet6 fe80::a547:b48:6f4c:6d64/64 scope link noprefixroute valid_lft forever preferred_lft forever inet6 fe80::df6c:9284:28fd:7480/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever vip:192.168.12.38漂移到node02節點。 重新把node01資料庫和keepalived服務啟動: [root@node01 ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! [root@node01 ~]# systemctl start keepalived [root@node01 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:30:cc:38 brd ff:ff:ff:ff:ff:ff inet 192.168.12.35/24 brd 192.168.12.255 scope global noprefixroute ens32 valid_lft forever preferred_lft forever inet6 fe80::df6c:9284:28fd:7480/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@node01 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:30:cc:38 brd ff:ff:ff:ff:ff:ff inet 192.168.12.35/24 brd 192.168.12.255 scope global noprefixroute ens32 valid_lft forever preferred_lft forever inet 192.168.12.38/24 scope global secondary ens32 valid_lft forever preferred_lft forever inet6 fe80::df6c:9284:28fd:7480/64 scope link noprefixroute valid_lft forever preferred_lft forever 再次檢視node02節點IP: [root@node02 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:25:89:7e brd ff:ff:ff:ff:ff:ff inet 192.168.12.36/24 brd 192.168.12.255 scope global noprefixroute ens32 valid_lft forever preferred_lft forever inet6 fe80::a547:b48:6f4c:6d64/64 scope link noprefixroute valid_lft forever preferred_lft forever inet6 fe80::df6c:9284:28fd:7480/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever 可以正常切換了