njsuntop 发表于 2018-12-30 06:32:12

lvs+keepalived实现web服务高可用

  lvs+keepalived实现web服务高可用

  1.系统环境:

  RHEL6:安装base;developmenttools(所有可选包)。

  四台服务器:两台负载服务器,两台真实web服务器。

  vip:192.168.50.179

  master:192.168.50.170(主负载)

  backup:192.168.50.171(备用负载)

  rs1:192.168.50.172      (真实web服务器1)

  rs2:192.168.50.173      (真实web服务器2)

  2.负载节点安装配置:

  (1)安装ipvsadm

  #ln -s/usr/src/kernels/`uname -r` /usr/src/linux
  #tar zxvfipvsadm-1.24.tar.gz
  #cdipvsadm-1.24
  #make;make install

  ipvsadm不需要做任何配置。只需确定正确安装即可。

  (2)安装配置keepalived

  我的系统环境在安装过程中提示如下信息:

http://blog.运维网.com/attachment/201104/164113719.jpg
  需要安装openssl-devel包,这个包也有几个依赖包,按提示安装即可。

  此外在安装过程中还缺少 popt-devel,libnl-devel包。直接安装。

  #tar zxvf keepalived-1.2.2.tar.gz

  #cd keepalived-1.2.2

  #./configure --prefix=/usr/local/keepalived

  #make;make install

  安装完成后看到如下提示信息说明安装正确:

http://blog.运维网.com/attachment/201104/164619394.jpg
  编辑配置文件keepalived.conf文件,下面是我的配置文件内容:

  # sed -e /^#/d /usr/local/keepalived/etc/keepalived/keepalived.conf

  ! Configuration File for keepalived
  global_defs {
  router_id LVS_DEVEL_1#此处在备用负载上改成其他值,如:LVS_DEVEL_2
  }
  vrrp_instance VI_1 {
  state MASTER         #此处在备用负载上改成 BACKUP
  interface eth0
  virtual_router_id 51
  priority 200         #此处在备用负载上的值改小一点,值越大有点权越大
  advert_int 5
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  192.168.50.179    #虚拟IP
  }
  }
  virtual_server 192.168.50.179 80 {
  delay_loop 6
  lb_algo rr
  lb_kind DR
  persistence_timeout 50#session保持时间
  protocol TCP
  real_server 192.168.50.172 80 {
  weight 100
  TCP_CHECK {
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
  check_port 80
  }
  }
  real_server 192.168.50.173 80 {
  weight 100
  TCP_CHECK {
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
  check_port 80
  }
  }
  }
  注:按照以上配置在主负载从失效状态再次上线时,会接管备用负载资源,在负载量大的应用环境不太可取,如果不想抢夺备用负载的资源,可把主负载也设置成“BACKUP”,并设置“nopreempt”。优先级要保持比备用负载高。

  #ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin

  #ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc
  #keepalived -f /etc/keepalived.conf

  echo "keepalived -f /etc/keepalived.conf" >> /etc/rc.local
  ---------------------------------------------------------------------------

  3.真实web节点配置

  (1)在真实节点安装安装好web应用环境。并分别建立两个测试页面。

  (2)在真实节点建立脚本文件

  #vi /etc/init.d/real_server.sh

  #!/bin/sh
  # chkconfig: 234572 08
  # description:Config realserver lo:0 port and apply arp patch
  VIP=192.168.50.179
  ./etc/rc.d/init.d/functions
  case $1 in
  start)
  echo "lo:0 port starting"
  echo "0">/proc/sys/net/ipv4/ip_forward
  /sbin/ifconfig lo:0 $VIP broadcast $VIPnetmask 255.255.255.255 up
  /sbin/route add -host $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
  ;;
  stop)
  echo "lo:0 port closing"
  ifconfig lo:0 down
  echo "1" > /proc/sys/net/ipv4/ip_forward
  echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
  ;;
  *)
  echo "Usage: $0 {start|stop}"
  exit 1
  esac
  #chkconfig --add real_server.sh
  #chkconfig --level 235real_server.sh on
  ----------------------------------------------------------------------------------

  4.测试
  分别停掉两台web服务器的http服务,或者down掉网络接口查看结果。
  分别停掉两台负载服务器,查看相关日志,看是否能自动接管。
  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  


页: [1]
查看完整版本: lvs+keepalived实现web服务高可用