lvs负载均衡(DR模式)

系统环境:
lvs+keepalived
centos7.5 ip:192.168.1.157 vip:192.168.1.150(主)
centos7.5 ip:192.168.1.153 vip:192.168.1.150(备)

lvs
centos7.5 ip:192.168.1.161 vip:192.168.1.150

真实主机
centos7.5 ip:192.168.1.155 vip:192.168.1.150
centos7.5 ip:192.168.1.154 vip:192.168.1.150

客户机
windown7 ip:192.168.1.156


查看版本信息命令
内核信息:uname -a (uname -r)
发行版本信息:cat /etc/redhat-release

[root@socket01 ~]# uname -a
Linux socket01 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@socket01 ~]# uname -r
3.10.0-862.el7.x86_64
[root@socket01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

1、配置真实主机:使用tomcat
配置java环境和修改tomcat主页
  1)、上传jdk解压放到/usr/local/java下
  2)、在/etc/profile.d/路径下新建文件java.sh,内容如下:
  JAVA_HOME=/usr/local/java
  CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/tools.jar
  export PATH=${PATH}:${JAVA_HOME}/bin
  3)、保存退出后验证jdk,执行java -version,显示如下:
  [root@socket01 ~]# java -version
  java version "1.8.0_112"
  Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
  Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)
  4)、修改tomcat下的index.jsp,路径如下(tomcat放在/usr/local/下)
  vim /usr/local/tomcat/webapps/ROOT/index.jsp
  搜索h1,在其内容后加上本机ip(当然可以加其他内容,主要为了方便识别) 保存退出后启动tomcat
  /usr/local/tomcat/bin/startup.sh
  5)、客户访问:http://192.168.1.155/(修改成80端口) http://192.168.1.155:8080/(默认端口)
两台真实主机配置一样

2、配置lvs负载均衡
  1)、配置路由服务器(ip:192.168.1.161)
  执行命令 lsmod|grep ip_vs 检查当前加载的内核模块,看是否存在 ip_vs 模块
  安装ipvsadm:yum -y install ipvsadm
  验证ipvsadm是否安装成功,显示信息如下,
  [root@sport01 ~]# ipvsadm
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  [root@sport01 ~]# lsmod|grep ip_vs
  ip_vs 141432 0
  nf_conntrack 133053 1 ip_vs
  libcrc32c 12644 3 xfs,ip_vs,nf_conntrack
  2)、新建lvs路由配置文件
  [root@sport01 ~]# vim /usr/local/sbin/lvs_dr.sh
  内容如下:(请根据自己实际规划好的来设置)
  #! /bin/bash
  echo 1 > /proc/sys/net/ipv4/ip_forward
  ipv=/sbin/ipvsadm
  vip=192.168.1.150
  rs1=192.168.1.155
  rs2=192.168.1.154
  ifconfig ens33:0 down
  ifconfig ens33:0 $vip broadcast $vip netmask 255.255.255.255 up
  route add -host $vip dev ens33:0
  $ipv -C
  $ipv -A -t $vip:80 -s wrr
  $ipv -a -t $vip:80 -r $rs1:80 -g -w 1
  $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
  $ipv -Ln
  执行该脚本文件:bash /usr/local/sbin/lvs_dr.sh
  说明:1、执行该文件会使用到ifconfig和route命令,需提前安装
      安装命令:yum -y install net-tools
     2、vip:虚拟ip,rs1/rs2:真实主机的ip
     3、ens33为本机的网卡,255.255.255.255为子网掩码,路由机和真实主机要一致
  3)、配置真实主机(两台配置一样)
  新建脚本文件:vim /usr/local/sbin/lvs_dr_rs.sh ,内容如下
  #! /bin/bash
  vip=192.168.1.150
  ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
  route add -host $vip lo:0
  echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
  echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  sysctl -p &>/dev/null
执行命令:bash /usr/local/sbin/lvs_dr_rs.sh

在客户机访问:http://192.168.1.150/,不断刷新,可看到页面的变换

3、lvs+keepalived,实现主备路由
当主路由服务器出现异常,自动切换到备用路由服务器,
当主路由服务器恢复正常,自动切换到主路由服务器

首页安装ipvsadm,keepalived
yum -y install ipvsadm,keepalived
查看ip_vs模块:lsmod|grep ip_vs
验证ipvsadm是否安装成功:ipvsadm
查看keepalived安装目录:rpm -ql keepalived
修改keepalived配置文件:vim /etc/keepalived/keepalived.conf,内容如下
! Configuration File for keepalived
global_defs {
  notification_email {
    root@localhost
  }
  notification_email_from root@localhost
  smtp_server localhost
  smtp_connect_timeout 30
  router_id NodeA
}
vrrp_instance VI_1 {
  state MASTER #指定A节点为主节点 备用节点上设置为BACKUP即可
  interface ens33 #绑定虚拟IP的网络接口
  virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
  priority 100 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
  advert_int 1 #组播信息发送间隔,两个节点设置必须一样
  authentication { #设置验证信息,两个节点必须一致
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
    192.168.1.150
  }
}
virtual_server 192.168.1.150 80 {
  delay_loop 6
  lb_algo rr
  lb_kind DR
  nat_mask 255.255.255.255
  persistence_timeout 0
  protocol TCP
  real_server 192.168.1.155 80 {
    weight 1
    HTTP_GET {
      url {
        path /index.jsp
        digest bc50eb0f710ec57726da87be326cd81f
      }
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
}
real_server 192.168.1.154 80 {
  weight 1
  HTTP_GET {
    url {
      path /index.jsp
      digest 39700655fed2a88b80b97512e7ae2e8d
    }
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
  }
}

备用服务器的配置文件如下:
! Configuration File for keepalived
global_defs {
  notification_email {
    root@localhost
  }
  notification_email_from root@localhost
  smtp_server localhost
  smtp_connect_timeout 30
  router_id NodeB
}
vrrp_instance VI_1 {
  state BACKUP #指定A节点为主节点 备用节点上设置为BACKUP即可
  interface ens33 #绑定虚拟IP的网络接口
  virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
  priority 99 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
  advert_int 1 #组播信息发送间隔,两个节点设置必须一样
  authentication { #设置验证信息,两个节点必须一致
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
    192.168.1.150
  }
}
virtual_server 192.168.1.150 80 {
  delay_loop 6
  lb_algo rr
  lb_kind DR
  nat_mask 255.255.255.255
  persistence_timeout 0
  protocol TCP
  real_server 192.168.1.155 80 {
  weight 1
  HTTP_GET {
    url {
      path /index.jsp
      digest bc50eb0f710ec57726da87be326cd81f
    }
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
  }
}
real_server 192.168.1.154 80 {
  weight 1
  HTTP_GET {
    url {
      path /index.jsp
      digest 39700655fed2a88b80b97512e7ae2e8d
    }
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
  }
}

注:digest算法,genhash -s ip -p 端口 -u /index.jsp,将算出来的值换掉原值

启动keepalived:
systemctl start keepalived或者service keepalived start
查看keepalived进程:ps aux|grep keepalived

客户机访问http://192.168.1.150,当把主路由服务器关掉后,自动切换到备用服务器
当主路由服务器恢复正常,又会切换到主路由服务器

 

posted on 2018-08-16 12:39  skyxia  阅读(283)  评论(0编辑  收藏  举报