keepalived --- 配置虚拟路由器示例

绘制拓扑

在这里插入图片描述

前提检查

1、时间必须同步

进行各个节点的时间是否同步。如果不同步,进行配置时间同步服务器进行时间同步。

2、确保iptables及selinux不会成为阻碍

进行相应iptables和selinux检查,避免各个节点之间无法进行正常通信和交互。

3、各节点之间可通过主机名互相通信(对KA并非必须)

  • 节点 1

    [root@Neo_Neo ~]# hostname
    Neo_Neo
    [root@Neo_Neo ~]# cat /etc/hosts | grep "192"
    192.168.1.12 Neo_Tang
    [root@Neo_Neo ~]# ping Neo_Tang
    PING Neo_Tang (192.168.1.12) 56(84) bytes of data.
    64 bytes from Neo_Tang (192.168.1.12): icmp_seq=1 ttl=64 time=0.553 ms
    64 bytes from Neo_Tang (192.168.1.12): icmp_seq=2 ttl=64 time=0.581 ms
    64 bytes from Neo_Tang (192.168.1.12): icmp_seq=3 ttl=64 time=0.618 ms
    64 bytes from Neo_Tang (192.168.1.12): icmp_seq=4 ttl=64 time=0.449 ms
    ^C
    --- Neo_Tang ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3003ms
    rtt min/avg/max/mdev = 0.449/0.550/0.618/0.064 ms
    
  • 节点 2

    [root@Neo_Tang ~]# hostname 
    Neo_Tang
    [root@Neo_Tang ~]# cat /etc/hosts | grep "192"
    192.168.1.11 Neo_Neo
    [root@Neo_Tang ~]# ping Neo_Neo
    PING Neo_Neo (192.168.1.11) 56(84) bytes of data.
    64 bytes from Neo_Neo (192.168.1.11): icmp_seq=1 ttl=64 time=0.521 ms
    64 bytes from Neo_Neo (192.168.1.11): icmp_seq=2 ttl=64 time=0.472 ms
    64 bytes from Neo_Neo (192.168.1.11): icmp_seq=3 ttl=64 time=1.40 ms
    64 bytes from Neo_Neo (192.168.1.11): icmp_seq=4 ttl=64 time=0.897 ms
    ^C
    --- Neo_Neo ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3004ms
    rtt min/avg/max/mdev = 0.472/0.824/1.407/0.375 ms
    

4、确保各节点的用于集群服务的接口支持MULTICAST通信

  • 节点 1

    [root@Neo_Neo ~]# ifconfig ens33 
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
            inet6 fe80::20c:29ff:fe47:2ddd  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:47:2d:dd  txqueuelen 1000  (Ethernet)
            RX packets 902  bytes 76346 (74.5 KiB)
            RX errors 0  dropped 132  overruns 0  frame 0
            TX packets 538  bytes 75874 (74.0 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
  • 节点 2

    [root@Neo_Tang ~]# ifconfig ens33
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.12  netmask 255.255.255.0  broadcast 192.168.1.255
            inet6 fe80::20c:29ff:fe85:7535  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:85:75:35  txqueuelen 1000  (Ethernet)
            RX packets 204  bytes 18942 (18.4 KiB)
            RX errors 0  dropped 31  overruns 0  frame 0
            TX packets 147  bytes 17222 (16.8 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    

4.1 接口 MULTICAST 设置方式

[root@Neo_Neo ~]# ifconfig ens33 | grep "ens33"
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

[root@Neo_Neo ~]# ip link set multicast off dev ens33

[root@Neo_Neo ~]# ifconfig ens33 | grep "ens33"
ens33: flags=67<UP,BROADCAST,RUNNING>  mtu 1500

[root@Neo_Neo ~]# ip link set multicast on dev ens33

[root@Neo_Neo ~]# ifconfig ens33 | grep "ens33"
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

虚拟路由器配置

1、keepalived 安装及启动、停止

~]# yum install keepalived -y
~]# rpm -ql keepalived
~]# systemctl start keepalived.service
~]# systemctl stop keepalived.service

2、虚拟路由器配置

2.1 节点 1 配置

