Centos上Lvs+Keepalived高可用Dr模式配置
发布时间:2018-9-25 11:39:00 阅读次数:

  centos6.8
  我有三台服务器ip分别是172.16.3.241,172.16.3.242,172.16.3.243
  1个虚拟ip:172.16.3.240
  目的,当我访问172.16.3.240 lvs会根据规则路由到241-243   3台不同的机器
  master服务器安装:
  安装ipvsadm
  yum install ipvsadm
  安装keepalived
  yum install keepalived
  安装依赖
  yum -y install openssl-devel
  yum installncurses-devel gcc gcc-c++ make rpm-build
  将keepalived配置成系统服务
  cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  mkdir /etc/keepalived/
  cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  修改修改主keepalived配置文件(从服务器只修改router_id、state和priority)
  vi /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  acassen@firewall.loc
  failover@firewall.loc
  sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  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 {
  172.16.3.240
  }
  }
  virtual_server 172.16.3.240 80 {
  delay_loop 6
  lb_algo rr
  lb_kind DR
  nat_mask 255.255.255.0
  persistence_timeout 50
  protocol TCP
  real_server 172.16.3.242 80 {
  weight 3
  TCP_CHECK {
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
  }
  }
  real_server 172.16.3.243 80 {
  weight 3
  TCP_CHECK {
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
  }
  }
  real_server 172.16.3.241 80 {
  weight 3
  TCP_CHECK {
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
  }
  }
  }
  }
  配置ipvsadm for Master
  把一下内容保存成:lvs_server.sh,并放置在/etc/init.d目录下
  #!/bin/bash
  #把一下内容保存成:lvs_server.sh
  #并放置在/etc/init.d目录下
  #如果想启动LVS Server执行:/etc/init.d/lvs_server.sh start
  #如果想停止LVS Server执行:/etc/init.d/lvs_server.sh stop
  #如果想重启LVS Server执行:/etc/init.d/lvs_server.sh restart
  VIP=172.16.3.240 #虚拟IP,更具具体情况而变
  #有几个输入几个,与下面的配置对应,同时必须与KeepAlived.config配置对应
  RIP1=172.16.3.241 #实际的服务器IP
  RIP2=172.16.3.242 #实际的服务器IP
  RIP3=172.16.3.243 #实际的服务器IP
  . /etc/rc.d/init.d/functions # 如果提示权限不够,那么先在命令行执行: chmod 777 /etc/rc.d/init.d/functions
  case "$1" in
  start)
  echo "启动LVS服务器"
  #设置虚拟IP和同步参数
  /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.0 up
  echo "1" >/proc/sys/net/ipv4/ip_forward
  #清空 IPVS的内存数据
  /sbin/ipvsadm -C
  #开启WEB 80 端口服务,并指向RIP1和RIP2的服务器
  /sbin/ipvsadm -A -t $VIP:80 -s rr
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -g
  #运行LVS
  /sbin/ipvsadm -ln
  ;;
  stop)
  echo "关闭LVS服务器"
  echo "0" >/proc/sys/net/ipv4/ip_forward
  /sbin/ipvsadm -C
  /sbin/ifconfig eth0:0 down
  ;;
  restart)
  echo "关闭LVS服务器"
  echo "0" >/proc/sys/net/ipv4/ip_forward
  /sbin/ipvsadm -C
  /sbin/ifconfig eth0:0 down
  echo "启动LVS服务器"
  #设置虚拟IP和同步参数
  /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.0 up
  echo "1" >/proc/sys/net/ipv4/ip_forward
  #清空 IPVS的内存数据
  /sbin/ipvsadm -C
  #设置LVS
  #开启WEB 80 端口服务,并指向RIP1和RIP2的服务器
  /sbin/ipvsadm -A -t $VIP:80 -s rr
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -g
  #运行LVS
  /sbin/ipvsadm -ln
  ;;
  *)
  echo "Usage: $0 {start|stop}"
  exit 1
  esac
  /etc/init.d/lvs_server.sh使用方法
  [root@RServer2 ~]# chmod 777 /etc/init.d/lvs_server.sh
  [root@RServer2 ~]# chmod 777 /etc/rc.d/init.d/functions
  [root@RServer2 ~]# /etc/init.d/lvs_server.sh start|stop|restart
  从服务器安装配置Master的安装基本一致,只是一些参数略有变化。
  变化的参数为止如下:
  1、keepalived.conf文件中
  原:state MASTER #标示状态为MASTER 备份机为BACKUP
  state BACKUP  #标示状态改为BACKUP
  原:priority 100   #MASTER权重要高于BACKUP 比如BACKUP为99
  priority 99   #MASTER权重要高于BACKUP,MASTER为100,那么BACKUP略小于,比如99
  RealServer配置步骤
  把一下内容保存成:lvs_real_server.sh,并放置在/etc/init.d目录下
  #!/bin/bash
  #把一下内容保存成:lvs_real_server.sh
  #并放置在root目录下
  #如果想启动real Server执行:/root/lvs_real_server.sh start
  #如果想停止real Server执行:/root/lvs_real_server.sh stop
  CLUSTER_VIP=172.16.3.240 #虚拟IP,更具具体情况而变
  . /etc/rc.d/init.d/functions # 如果提示权限不够,那么先在命令行执行: chmod 777 /etc/rc.d/init.d/functions
  case "$1" in
  start)
  /sbin/ifconfig lo:0 $CLUSTER_VIP netmask 255.255.255.0 broadcast $CLUSTER_VIP
  /sbin/route add -host $CLUSTER_VIP dev 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 2>&1
  echo "真实服务器启动....."
  ;;
  stop)
  /sbin/ifconfig lo:0 down
  /sbin/route del $CLUSTER_VIP >/dev/null 2>&1
  echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
  echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
  echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
  echo "真实服务器停止....."
  ;;
  *)
  echo "Usage: $0 {start|stop}"
  exit 1
  esac
  /root/lvs_real_server.sh使用方法
  [root@RServer2 ~]# chmod 777 /root/lvs_real_server.sh
  [root@RServer2 ~]# chmod 777 /etc/rc.d/init.d/functions
  [root@RServer2 ~]# /root/lvs_real_server.sh start|stop|restart
  为了方便测试 每台机器安装httpd服务
  yum install httpd
  每台机器都创建index.html
  echo “172.16.3.241” > /var/www/html/index.html
  echo “172.16.3.242” > /var/www/html/index.html
  echo “172.16.3.243” > /var/www/html/index.html
  [root@localhost ~]# service iptables stop  #关闭防火墙
  [root@localhost ~]# setenforce 0  #临时关闭selinux
  测试
  http://1172.16.3.240 #访问一直刷新会轮训显示1172.16.3.241-243
  常用命令:
  [root@localhost ~]# ipvsadm -ln #显示集群中服务器ip信息
  [root@localhost ~]# ip addr #显示VTP绑定在哪个服务器上


秉承“为人民服务”和“雷锋精神”的经营指导思想,为您提供稳定优质的产品和服务!请联系在线客服或致电我们 4006-618-418

  • 网盾科技微信公众号
友情链接 武汉网盾科技  武汉服务器租用  电脑报  海洋网动态云  潜江人论坛  潜江资讯网  海外弹性云主机  HIFIDIY音响论坛  攻城狮健身  华夏影象中心  红隼网络技术服务