diaoyudao 发表于 2019-1-5 15:10:41

18.6 负载均衡集群介绍;18.7 LVS介绍;18.9 LVS NAT模式搭建(上);18.10 LVS NAT模式搭建(下)

  扩展:
  lvs 三种模式详解
  http://www.it165.net/admin/html/201401/2248.html
  lvs几种算法
  http://www.aminglinux.com/bbs/thread-7407-1-1.html
  关于arp_ignore和 arp_announce
  http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
  lvs原理相关的   http://blog.csdn.net/pi9nc/article/details/23380589
  18.6 负载均衡集群介绍
  1. 主流开源软件LVS、keepalived、haproxy、nginx等
  2. 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既可以认为是4层,也可以当做7层使用
  3. keepalived的负载均衡功能其实就是lvs
  4. lvs这种4层的负载均衡是可以分发除80外的其他端口通信的,比如MySQL的,而nginx仅仅支持http,https,mail,haproxy也支持MySQL这种
  5. 相比较来说,LVS这种4层的更稳定,能承受更多的请求,而nginx这种7层的更加灵活,能实现更多的个性化需求
  18.7 LVS介绍;
  1. LVS是由国人章文嵩开发
  2. 流行度不亚于apache的httpd,基于TCP/IP做的路由和转发,稳定性和效率很高
  3. LVS最新版本基于Linux内核2.6,有好多年不更新了
  4. LVS有三种常见的模式:NAT、DR、IP Tunnel
  5. LVS架构中有一个核心角色叫做分发器(Load balance),它用来分发用户的请求,还有诸多处理用户请求的服务器(Real Server,简称rs)
  https://s2.运维网.com/wyfs02/M00/A4/A1/wKioL1muoyjBoUZrAAIVPY95NEk964.pnghttps://s2.运维网.com/wyfs02/M00/05/F0/wKiom1muo1iDrzo2AAILAvIiWyw815.pnghttps://s5.运维网.com/wyfs02/M02/05/F0/wKiom1muo22w93MqAALLUqMFHyc009.png18.8 LVS的调度算法
  前四条算法较为重要!!!
  1. 轮询 Round-Robinrr
  2. 加权轮询 Weight Round-Robin wrr
  3. 最小连接 Least-Connection lc
  4. 加权最小连接 Weight Least-Connection wlc
  5. 基于局部性的最小连接 Locality-Based Least Connections lblc
  6. 带复制的基于局部性最小连接 Locality-Based Least Connections with Replicationlblcr
  7. 目标地址散列调度 Destination Hashing dh
  8. 源地址散列调度 Source Hashingsh
  18.9 LVS NAT模式搭建(上)
  NAT模式搭建 – 准备工作
  准备三台机器:hao1hao2hao3
  hao1机器操作:
  hao1机器作为:分发器,也叫调度器(简写为dir)
  1. 添加一个新网卡,选择仅主机模式
  ens33网卡内网:192.168.211.128(ANT模式)
  ens37网卡外网:192.168.47.128(仅主机模式)
  https://s1.运维网.com/wyfs02/M00/A4/A1/wKioL1muo23i0NlrAACdEdwStpY370.png2. 编辑ens37网卡配置文件:
  # vi /etc/sysconfig/network-scripts/ifcfg-ens37
  设定ip为(仅主机网段): 192.168.47.128
  添加内容(不用设置网关):
  NAME=ens37
  DEVICE=ens37
  ONBOOT=no
  IPADDR=192.168.47.128
  PREFIX=24
  https://s4.运维网.com/wyfs02/M01/05/F0/wKiom1muo6KR5_pKAABhSTjRBrA805.png
  3. 重启网络服务命令:(重启network.service网络服务)
  # systemctl restart network.service
  4. 激活ens37网卡
  # ifup ens37
  5. 在windows系统,ping下ens37(仅主机)外网ip:
  https://s1.运维网.com/wyfs02/M02/05/F0/wKiom1muo7jhN34pAAA6qt7s0ks065.png
  hao1   hao2   hao3机器都要执行下面关闭防火墙命令:
  1. 关闭firewalld防火墙:
  # systemctl stop firewalld
  设定开机不启动firewalld防火墙:
  #systemctl disable firewalld
  查看firewalld防火墙是否关闭?
  # iptables -nvL
  https://s1.运维网.com/wyfs02/M02/05/F0/wKiom1muo82y4G4xAAA4oDOirnI038.png2. 安装centos 6系统中的iptables防火墙工具:
  # yum install -y iptables-services
  如果上面安装慢,临时重命名epel.repo,再yum 安装
  (记得改回重命名!):
  mv /etc/yum.repos.d/epel.repo/etc/yum.repos.d/epel.repo1
  启用iptables:
  # systemctl enable iptables
  开启iptables:
  # systemctl start iptables
  清除防火墙规则:
  # iptables -F
  关闭iptables:
  # service iptables save
  查看firewalld防火墙是否关闭?
  # iptables -nvL
  https://s3.运维网.com/wyfs02/M02/A4/A1/wKioL1muo8uj7z2-AAA4oDOirnI243.png3. 临时关闭getenforce防火墙:
  # setenforce 0
  永久关闭getenforce防火墙:
  # vi /etc/selinux/config
  更改内容:
  SELINUX=disabled
  https://s5.运维网.com/wyfs02/M00/05/F0/wKiom1muo_3BRkjTAABJjznxqzw138.pnghao2   hao3机器上操作:
  ens33网卡网关改成ip段为内网段,ip为hao1内网ip(128)
  1. 更改hao2机器ens33网卡配置文件:
  # vi /etc/sysconfig/network-scripts/ifcfg-ens33
  https://s3.运维网.com/wyfs02/M02/A4/A1/wKioL1muo_qDr4auAAAQwVpCgIQ597.png
  重启网卡:
  # systemctl restart network
  2. 更改hao3机器ens33网卡配置文件:
  # vi /etc/sysconfig/network-scripts/ifcfg-ens33
  https://s2.运维网.com/wyfs02/M00/05/F0/wKiom1mupCyRO-ihAAAQ-z04_lU641.png
  重启网卡:
  # systemctl restart network
  18.10 LVS NAT模式搭建(下)
  hao1机器(dir)上操作:
  1. 在hao1机器(dir)上,安装 ipvsadm:
  # yum install -y ipvsadm
  2. 在hao1机器(dir)上,编写lvs_nat.sh脚本:
  # vim /usr/local/sbin/lvs_nat.sh
  添加内容:
  #! /bin/bash
  # director 服务器上开启路由转发功能
  echo 1 > /proc/sys/net/ipv4/ip_forward
  # 关闭icmp的重定向
  echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
  echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
  # 注意区分网卡名字,阿铭的两个网卡分别为ens33和ens37
  echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
  echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects
  # director 设置nat防火墙
  iptables -t nat -F
  iptables -t nat -X
  iptables -t nat -A POSTROUTING -s 192.168.211.0/24-j MASQUERADE
  # director设置ipvsadm
  IPVSADM='/usr/sbin/ipvsadm'
  $IPVSADM -C
  $IPVSADM -A -t 192.168.47.128:80 -s rr
  $IPVSADM -a -t 192.168.47.128:80 -r 192.168.211.129:80 -m -w 1
  $IPVSADM -a -t 192.168.47.128:80 -r 192.168.211.130:80 -m -w 1
