设为首页 收藏本站
查看: 1849|回复: 0

[经验分享] LVS+KEEPALIVED+NAT详解

[复制链接]

尚未签到

发表于 2015-12-24 10:56:33 | 显示全部楼层 |阅读模式
相信大家对LVS+ KEEPALIVED已经很熟悉了。LVS即LinuxVirtualServer;KEEPALIVED是为LVS设计的,主要提供了VRRP功能,解决静态路由的单点故障的问题,并且还能够检测每个服务节点的健康状态,当出现故障节点,keepalived能够剔除该节点,当故障节点回复后,又能够重新加入集群。


       本实验是LVS+KEEPALIVED+NAT模式,在企业里这种模式用的比较少,适合小的访问量。正因如此,此方面的正式文档比较少,这也是我写此博客的原因。NAT模式所有的数据量都需要通过LD,所以LD得负载比较大,为减少LD的负载,可以选择使用TUN模式。
实验架构简图如下:
DSC0000.png

LD:
station1 :外网IPeth0 172.16.1.11
内网IPeth1 192.168.1.11
Station2:外网 IP eth0 172.16.1.12
内网:IPeth1 192.168.1.12
浮动IP172.16.1.100
网关浮动IP192.168.1.254


Realserver: Apache1 192.168.1.13
                 Apache2 192.168.1.14

下面开始进行配置:

在station1上
打开路由功能。
安装keepalived-1.2.7



  • yum -y install gcc make openssl-devel openssl net-snmp net-snmp-devel popt popt-devel

  • ./configure --prefix=/usr/local/keepalived –enable-snmp

  • make && make install
  • ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
  • ln -s /usr/local/keepalived/etc/keepalived.conf /etc/keepalived.conf
修改主配置文件:



  • ! Configuration File for keepalived

  • global_defs {
  •     router_id KL_HOST1
  • }

  • vrrp_instance VI_1 {
  •     state BACKUP
  •     interface eth0
  •     virtual_router_id 51
  •     priority 150
  •     nopreempt
  •     advert_int 1
  •     authentication {
  •         auth_type PASS
  •         auth_pass 1111
  •     }
  • virtual_ipaddress {
  •     172.16.1.100/24
  •     }
  • }

  • vrrp_instance VI_2 {
  •     state BACKUP
  •     interface eth1
  •     nopreempt
  •     virtual_router_id 52
  •     priority 150
  •     advert_int 1
  •     authentication {
  •         auth_type PASS
  •         auth_pass 2222
  • }
  • virtual_ipaddress {
  •     192.168.1.254/24
  • }
  • }
  • vrrp_sync_group VG_1 {
  •     group {
  •         VI_1
  •         VI_2
  •     }
  • }

  • virtual_server 172.16.1.100 80 {
  •     delay_loop 6
  •     lb_algo rr
  •     lb_kind NAT
  •     nat_mask 255.255.255.0
  •     persistence_timeout 50
  •        protocol TCP

  • #    sorry_server 192.168.200.200 1358

  • real_server 192.168.1.13 80 {
  •     weight 1
  •     HTTP_GET {
  •         url {
  •             path /urltest/test.html
  •                digest 37dba1d9a3c103df127b4e957c9de188
  • }
  •     connect_timeout 3
  •     nb_get_retry 3
  •     delay_before_retry 3
  •     }
  • }

  • real_server 192.168.1.14 80 {
  •     weight 2
  •     HTTP_GET {
  •         url {
  •             path /urltest/test.html
  •             digest 37dba1d9a3c103df127b4e957c9de188
  •     }
  •     connect_timeout 3
  •     nb_get_retry 3
  •     delay_before_retry 3
  •         }
  •     }
  • }
这里面的md5值是使用如下命令生成的。



  • /usr/local/keepalived/bin/genhash -s 192.168.1.14 -p 80 -u /urltest/test.html
