q29191 发表于 2019-1-3 11:53:56

生产环境中CentOS5.6下配置LVS

  公司内网的一个项目开发那边自己配置的LVS,出了点问题,我重装配置了,济南和东莞各一套,东莞我用的是LVS-HA,济南我用的是LVS+keepalived,下面是具体步骤:
  一、环境
  东莞
  VIP:172.28.29.71
  realserver1:172.28.29.39(BACKUP)
  realserver2:172.28.29.40(MASTER)
  realserver3:172.28.29.41
  济南
  VIP:162.28.29.75
  realserver1:162.28.29.38(MASTER)
  realserver2:162.28.29.39(BACKUP)
  realserver3:162.28.29.40
二、部署
A、东莞
1.安装需要的包(MASTER、BACKUP都安装安装)
yum -y install ipvsadm modcluster piranha system-config-cluster
2.撰写lvs.cfg配置文件(MASTER、BACKUP的内容是一样的)
# vi /etc/sysconfig/ha/lvs.cf
serial_no = 18
primary = 172.28.29.40
service = lvs
backup_active = 1
backup = 172.28.29.39
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = direct
debug_level = NONE
monitor_links = 0
syncdaemon = 0
virtual 172.28.29.71 {
   active = 1
   address = 172.28.29.71 eth0:1
   vip_nmask = 255.255.255.0
   port = 81
   send = "GET / HTTP/1.0\r\n\r\n"
   expect = "HTTP"
   use_regex = 0
   load_monitor = none
   scheduler = wrr
   protocol = tcp
   timeout = 6
   reentry = 15
   quiesce_server = 0
   server 172.28.29.39 {
         address = 172.28.29.39
         active = 1
         port = 81
         weight = 1
   }
   server 172.28.29.40 {
         address = 172.28.29.40
         active = 1
         weight = 1
         port = 81
   }
   server 172.28.29.41 {
         address = 172.28.29.41
         active = 1
         port = 81
         weight = 1
   }
}



3.撰写realserver的脚本(三台realserver都需要)
vi /etc/init.d/lvs-realserver.sh

#/bin/bash
VIP=172.28.29.71

start(){
/sbin/ifconfig eth0:1 ${VIP} broadcast ${VIP} netmask 255.255.255.0 up
/sbin/route add -host ${VIP} dev eth0:1
/sbin/sysctl -w net.ipv4.conf.lo.arp_ignore=1
/sbin/sysctl -w net.ipv4.conf.lo.arp_announce=2
/sbin/sysctl -w net.ipv4.conf.all.arp_ignore=1
/sbin/sysctl -w net.ipv4.conf.all.arp_announce=2
/sbin/sysctl -p
}

stop(){
/sbin/ifconfig eth0:1 down
/sbin/sysctl -w net.ipv4.conf.lo.arp_ignore=0
/sbin/sysctl -w net.ipv4.conf.lo.arp_announce=0
/sbin/sysctl -w net.ipv4.conf.all.arp_ignore=0
/sbin/sysctl -w net.ipv4.conf.all.arp_announce=0
}

restart(){
stop
start
}

case $1 in
start)
   start
;;
stop)
   stop
;;
restart)
   restart
;;
status)
   /sbin/ifconfig
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac


4.添加执行权限并启动realserver的脚本,加人开机启动(三台都需要)
# chmod +x/etc/init.d/lvs-realserver.sh
# sh /etc/init.d/lvs-realserver.sh
# echo "sh /etc/init.d/lvs-realserver.sh" >> /etc/rc.local

5.打开内核的路由转发功能(MASTER和BACKUP)
# vi /etc/sysctl.conf
把net.ipv4.ip_forward = 0 给为 1
执行一下: sysctl -p

6.启动LVS集群(先MASTER.后BACKUP)
/etc/init.d/pulse start


7.查看运行情况
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP172.28.29.71:hosts2-ns wrr
-> 172.28.29.41:hosts2-ns       Route   1      15410      1         
-> 172.28.29.39:hosts2-ns       Route   1      15476      1         
-> 172.28.29.40:hosts2-ns       Local   1      2          2231



B、济南
1. 安装需要的包(MASTER、BACKUP)
yum -y install ipvsadm
2.安装keepalived (MASTER、BACKUP)
# ln -sv /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux/
# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
# tar -zxvf keepalived-1.1.17.tar.gz
# cd keepalived-1.1.17
# ./configure
# make&&make install

。。。。。。
Keepalived configuration
------------------------
Keepalived version       : 1.1.17
Compiler               : gcc
Compiler flags         : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
Use VRRP Framework       : Yes
Use LinkWatch            : No
Use Debug flags          : No


# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/sbin/keepalived /usr/sbin/
# mkdir /etc/keepalived

vi /etc/keepalived/keepalived.conf


! Configuration File for keepalived

global_defs {
   notification_email {
      navyaijm@163.com
   }
   notification_email_from navyaijm@163.com
   smtp_server smtp.163.com
# smtp_connect_timeout 30
   router_id LVS_DEVEL
}

# VIP1
vrrp_instance VI_1 {
    state MASTER   备为:BACKUP
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 51
    priority 100   备为:90
    advert_int 5
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      162.28.29.75
    }
}

virtual_server 162.28.29.75 81 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 60
    protocol TCP

    real_server 162.28.29.38 81 {
      weight 100
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 81
      }
    }
    real_server 162.28.29.39 81 {
      weight 100
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 81
      }
   }
    real_server 162.28.29.40 81 {
      weight 100
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 81
      }
   }
}


3.撰写realserver脚本(三台一样)
# vi /etc/init.d/lvs-realserver.sh

#/bin/bash

VIP=162.28.29.75

start(){
/sbin/ifconfig eth0:1 ${VIP} broadcast ${VIP} netmask 255.255.255.0 up
/sbin/route add -host ${VIP} dev eth0:1
/sbin/sysctl -w net.ipv4.conf.lo.arp_ignore=1
/sbin/sysctl -w net.ipv4.conf.lo.arp_announce=2
/sbin/sysctl -w net.ipv4.conf.all.arp_ignore=1
/sbin/sysctl -w net.ipv4.conf.all.arp_announce=2
/sbin/sysctl -p
}

stop(){
/sbin/ifconfig eth0:1 down
/sbin/sysctl -w net.ipv4.conf.lo.arp_ignore=0
/sbin/sysctl -w net.ipv4.conf.lo.arp_announce=0
/sbin/sysctl -w net.ipv4.conf.all.arp_ignore=0
/sbin/sysctl -w net.ipv4.conf.all.arp_announce=0
}

restart(){
stop
start
}

case $1 in
start)
   start
;;
stop)
   stop
;;
restart)
   restart
;;
status)
   /sbin/ifconfig
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac

4.添加执行权限并启动realserver的脚本,加人开机启动(三台都需要)
# chmod +x/etc/init.d/lvs-realserver.sh
# sh /etc/init.d/lvs-realserver.sh
# echo "sh /etc/init.d/lvs-realserver.sh" >> /etc/rc.local

5.打开内核的路由转发功能(MASTER和BACKUP)
# vi /etc/sysctl.conf
把net.ipv4.ip_forward = 0 给为 1
执行一下: sysctl -p

6.启动LVS集群(先MASTER.后BACKUP)
# /etc/init.d/keepalived start
7.查看运行情况
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP162.28.29.75:81 rr persistent 60
-> 162.28.29.40:81            Route   100    1961       0         
-> 162.28.29.39:81            Route   100    1648       1         
-> 162.28.29.38:81            Local   100    0          227      















页: [1]
查看完整版本: 生产环境中CentOS5.6下配置LVS