iyth888 发表于 2015-11-20 09:16:14

CentOS 6.0 配置LVS(DR)+KeepAlived+nginx--- (2)

七、Master安装步骤:
1、安装ipvsadm 和 KeepAlived for Master
  #mkdir /soft
  #cd /soft
  # wgethttp://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
  # wgethttp://www.keepalived.org/software/keepalived-1.1.17.tar.gz
  #ln -s/usr/src/kernels/2.6.32-71.el6.i686/ /usr/src/linux
  # tar -zxvfipvsadm-1.24.tar.gz
  # cd ipvsadm-1.24
  # make;makeinstall
  # cd ..
  # tar -zxvfkeepalived-1.1.17.tar.gz
  # cd keepalived-1.1.17
  # ./configure--prefix=/usr/local/keepalived
  #make;make install
  顺利的话就这些步骤了,如果出现错误提示,那么根据具体的错误具体处理,一般可能出现的错误:
  
  1)、OpenSSL,提示可能如下
  !!! OpenSSL is not properly installed onyour system. !!!
  !!! Can not include OpenSSL headers files.
  解决方案:运行yum -y installopenssl-devel
  
  2)、提示没有gcc编译器
  解决方案:运行yum installncurses-devel gcc gcc-c++ make rpm-build
  

2、配置keepalived forMaster
  # cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
  # cp/usr/local/keepalived/sbin/keepalived /usr/sbin/
  # cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  # mkdir -p/etc/keepalived/
  # cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf
  # chmod +x/etc/init.d/keepalived
  # gedit/etc/keepalived/keepalived.conf
  或是
  # vi/etc/keepalived/keepalived.conf
  

  keepalived.conf文件如下:
  #Master服务器上的配置/etc/keepalived/keepalived.conf
  global_defs {
  notification_email {
  likx@****.com #可以多个地址
  }
  notification_email_from likx@****.com
  smtp_server 192.168.0.**
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  
  #监测ipvsadm进程状态,每3秒执行一次
  vrrp_script chk_ipvsadm{
  script "/usr/local/keepalived/chk_ipvsadm.sh"
  interval 3
  weight 3
  }
  
  vrrp_instance VI_1 {
  state MASTER #标示状态为MASTER 备份机为BACKUP
  interface eth0
  virtual_router_id 51
  priority 100   #MASTER权重要高于BACKUP 比如BACKUP为99
  advert_int 5
  mcast_src_ip 192.168.20.101 #Master服务器IP,如果是备份机请填写备份机的IP
  

  authentication {
  auth_type PASS #主从服务器验证方式
  auth_pass 1111
  }
  
  virtual_ipaddress {
  192.168.20.99 #虚拟IP
  }
  }
  
  #虚拟服务器 21端口的配置
  virtual_server 192.168.20.99 21 {
  delay_loop 10         #(每隔10秒查询realserver状态)
  lb_algo rr            #(lvs 算法)
  lb_kind DR            #(DirectRoute)
  persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
  protocol TCP         #(用TCP协议检查realserver状态)
  

  #实际服务器的IP和端口
  real_server 192.168.20.102 21 {
  weight5
  TCP_CHECK {
  connect_timeout 10
  connect_port 21
  }
  }
  

  #实际服务器的IP和端口
  real_server 192.168.20.100 21 {
  weight5
  TCP_CHECK {
  connect_timeout 10
  connect_port 21
  }
  }
  
  #实际服务器的IP和端口
  real_server 192.168.20.101 21 {
  weight5
  TCP_CHECK {
  connect_timeout 10
  connect_port 21
  }
  }
  }
  
  #虚拟服务器 80端口的配置
  virtual_server 192.168.20.99 80 {
  delay_loop 6
  lb_algo rr
  lb_kind DR
  protocol TCP
  

  real_server 192.168.20.102 80 {
  weight5
  TCP_CHECK {
  connect_timeout 10
  connect_port 80
  }
  }
  
  real_server 192.168.20.100 80 {
  weight5
  TCP_CHECK {
  connect_timeout 10
  connect_port 80
  }
  }
  
  real_server 192.168.20.101 80 {
  weight5
  TCP_CHECK {
  connect_timeout 10
  connect_port 80
  }
  }
  }
  }
  

  
  keepAlived使用方式:
  /etc/init.d/keepalived start|stop|restart
  /usr/local/keepalived/chk_ipvsadm.sh文件内容如下:
  #!/bin/bash
  #
  # author: likexi
  # description:
  # 定时查看ipvsadm是否存在,如果不存在则启动ipvsadm,
  # 如果启动失败,则停止keepalived
  #
  status=$(ps aux|grep ipvsadm | grep -v grep| grep -v bash | wc -l)
  if [ "${status}" = "0"]; then
  service ipvsadm start
  status2=$(ps aux|grep ipvsadm | grep -v grep | grep -v bash |wc -l)
  if [ "${status2}" = "0"]; then
  /etc/init.d/keepalived stop
  fi
  fi
  
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.shstart
  #如果想停止LVS Server执行:/etc/init.d/lvs_server.shstop
  #如果想重启LVS Server执行:/etc/init.d/lvs_server.shrestart
  

  VIP=192.168.20.99 #虚拟IP,更具具体情况而变
  #有几个输入几个,与下面的配置对应,同时必须与KeepAlived.config配置对应
  RIP1=192.168.20.101 #实际的服务器IP
  RIP2=192.168.20.100 #实际的服务器IP
  RIP2=192.168.20.102 #实际的服务器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.255 up   #此处的netmask不能使用其他地址

  echo "1" >/proc/sys/net/ipv4/ip_forward
  #清空 IPVS的内存数据
  /sbin/ipvsadm -C
  
  #设置LVS
  #开启FTP 21 端口服务,并指向RIP1和RIP2的服务器
  /sbin/ipvsadm -A -t $VIP:21 -s rr
  /sbin/ipvsadm -a -t $VIP:21 -r $RIP1:21 -g
  /sbin/ipvsadm -a -t $VIP:21 -r $RIP2:21 -g
  /sbin/ipvsadm -a -t $VIP:21 -r $RIP3:21 -g
  

  #开启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.255 up               #此处的netmask不能使用其他地址
  echo "1" >/proc/sys/net/ipv4/ip_forward
  

  #清空 IPVS的内存数据
  /sbin/ipvsadm -C
  

  #设置LVS
  #开启FTP 21 端口服务,并指向RIP1和RIP2的服务器
  /sbin/ipvsadm -A -t $VIP:21 -s rr
  /sbin/ipvsadm -a -t $VIP:21 -r $RIP1:21 -g
  /sbin/ipvsadm -a -t $VIP:21 -r $RIP2:21 -g
  /sbin/ipvsadm -a -t $VIP:21 -r $RIP3:21 -g
  

  #开启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使用方法
  # chmod 777/etc/init.d/lvs_server.sh
  # chmod 777/etc/rc.d/init.d/functions
  #/etc/init.d/lvs_server.sh start|stop|restart
  

  到这里为止,Master配置完成。
  

