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

[经验分享] RHEL6 搭建LVS/NAT 负载均衡集群 案例

[复制链接]
YunVN网友  发表于 2019-1-4 10:18:23 |阅读模式
  搭建LVS/NAT案例

  实验拓扑图

  操作流程
  Director Server :     192.168.4.50 pc50
  安装并启用ipvsadm
  创建虚拟服务器
  向虚拟服务器上加入节点
  Real Server :     192.168.4.51 pc51     192.168.4.52 pc52
  配置WEB 服务器
  Clinet :    192.168.2.253 pc253
  连接虚拟服务器测试
  具体步骤
  环境准备
  配置yum源
  # service iptables stop            //关闭防火墙
  # chkconfig iptables off            //关闭开机自启
  # setenforce 0                            //设置SELinux 为宽松模式
  

  网站服务器     pc51 /  pc52
  # yum -y install httpd
  [root@pc51 ~]# echo '192.168.4.51' > /var/www/html/test.html
  [root@pc52 ~]# echo "192.168.4.52" > /var/www/html/test.html
  # service httpd start
  # chkconfig httpd on
  # yum -y install elinks
  [root@pc51 ~]# elinks --dump http://localhost/test.html
     192.168.4.51
  [root@pc52 ~]# elinks --dump http://localhost/test.html
     192.168.4.52
  

  配置分发器 pc50
  # mount /dev/cdrom /mnt/
  //安装 ipvsadm   rpm 包在光盘挂载文件下的LoadBalancer目录下
  #cd /mnt/LoadBalancer/
  #yum -y install ipvsadm-1.26-4.el6.x86_64.rpm
  //开启内核的路由转发功能
  # sed -i '7s/0/1/' /etc/sysctl.conf
  # sed -n '7p' /etc/sysctl.conf
  net.ipv4.ip_forward = 1
  网站服务器 pc51 /pc52
  指定网关地址 192.168.4.50
  # route -n//查看路由
  # route add default gw 192.168.4.50//临时配置网关 网卡重启后生效
  //永久配置网关
  # vim /etc/sysconfig/network-scripts/ifcfg-eth0
  # sed -n '7p' /etc/sysconfig/network-scripts/ifcfg-eth0
  GATEWAY=192.168.4.50
  # ifdown eth0 ; ifup eth0      //重新加载网卡
  客户端 192.168.2.253 配置
  指定网关地址 192.168.2.50 :
  # vim /etc/sysconfig/network-scripts/ifcfg-eth1
  # sed -n '7p' /etc/sysconfig/network-scripts/ifcfg-eth1
  GATEWAY=192.168.2.50
  # ifdown eth1 ; ifup eth1
  # ping -c 2 192.168.4.51
  PING 192.168.4.51 (192.168.4.51) 56(84) bytes of data.
  64 bytes from 192.168.4.51: icmp_seq=1 ttl=63 time=0.322 ms
  64 bytes from 192.168.4.51: icmp_seq=2 ttl=63 time=0.503 ms
  

  --- 192.168.4.51 ping statistics ---
  2 packets transmitted, 2 received, 0% packet loss, time 1000ms
  rtt min/avg/max/mdev = 0.322/0.412/0.503/0.092 ms
  # yum -y install elinks
  

  配置分发器 pc50
  # yum -y install ipvsadm-1.26-4.el6.x86_64.rpm
  # rpm -q ipvsadm
  ipvsadm-1.26-4.el6.x86_64
  

  添加虚拟服务
  # ipvsadm -L  //查看 IPVS
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
    -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  # ipvsadm -A -t 192.168.2.50:80 -s rr//添加虚拟服务 调度算法为Round Robin
  # ipvsadm -L
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
    -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  TCP  192.168.2.50:http rr
  # ipvsadm -Ln   //- n  数字显示
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
    -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  TCP  192.168.2.50:80 rr
  //向虚拟服务器中加入节点

  # ipvsadm -a -t 192.168.2.50:80 -r 192.168.4.51:80 -m
  # ipvsadm -a -t 192.168.2.50:80 -r 192.168.4.52:80 -m
  # ipvsadm -Ln
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
    -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  TCP  192.168.2.50:80 rr
    -> 192.168.4.51:80              Masq    1      0          0
    -> 192.168.4.52:80              Masq    1      0          0
  # /etc/init.d/ipvsadm save//使配置永久生效
  ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:      [确定]
  # cat /etc/sysconfig/ipvsadm
  -A -t 192.168.2.50:80 -s rr
  -a -t 192.168.2.50:80 -r 192.168.4.51:80 -m -w 1
  -a -t 192.168.2.50:80 -r 192.168.4.52:80 -m -w 1
  

  客户端测试
  # elinks --dump http://192.168.2.50/test.html
     192.168.4.51
  # elinks --dump http://192.168.2.50/test.html
     192.168.4.52
  //客户端 轮询到不同的后端真实服务器  
  

  [root@pc50 ~]# ipvsadm -Ln --stats
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
  TCP  192.168.2.50:80                 2       10       10      846     1098
  -> 192.168.4.51:80                     1        5        5        423      549
  -> 192.168.4.52:80                     1        5        5        423      549
  模拟pc51 web服务故障:
  [root@pc51 ~]# service httpd stop
  [root@pc50 ~]# ipvsadm -Z
  

  //客户端测试
  # elinks --dump http://192.168.2.50/test.html
  192.168.4.52
  # elinks --dump http://192.168.2.50/test.html
  192.168.4.52
  

  [root@pc50 ~]# ipvsadm -Ln --stats
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
  TCP  192.168.2.50:80                 3       11       11      906     1138
  -> 192.168.4.51:80                     1        1        1         60       40
  -> 192.168.4.52:80                     2       10       10      846     1098
  

  会发现 LVS/NAT 单点故障时 并不能健康性检查
  可以 编写一个脚本 监测两台Real Server 的服务 是否正常 如果监测到故障  将对应的服务在调度服务器 停掉
  使用周期性计划任务 定时运行监测脚本 到达 健康检查的目的





运维网声明 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-659223-1-1.html 上篇帖子: LVS负载均衡群集2(DR模式) 下篇帖子: corosync+pacemaker高可用的lvs directory
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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