Keepalived 简单搭建高可用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived 简单搭建高可用相关的知识,希望对你有一定的参考价值。


    环境:

      node1:192.168.11.26

      node2:192.168.11.28

      VIP:192.168.11.99

Centos版本6.5 Keepalived Centos6.4+就收入base仓库,可直接Yum安装,本次采用Yum安装


一、node1配置

[[email protected] keepalived]# yum isntall -y keepalived    #安装keepallived
[[email protected] keepalived]# cd /etc/keepalived/          #进入/etc/keepalived
[[email protected] keepalived]# cp keepalived.conf{,.bak}    #备份
[[email protected] keepalived]# vim keepalived.conf          #配置Keepalived简单实现VIP漂移
! Configuration File for keepalived

global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 6Nb6zjWB
    }
    virtual_ipaddress {
        192.168.11.99
    }
}
#后面的删除,本次仅简单配置VIP漂移功能

二、node2配置

[[email protected]_master keepalived]# yum isntall -y keepalived    #安装keepallived
[[email protected]_master keepalived]# cd /etc/keepalived/          #进入/etc/keepalived
[[email protected]_master keepalived]# cp keepalived.conf{,.bak}    #备份
[[email protected]_master keepalived]# vim keepalived.conf          #配置Keepalived简单实现VIP漂移
! Configuration File for keepalived

global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP                                            #修改为BACKUP
    interface eth0
    virtual_router_id 51
    priority 98                                             #修改优先级98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 6Nb6zjWB
    }
    virtual_ipaddress {
        192.168.11.99
    }
}

三、启动示例

[[email protected]_master keepalived]# service keepalived start    #启动node2节点的keepalived
Starting keepalived:                                       [  OK  ]
[[email protected]_master keepalived]# ip add sh                   #查看配置成功:inet 192.168.11.99/32 scope global eth0
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:ba:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.28/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.99/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:bab0/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected]_master ~]# tail -f /var/log/messages           #查看日志:Netlink reflector reports IP 192.168.11.99 added,不关闭继续监视,去启动MASTER

Oct 20 22:25:33 redis_master Keepalived[24842]: Starting Keepalived v1.2.13 (03/19,2015)
Oct 20 22:25:33 redis_master Keepalived[24843]: Starting Healthcheck child process, pid=24844
Oct 20 22:25:33 redis_master Keepalived[24843]: Starting VRRP child process, pid=24845
Oct 20 22:25:33 redis_master Keepalived_vrrp[24845]: Netlink reflector reports IP 192.168.11.28 added
Oct 20 22:25:33 redis_master Keepalived_healthcheckers[24844]: Netlink reflector reports IP 192.168.11.28 added
Oct 20 22:25:33 redis_master Keepalived_vrrp[24845]: Netlink reflector reports IP fe80::20c:29ff:fe2d:bab0 added
Oct 20 22:25:33 redis_master Keepalived_vrrp[24845]: Registering Kernel netlink reflector
Oct 20 22:25:33 redis_master Keepalived_vrrp[24845]: Registering Kernel netlink command channel
Oct 20 22:25:33 redis_master Keepalived_healthcheckers[24844]: Netlink reflector reports IP fe80::20c:29ff:fe2d:bab0 added
Oct 20 22:25:33 redis_master Keepalived_vrrp[24845]: Registering gratuitous ARP shared channel
Oct 20 22:25:33 redis_master Keepalived_healthcheckers[24844]: Registering Kernel netlink reflector
Oct 20 22:25:33 redis_master Keepalived_healthcheckers[24844]: Registering Kernel netlink command channel
Oct 20 22:25:34 redis_master Keepalived_healthcheckers[24844]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Oct 20 22:25:34 redis_master Keepalived_vrrp[24845]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Oct 20 22:25:34 redis_master Keepalived_vrrp[24845]: Configuration is using : 62847 Bytes
Oct 20 22:25:34 redis_master Keepalived_healthcheckers[24844]: Configuration is using : 7390 Bytes
Oct 20 22:25:34 redis_master Keepalived_vrrp[24845]: Using LinkWatch kernel netlink reflector...
Oct 20 22:25:34 redis_master Keepalived_healthcheckers[24844]: Using LinkWatch kernel netlink reflector...
Oct 20 22:25:34 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) Entering BACKUP STATE
Oct 20 22:25:34 redis_master Keepalived_vrrp[24845]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Oct 20 22:25:38 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 20 22:25:39 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 20 22:25:39 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 20 22:25:39 redis_master Keepalived_healthcheckers[24844]: Netlink reflector reports IP 192.168.11.99 added
Oct 20 22:25:39 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.99
Oct 20 22:25:44 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.99

Oct 20 22:29:03 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) Received higher prio advert                #提示发现更高的优先级
Oct 20 22:29:03 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) Entering BACKUP STATE                      #进入BACKUP状态
Oct 20 22:29:03 redis_master Keepalived_vrrp[24845]: VRRP_Instance(VI_1) removing protocol VIPs.                    #删除VIPs协议
Oct 20 22:29:03 redis_master Keepalived_healthcheckers[24844]: Netlink reflector reports IP 192.168.11.99 removed   #删除VIP地址并通告