八、Backup安装步骤:
  Backup的安装和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
  原:mcast_src_ip 192.168.20.101 #Master服务器IP,如果是备份机请填写备份机的IP
  mcast_src_ip 192.168.20.100#换成BACKUP服务器IP
  
九、RealServer配置步骤:
  RealServer只需要在root目录下创建lvs_real_server.sh文件,并执行他一遍即可,lvs_real_server.sh的文件内容如下:
  #!/bin/bash
  #把一下内容保存成:lvs_real_server.sh
  #并放置在root目录下
  #如果想启动real Server执行:/root/lvs_real_server.shstart
  #如果想停止real Server执行:/root/lvs_real_server.shstop
  

  CLUSTER_VIP=192.168.20.99 #虚拟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.255 broadcast$CLUSTER_VIP            #此处的netmask不能使用其他地址

  /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使用方法
  # chmod 777/root/lvs_real_server.sh
  # chmod 777/etc/rc.d/init.d/functions
  # /root/lvs_real_server.shstart|stop|restart
  

十、配置web服务器:
  在三台服务器上分别配置nginx服务器作为lvs集群系统的web服务器。分别修改每个nginx的index.html页面,使每台服务器上的起始页面可以容易分辨。具体做法请参照网络教程。

  
十一、LVS群集系统维护命令:
1、ipvsadm维护
  
  启动:/etc/init.d/lvs_server.sh start
  停止:/etc/init.d/lvs_server.sh stop
  重启:/etc/init.d/lvs_server.sh restart
  

2、keepAlived维护
  启动:/etc/init.d/keepalived start
  停止:/etc/init.d/keepalived stop
  重启:/etc/init.d/keepalived restart
  

3、real server 维护
  启动:/root/lvs_real_server.sh start
  停止:/root/lvs_real_server.sh stop
  

十二、验证:
  1、首先依次开启每个服务器上的各服务:
  /etc/init.d/lvs_server.sh start

      /etc/init.d/keepalived start  /root/lvs_real_server.sh start
  /usr/local/nginx/sbin/nginx start            #nginx服务器启动

  2、浏览器页面输入 http://192.168.20.99
  3、关闭master、backup,看看页面每次是否都会切换。
页: [1]
查看完整版本: CentOS 6.0 配置LVS(DR)+KeepAlived+nginx--- (2)