现场Keepalived配置测试

keepalived配置

配置主备,抢占和非抢占模式下的HA(高可用)

  1. yum 安装 keepalived
  2. 为了通过web来测试,安装nginx,(nginx使用tengine的包)
    1
    2
    yum -y install keepalived
    yum -y install tengine
  3. 分别配置主副节点的keepalived.conf 配置, 目前仅做简单配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    ! Configuration File for keepalived

    global_defs {
    router_id LVS_DEVEL # 删除了email通知
    }

    # 配置一个vrrp_instacne 名字随便
    vrrp_instance VI_1 {
    # 分别是主备
    state MASTER/BACKUP
    # 选择绑定那张网卡,需要是在用的
    interface eth1
    # 主备id要一样
    virtual_router_id 51
    # 主备优先级值一般设置不一样,但是应该遵循master_priority - backup_priority < weight (如果有设置vrrp_script,且设置了weight的情况下)
    priority 100
    # 通信时间
    advert_int 1
    # 认证方式,密码一样,方便主备通信
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    # 绑定的VIP(虚拟IP,即对外用的IP地址),可以是多个,每行一个。
    virtual_ipaddress {
    192.168.10.193
    }
    }
  4. 分别启动服务,然后通过浏览器访问192.168.10.193:15003(我设置nginx监控15003端口,没有使用80),来确定keepalive生效

当master节点挂掉重启后,会自动抢占成为master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
主节点关掉后,主节点日志:
Apr 14 00:02:12 ora12c Keepalived[1944]: Stopping Keepalived v1.2.13 (03/19,2015)
Apr 14 00:02:12 ora12c Keepalived_vrrp[1946]: VRRP_Instance(VI_1) sending 0 priority


备节点的状态:
Apr 13 16:14:00 interface16 Keepalived_vrrp[14033]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 13 16:14:01 interface16 kernel: ACPI: No handler for Region [POWR] (ffff881821ff72b8) [IPMI]
Apr 13 16:14:01 interface16 Keepalived_vrrp[14033]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 13 16:14:01 interface16 Keepalived_vrrp[14033]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 13 16:14:01 interface16 Keepalived_vrrp[14033]: VRRP_Instance(VI_1) Sending gratuitous ARPs on em1 for 192.168.10.193
Apr 13 16:14:01 interface16 Keepalived_healthcheckers[14032]: Netlink reflector reports IP 192.168.10.193 added
Apr 13 16:14:06 interface16 Keepalived_vrrp[14033]: VRRP_Instance(VI_1) Sending gratuitous ARPs on em1 for 192.168.10.193

备用节点ip情况:
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether b8:2a:72:de:40:c3 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.170/24 brd 192.168.10.255 scope global em1
inet 192.168.10.193/32 scope global em1
inet6 fe80::ba2a:72ff:fede:40c3/64 scope link
valid_lft forever preferred_lft forever
此时VIP已经漂移到170这台机器上了

---

主节点:重启后,messages的日志
Apr 13 23:47:43 ora12c Keepalived_healthcheckers[1945]: Using LinkWatch kernel netlink reflector...
Apr 13 23:47:43 ora12c Keepalived_vrrp[1946]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 13 23:47:43 ora12c Keepalived_vrrp[1946]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Apr 13 23:47:44 ora12c Keepalived_vrrp[1946]: VRRP_Instance(VI_1) Entering MASTER STATE

此时主节点ip情况
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:0c:29:10:4f:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.10.191/24 brd 192.168.10.255 scope global eth1
inet 192.168.10.193/32 scope global eth1
设置的对外IP(即VIP)绑定到 191这台机器上了


备节点此时状态:
Apr 13 16:15:22 interface16 Keepalived_vrrp[14033]: VRRP_Instance(VI_1) Received higher prio advert
Apr 13 16:15:22 interface16 Keepalived_vrrp[14033]: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 13 16:15:22 interface16 Keepalived_vrrp[14033]: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 13 16:15:22 interface16 Keepalived_healthcheckers[14032]: Netlink reflector reports IP 192.168.10.193 removed
Apr 13 16:15:24 interface16 ntpd[2105]: Deleting interface #7 em1, 192.168.10.193#123, interface stats: received=0, sent=0, dropped=0, active_time=81 secs

此时备节点ip情况:
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether b8:2a:72:de:40:c3 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.170/24 brd 192.168.10.255 scope global em1
inet6 fe80::ba2a:72ff:fede:40c3/64 scope link
valid_lft forever preferred_lft forever


非抢占模式

非抢占模式配置:

  1. 主备文件中都在vrrp_instance中添加参数nopreempt,表示不争抢
  2. 在vrrp_instance中将state都改为BACKUP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34


    修改完成后先停用主节点,重启备用节点的keepalived进程,
    目的是,让备用节点先拿到VIP,验证启动主节点进程后,VIP不会漂移到主节点 --ok,因为配置了nopreempt参数,所以主节点不会抢占,

    此时备用节点ip情况:
    2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether b8:2a:72:de:40:c3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.170/24 brd 192.168.10.255 scope global em1
    inet 192.168.10.193/32 scope global em1

    杀掉备用节点进程
    [root@interface16 ~]# killall keepalived

    此时主节点会变成MASTER主节点,日志:
    Apr 14 00:20:16 ora12c Keepalived_vrrp[2213]: VRRP_Instance(VI_1) Transition to MASTER STATE
    Apr 14 00:20:17 ora12c Keepalived_vrrp[2213]: VRRP_Instance(VI_1) Entering MASTER STATE
    Apr 14 00:20:17 ora12c Keepalived_vrrp[2213]: VRRP_Instance(VI_1) setting protocol VIPs.
    Apr 14 00:20:17 ora12c Keepalived_vrrp[2213]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.10.193
    Apr 14 00:20:17 ora12c Keepalived_healthcheckers[2212]: Netlink reflector reports IP 192.168.10.193 added
    Apr 14 00:20:22 ora12c Keepalived_vrrp[2213]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.10.193

    主节点ip情况:
    2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:10:4f:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.191/24 brd 192.168.10.255 scope global eth1
    inet 192.168.10.193/32 scope global eth1
    inet6 fe80::20c:29ff:fe10:4fbc/64 scope link
    valid_lft forever preferred_lft foreve


    综上,当设置nopreempt非抢占模式时,会根据priority大小选举一个MASTER节点,但是当MASTER节点的keepalived进程出现问题,即使恢复后,就不会再主动抢占MASTER,
    但是如果其他备用节点的keepalived进程都出现问题时,该节点会自动接管成为MASTER。
    --所以,这也是网上教程中都在提的双主模式下,通过杀掉keepalived进程,实现出现问题时完成主备切换。
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2023 ligongzhao
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信