Rootop 服务器运维与web架构

keepalived+lvs构建高可用 实战篇

| 暂无评论

上一篇文章中说到了两台机器之间的热备问题,并且已经实现主机down机后,备份机自动接管VIP。现在需要在此基础上不仅要提供主备之间自动切换,而且实现real server健康检查,通过keepalived就可以实现,因为2.6版本以后的内核都默认支持ipvs,不需要再单独安装lvs。通过keepalived参数定义即可实现lvs功能。

服务器环境IP信息:
real server 1        :192.168.1.10
real server 2        :192.168.1.11
direct server master :192.168.1.12
direct server slaves :192.168.1.13
VIP                  :192.168.1.14

查看是否支持:

[root@localhost ~]# modprobe -l | grep ipvs
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_dh.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_ftp.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_lblc.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_lblcr.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_lc.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_nq.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_rr.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_sed.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_sh.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_wlc.ko
/lib/modules/2.6.18-194.el5/kernel/net/ipv4/ipvs/ip_vs_wrr.ko

本文章跟之前有关keepalived包括lvs文章中出现的ip信息不一样,这是因为一部分是在公司部署环境并记录的,一部分是在家中,IP信息不一样,大家注意一下。

安装keepalived前面已经说过,不再提,编辑主配置文件:

! Configuration File for keepalived

global_defs {
   notification_email {
   root@networkquestions.org
   }
   notification_email_from venus@networkquestions.org
   smtp_server localhost
   smtp_connect_timeout 30
   router_id LVS_Node_master
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.14                //提供服务的VIP
    }
}

virtual_server 192.168.1.14 80 {    //定义虚拟服务器组
    delay_loop 6                    //运行情况检查
    lb_algo rr                      //调度算法
    lb_kind DR                      //负载机制
    persistence_timeout 50          //会话保持时间
    protocol TCP                    //定义转发协议类型

    real_server 192.168.1.10 80 {   //节点服务器
        weight 1                    //权重
        TCP_CHECK {                 //tcp 检查
        connect_timeout 3           //连接超时/秒
        nb_get_retry 3              //重试次数
        delay_before_retry 3        //重试间隔
                  }
           }
     real_server 192.168.1.11 80 {
        weight 1
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
                    }
            }
    }

}

保存,退出,在direct server BACKUP中直接复制粘贴以上部分。只需修改从机为BACKUP状态和priority 99权值,小于主机即可。重启keepalived。

这样direct server和real server的健康检查配置完成。在real server中只需参考:https://www.rootop.org/pages/2078.html  运行real server的执行脚本即可。

direct server 主备切换测试:

停掉主direct server的keepalived服务,或者直接拔掉网线。

[root@localhost ~]# service keepalived stop
停止 keepalived: [确定]
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
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 qlen 1000
link/ether 00:0c:29:5b:dd:da brd ff:ff:ff:ff:ff:ff
inet 192.168.1.12/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe5b:ddda/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0

主服务器释放192.168.1.14 IP,查看备用服务器:

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
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 qlen 1000
link/ether 00:0c:29:3b:7e:f3 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.13/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.14/32 scope global eth0
inet6 fe80::20c:29ff:fe3b:7ef3/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0

备机接管vip。

通过浏览器访问VIP,返回内容正常。启动主服务器keepalived服务,重新接管VIP。

real server 健康检查:

停掉real server 1的web服务或者拔掉网线,查看direct server的 /var/log/messages

[root@localhost ~]# tail -f /var/log/messages
Feb 22 09:42:10 localhost Keepalived_healthcheckers[3353]: TCP connection to [192.168.1.10]:80 failed !!!
Feb 22 09:42:11 localhost Keepalived_healthcheckers[3353]: Removing service [192.168.1.10]:80 from VS [192.168.1.14]:80
Feb 22 09:42:11 localhost Keepalived_healthcheckers[3353]: Remote SMTP server [0.0.0.0]:25 connected.
Feb 22 09:42:12 localhost Keepalived_healthcheckers[3353]: SMTP alert successfully sent.
web服务停止后自动从vs中移出节点。

启动real server 1 的web服务,查看direct server 日志:
Feb 22 09:43:35 localhost Keepalived_healthcheckers[3353]: TCP connection to [192.168.1.10]:80 success.
Feb 22 09:43:35 localhost Keepalived_healthcheckers[3353]: Adding service [192.168.1.10]:80 to VS [192.168.1.14]:80
Feb 22 09:43:35 localhost Keepalived_healthcheckers[3353]: Remote SMTP server [0.0.0.0]:25 connected.
Feb 22 09:43:36 localhost Keepalived_healthcheckers[3353]: SMTP alert successfully sent.

web服务恢复后节点自动加到vs中。此过程中,运维只需要修复web服务,无需对keepalived做任何配置。

 

 

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/2125.html

作者:Venus

专注于 服务器运维与web架构 E-mail:venus#rootop.org

发表评论