Chinaunix首页 | 论坛 | 博客
  • 博客访问: 872945
  • 博文数量: 72
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1693
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-04 15:53
个人简介

主要从事Linux,云原生架构改造,服务网格,ELK,python,golang等相关技术。

文章分类

全部博文(72)

文章存档

2015年(52)

2014年(20)

分类: 系统运维

2015-04-19 23:25:32

1.在lvs的master节点上设置:
[root@vm1 ~]# yum install keepalived -y
[root@vm1 ~]# vim /etc/keepalived/keepalived.conf

点击(此处)折叠或打开

  1. ! Configuration File for keepalived

  2. global_defs {
  3. notification_email {
  4. yhai_ying@163.com
  5. }
  6. notification_email_from sns-lvs@gmail.com
  7. smtp_server 127.0.0.1
  8. # smtp_connect_timeout 30
  9. router_id LVS_DEVEL
  10. }

  11. #written by :netseek
  12. # VIP1
  13. vrrp_instance VI_1 {
  14. state MASTER #备份服务器上将MASTER改为BACKUP
  15. interface eth0
  16. virtual_router_id 51
  17. priority 100 # 备份服务上将100改为99
  18. advert_int 1
  19. authentication {
  20. auth_type PASS
  21. auth_pass 1111
  22. }
  23. virtual_ipaddress {
  24. 192.168.1.188
  25. #(如果有多个VIP,继续换行填写.)
  26. }
  27. }

  28. virtual_server 192.168.1.188 80 {
  29. delay_loop 6 #(每隔10秒查询realserver状态)
  30. lb_algo wrr #(lvs 算法)
  31. lb_kind DR #(Direct Route)
  32. persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
  33. protocol TCP #(用TCP协议检查realserver状态)

  34. real_server 192.168.1.121 80 {
  35. weight 3 #(权重)
  36. TCP_CHECK {
  37. connect_timeout 10 #(10秒无响应超时)
  38. nb_get_retry 3
  39.         delay_before_retry 3
            connect_port 80
            }
        }
        real_server 192.168.1.122 80 {
            weight 3
            TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
            }
         }
    }
[root@vm1 ~]# /etc/init.d/keepalived start

2.在lvs的backup节点上设置
[root@vm4 ~]# yum install keepalived -y
[root@vm4 ~]# vim /etc/keepalived/keepalived.conf

点击(此处)折叠或打开

  1. ! Configuration File for keepalived

    global_defs {
       notification_email {
             yhai_ying@163.com
       }
       notification_email_from sns-lvs@gmail.com
       smtp_server 127.0.0.1
      # smtp_connect_timeout 30
       router_id LVS_DEVEL
    }

    #written by :netseek
    # VIP1
    vrrp_instance VI_1 {
        state BACKUP             #备份服务器上将MASTER改为BACKUP
        interface eth0
        virtual_router_id 51
        priority 99    # 备份服务上将100改为99
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.1.188
            #(如果有多个VIP,继续换行填写.)
        }
    }

    virtual_server 192.168.1.188 80 {
        delay_loop 6                  #(每隔10秒查询realserver状态)
        lb_algo wrr                  #(lvs 算法)
        lb_kind DR                  #(Direct Route)
        persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
        protocol TCP                #(用TCP协议检查realserver状态)

        real_server 192.168.1.121 80 {
            weight 3               #(权重)
            TCP_CHECK {
            connect_timeout 10       #(10秒无响应超时)
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
            }
        }
        real_server 192.168.1.122 80 {
            weight 3
            TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
            }
         }
    }

下面我们做个测试:
配置完成后我们停掉master上的keepalived服务然后去查看日志文件,看看backup节点是否接管服务:
[root@vm1 ~]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]
[root@vm1 ~]# tail -f /var/log/messages
Apr 20 07:05:43 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 20 07:05:44 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 20 07:05:44 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 20 07:05:44 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.188
Apr 20 07:05:49 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.188
Apr 20 07:08:10 vm1 Keepalived[1160]: Stopping Keepalived v1.2.7 (09/26,2012)
Apr 20 07:08:10 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) sending 0 priority
Apr 20 07:08:10 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 20 07:08:10 vm1 Keepalived_healthcheckers[1162]: Netlink reflector reports IP 192.168.1.188 removed
Apr 20 07:08:10 vm1 kernel: IPVS: __ip_vs_del_service: enter
我们可以看到keepalived健康检查已经被移除。
再来看看backup的日志文件:
[root@vm4 ~]# tail -f /var/log/messages
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: IPVS: Destination already exists
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: IPVS: Destination already exists
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: Using LinkWatch kernel netlink reflector...
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: Activating healthchecker for service [192.168.1.121]:80
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: Activating healthchecker for service [192.168.1.122]:80
Apr 20 07:08:10 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 20 07:08:11 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 20 07:08:11 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 20 07:08:11 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.188
Apr 20 07:08:16 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.188
之前backup节点一直在监视master的keepalived服务的状态,当master节点上健康检查出现故障的时候,backup自动接管服务。

阅读(10199) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~