四、node1启动keepalived

[[email protected] keepalived]# service keepalived start                        #启动keepalived
正在启动 keepalived:                                      [确定]
[[email protected] keepalived]# ip add sh                                       #查看配置成功:inet 192.168.11.99/32 scope global eth0
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ad:98:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.26/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.17/32 brd 192.168.11.17 scope global eth0:0
    inet 192.168.11.99/32 scope global eth0
    inet6 fe80::20c:29ff:fead:985c/64 scope link 
[[email protected] ~]# tail -f /var/log/messages                                #查看日志:Netlink reflector reports IP 192.168.11.99 added
Jun 23 06:19:35 web2 Keepalived[9882]: Starting Keepalived v1.2.13 (03/19,2015)
Jun 23 06:19:35 web2 Keepalived[9883]: Starting Healthcheck child process, pid=9885
Jun 23 06:19:35 web2 Keepalived[9883]: Starting VRRP child process, pid=9886
Jun 23 06:19:35 web2 Keepalived_vrrp[9886]: Netlink reflector reports IP 192.168.11.26 added
Jun 23 06:19:35 web2 Keepalived_vrrp[9886]: Netlink reflector reports IP 192.168.11.17 added
Jun 23 06:19:35 web2 Keepalived_vrrp[9886]: Netlink reflector reports IP 20.1.1.254 added
Jun 23 06:19:35 web2 Keepalived_vrrp[9886]: Netlink reflector reports IP fe80::20c:29ff:fead:985c added
Jun 23 06:19:35 web2 Keepalived_vrrp[9886]: Netlink reflector reports IP fe80::20c:29ff:fead:9866 added
Jun 23 06:19:35 web2 Keepalived_vrrp[9886]: Registering Kernel netlink reflector
Jun 23 06:19:35 web2 Keepalived_vrrp[9886]: Registering Kernel netlink command channel
Jun 23 06:19:35 web2 Keepalived_vrrp[9886]: Registering gratuitous ARP shared channel
Jun 23 06:19:35 web2 Keepalived_healthcheckers[9885]: Netlink reflector reports IP 192.168.11.26 added
Jun 23 06:19:35 web2 Keepalived_healthcheckers[9885]: Netlink reflector reports IP 192.168.11.17 added
Jun 23 06:19:35 web2 Keepalived_healthcheckers[9885]: Netlink reflector reports IP 20.1.1.254 added
Jun 23 06:19:35 web2 Keepalived_healthcheckers[9885]: Netlink reflector reports IP fe80::20c:29ff:fead:985c added
Jun 23 06:19:35 web2 Keepalived_healthcheckers[9885]: Netlink reflector reports IP fe80::20c:29ff:fead:9866 added
Jun 23 06:19:35 web2 Keepalived_healthcheckers[9885]: Registering Kernel netlink reflector
Jun 23 06:19:35 web2 Keepalived_healthcheckers[9885]: Registering Kernel netlink command channel
Jun 23 06:19:37 web2 Keepalived_healthcheckers[9885]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Jun 23 06:19:37 web2 Keepalived_healthcheckers[9885]: Configuration is using : 7520 Bytes
Jun 23 06:19:37 web2 Keepalived_healthcheckers[9885]: Using LinkWatch kernel netlink reflector...
Jun 23 06:19:45 web2 Keepalived_vrrp[9886]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Jun 23 06:19:45 web2 Keepalived_vrrp[9886]: Configuration is using : 62977 Bytes
Jun 23 06:19:45 web2 Keepalived_vrrp[9886]: Using LinkWatch kernel netlink reflector...
Jun 23 06:19:45 web2 Keepalived_vrrp[9886]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jun 23 06:19:46 web2 Keepalived_vrrp[9886]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 23 06:19:46 web2 Keepalived_vrrp[9886]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Jun 23 06:19:47 web2 Keepalived_vrrp[9886]: VRRP_Instance(VI_1) Entering MASTER STATE
Jun 23 06:19:47 web2 Keepalived_vrrp[9886]: VRRP_Instance(VI_1) setting protocol VIPs.
Jun 23 06:19:47 web2 Keepalived_vrrp[9886]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.99
Jun 23 06:19:47 web2 Keepalived_healthcheckers[9885]: Netlink reflector reports IP 192.168.11.99 added
Jun 23 06:19:52 web2 Keepalived_vrrp[9886]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.99


本文出自 “SunshineBoySZF” 博客,请务必保留此出处http://sunshineboyszf.blog.51cto.com/12087328/1864063

以上是关于Keepalived 简单搭建高可用的主要内容,如果未能解决你的问题,请参考以下文章

Nginx+Keepalived搭建高可用负载平衡WEB 集群

LVS + Keepalived 搭建高可用的负载均衡群集

Lvs+keepalived+httpd+NFS搭建高可用

Keepalived+Haproxy搭建高可用Web群集

Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)

keepalived+nginx搭建高可用and负载均衡集群