防火墙配置:



  • [root@station1 tmp]# iptables -L -n

  • Chain INPUT (policy ACCEPT)
  • target     prot opt source               destination
  • ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:80
  • ACCEPT     112 -- 0.0.0.0/0            0.0.0.0/0
  • ACCEPT     all -- 0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
  • ACCEPT     icmp -- 0.0.0.0/0            0.0.0.0/0
  • ACCEPT     all -- 0.0.0.0/0            0.0.0.0/0
  • ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
  • REJECT     all -- 0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

  • Chain FORWARD (policy ACCEPT)
  • target     prot opt source               destination

  • Chain OUTPUT (policy ACCEPT)
  • target     prot opt source               destination
  • [root@station1 tmp]# iptables -L -n -t nat
  • Chain PREROUTING (policy ACCEPT)
  • target     prot opt source               destination

  • Chain POSTROUTING (policy ACCEPT)
  • target     prot opt source               destination
  • MASQUERADE  all -- 192.168.1.0/24       0.0.0.0/0

  • Chain OUTPUT (policy ACCEPT)
  • target     prot opt source               destination
  • [root@station1 tmp]#
在station2操作
打开路由功能
安装keepalived-1.2.7



  • yum -y install gcc make openssl-devel openssl net-snmp net-snmp-devel popt popt-devel

  • ./configure --prefix=/usr/local/keepalived –enable-snmp

  • make && make install
  • ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
  • ln -s /usr/local/keepalived/etc/keepalived.conf /etc/keepalived.conf
修改主配置文件:





  • ! Configuration File for keepalived


  • global_defs {
  •    router_id KL_HOST2
  • }

  • vrrp_instance VI_1 {
  •     state BACKUP
  •     interface eth0
  •     virtual_router_id 51
  •     priority 100
  •     advert_int 1
  •     authentication {
  •         auth_type PASS
  •         auth_pass 1111
  •     }
  •     virtual_ipaddress {
  •         172.16.1.100/24
  •     }
  • }

  • vrrp_instance VI_2 {
  •     state BACKUP
  •     interface eth1
  •     virtual_router_id 52
  •     priority 100
  •     advert_int 1
  •     authentication {
  •     auth_type PASS
  •     auth_pass 2222
  •     }
  •     virtual_ipaddress {
  •     192.168.1.254/24
  •     }
  • }
  • vrrp_sync_group VG_1 {
  •     group {
  •     VI_1
  •     VI_2
  •    }
  • }

  • virtual_server 172.16.1.100 80 {
  •     delay_loop 6
  •     lb_algo rr
  •     lb_kind NAT
  •     nat_mask 255.255.255.0
  •     persistence_timeout 50
  •     protocol TCP

  • #    sorry_server 192.168.200.200 1358

  •     real_server 192.168.1.13 80 {
  •         weight 1
  •         HTTP_GET {
  •             url {
  •               path /urltest/test.html
  •               digest 37dba1d9a3c103df127b4e957c9de188
  •             }
  •             connect_timeout 3
  •             nb_get_retry 3
  •             delay_before_retry 3
  •         }
  •     }

  •     real_server 192.168.1.14 80 {
  •         weight 2
  •         HTTP_GET {
  •             url {
  •               path /urltest/test.html
  •               digest 37dba1d9a3c103df127b4e957c9de188
  •             }
  •             connect_timeout 3
  •             nb_get_retry 3
  •             delay_before_retry 3
  •         }
  •     }
  • }
station2的防火墙和station1的完全一样

apache2和apache1上做如下相同配置





  • yum -y install httpd
  • mkdir /var/www/html/urltest/
  • echo “this is a test page” > /var/www/html/urltest/test.html

  • iptables -I INPUT -p tcp –dport 80 -j ACCEPT
  • service iptables save
  • service httpd start
  • chkconfig httpd on

所有配置已经完成
在staiton1和station2上开启keepalived服务





  • keepalived -f /etc/keepalived.conf

  • echo “keepalived -f /etc/keepalived.conf” >> /etc/rc.local


试keepalived的vrrp功能和集群功能都没有任何问题!



  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-155630-1-1.html 上篇帖子: 用keepalived来lvs配置及其问题初汇 下篇帖子: 【原创】基于keepalived+lvs支持多虚拟IP
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表