利用keepalvied实现负载均衡和和高可用性 -凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 855082
  • 博文数量: 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自动接管服务。

阅读(9948) | 评论(0) | 转发(1) |
0

上一篇:lvs负载均衡的配置

下一篇:

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图