https://s2.运维网.com/wyfs02/M02/05/F0/wKiom1mupEGgTdDlAACwywtymHQ355.png3. 执行lvs_nat.sh脚本:
  # sh /usr/local/sbin/lvs_nat.sh
  hao2机器(r)上操作:
  1. 启动nginx(yum安装的nginx):
  # systemctl start nginx
  2. 搜索nginx是否启动?
  # ps aux |grep nginx
  3. 清空index.html内容(yum安装的nginx):
  # > /usr/share/nginx/html/index.html
  4. 编辑index.html(yum安装的nginx):
  # vim /usr/share/nginx/html/index.html
  添加内容(便于和hao3区分):
  hao2
  5. 查看:
  # curl localhost
  hao3机器(r)上操作:
  1. 启动nginx(yum安装的nginx):
  # systemctl start nginx
  2. 搜索nginx是否启动?
  # ps aux |grep nginx
  3. 清空index.html内容(yum安装的nginx):
  # > /usr/share/nginx/html/index.html
  4. 编辑index.html(yum安装的nginx):
  # vim /usr/share/nginx/html/index.html
  添加内容(便于和hao2区分):
  hao3
  5. 查看:
  # curl localhost
  hao1机器(dir)上操作测试:
  1. curl访问hao1机器(dir) ens37外网ip:
  # curl 192.168.47.128



页: [1]
查看完整版本: 18.6 负载均衡集群介绍;18.7 LVS介绍;18.9 LVS NAT模式搭建(上);18.10 LVS NAT模式搭建(下)