[root@Neo_Neo ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   
   notification_email {
   
	root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Neo_Tang
   vrrp_mcast_group4 224.0.0.58
}

vrrp_instance VI_1 {
   
    state BACKUP
    interface ens33
    virtual_router_id 33
    priority 96
    advert_int 1
    authentication {
   
        auth_type PASS
        auth_pass JIAOTANG
    }
    virtual_ipaddress {
   
	192.168.1.99/24 dev ens33 label ens33:0
    }
}

vrrp_instance VI_2 {
   
    state MASTER
    interface ens33
    virtual_router_id 34
    priority 100
    advert_int 1
    authentication {
   
        auth_type PASS
        auth_pass JIAOtANG
    }
    virtual_ipaddress {
   
        192.168.1.98/24 dev ens33 label ens33:1
    }
}

2.2 节点 2 配置

[root@Neo_Tang ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   
   notification_email {
   
	root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id neo-neo
   vrrp_mcast_group4 224.0.0.58
}

vrrp_instance VI_1 {
   
    state MASTER
    interface ens33
    virtual_router_id 33
    priority 100
    advert_int 1
    authentication {
   
        auth_type PASS
        auth_pass JIAOTANG
    }
    virtual_ipaddress {
   
	192.168.1.99/24 dev ens33 label ens33:0
    }
}

vrrp_instance VI_2 {
   
    state BACKUP
    interface ens33
    virtual_router_id 34
    priority 95
    advert_int 1
    authentication {
   
        auth_type PASS
        auth_pass JIAOtANG
    }
    virtual_ipaddress {
   
        192.168.1.98/24 dev ens33 label ens33:1
    }
}

3、状态查看

3.1 节点 1 开启 keepalived 程序,节点 2 不开启

  • 节点 1

    [root@Neo_Neo ~]# systemctl start keepalived.service
    [root@Neo_Neo ~]# systemctl status keepalived.service
    ● keepalived.service - LVS and VRRP High Availability Monitor
       Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
       Active: active (running) since Fri 2019-10-04 04:57:24 EDT; 5s ago
      Process: 6881 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
     Main PID: 6882 (keepalived)
       CGroup: /system.slice/keepalived.service
               ├─6882 /usr/sbin/keepalived -D
               ├─6883 /usr/sbin/keepalived -D
               └─6884 /usr/sbin/keepalived -D
    
    Oct 04 04:57:26 Neo_Neo Keepalived_vrrp[6884]: Sending gratuitous ARP on ens33 for 192.168.1.98
    Oct 04 04:57:28 Neo_Neo Keepalived_vrrp[6884]: VRRP_Instance(VI_1) Transition to MASTER STATE
    Oct 04 04:57:29 Neo_Neo Keepalived_vrrp[6884]: VRRP_Instance(VI_1) Entering MASTER STATE
    Oct 04 04:57:29 Neo_Neo Keepalived_vrrp[6884]: VRRP_Instance(VI_1) setting protocol VIPs.
    Oct 04 04:57:29 Neo_Neo Keepalived_vrrp[6884]: Sending gratuitous ARP on ens33 for 192.168.1.99
    Oct 04 04:57:29 Neo_Neo Keepalived_vrrp[6884]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.99
    Oct 04 04:57:29 Neo_Neo Keepalived_vrrp[6884]: Sending gratuitous ARP on ens33 for 192.168.1.99
    Oct 04 04:57:29 Neo_Neo Keepalived_vrrp[6884]: Sending gratuitous ARP on ens33 for 192.168.1.99
    Oct 04 04:57:29 Neo_Neo Keepalived_vrrp[6884]: Sending gratuitous ARP on ens33 for 192.168.1.99
    Oct 04 04:57:29 Neo_Neo Keepalived_vrrp[6884]: Sending gratuitous ARP on ens33 for 192.168.1.99
    [root@Neo_Neo ~]# ifconfig 
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
            inet6 fe80::20c:29ff:fe47:2ddd  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:47:2d:dd  txqueuelen 1000  (Ethernet)
            RX packets 1553  bytes 127962 (124.9 KiB)
            RX errors 0  dropped 293  overruns 0  frame 0
            TX packets 910  bytes 153296 (149.7 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.99  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:47:2d:dd  txqueuelen 1000  (Ethernet)
    
    ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.98  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:47:2d:dd  txqueuelen 1000  (Ethernet)
    
  • 节点 2

    [root@Neo_Tang ~]# systemctl status keepalived.service
    ● keepalived.service - LVS and VRRP High Availability Monitor
       Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
       Active: inactive (dead)
    
    Oct 04 04:51:42 Neo_Tang Keepalived[7089]: Stopping
    Oct 04 04:51:42 Neo_Tang systemd[1]: Stopping LVS and VRRP High Availability Monitor...
    Oct 04 04:51:42 Neo_Tang Keepalived_vrrp[7091]: VRRP_Instance(VI_1) sent 0 priority
    Oct 04 04:51:42 Neo_Tang Keepalived_vrrp[7091]: VRRP_Instance(VI_1) removing protocol VIPs.
    Oct 04 04:51:42 Neo_Tang Keepalived_vrrp[7091]: VRRP_Instance(VI_2) sent 0 priority
    Oct 04 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值