# 拓撲以下所示
html
# 節點分佈狀況linux
LVS-dr-masternginx
eth0: 192.168.146.141
後端
LVS-dr-slavetomcat
eth0: 192.168.146.142
bash
nginx1:服務器
eth0: 192.168.146.139
ide
nginx2:oop
eth0: 192.168.146.140
測試
tomcat1:
eth0: 192.168.146.138 啓用了4個tomcat
VIP: 192.168.146.200
# 具體配置
### lvs master
### 一、安裝ipvsadm、keepalived
yum install -y keepalived ipvsadm
### 二、使用keepalived來管理lvs
腳本:
global_defs { router_id master_210 } vrrp_instance aiyou { state MASTER interface eth0 virtual_router_id 100 #這個數值 master和slave必須統一 priority 151 #這個數值決定哪臺服務器是master advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.146.200 } } virtual_server 192.168.146.200 80 { delay_loop 6 lb_algo wrr lb_kind DR # persistence_timeout 50 protocol TCP real_server 192.168.146.139 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.146.140 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
### lvs slave
### 一、安裝ipvsadm、keepalived
yum install -y keepalived ipvsadm
### 二、使用keepalived來管理lvs
global_defs { router_id slave_211 } vrrp_instance aiyou { state MASTER interface eth0 virtual_router_id 100 #這個數值 master和slave必須統一 priority 150 #這個數值決定哪臺服務器是master 這裏咱們比master數值低,因此角色是backup, advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.146.200 } } virtual_server 192.168.146.200 80 { delay_loop 6 lb_algo wrr lb_kind DR # persistence_timeout 50 protocol TCP real_server 192.168.146.139 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.146.140 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
### nginx1
### nginx上要跑個腳本
#!/bin/bash # # Script to start LVS DR real server. # description: LVS DR real server # . /etc/rc.d/init.d/functions VIP=192.168.146.200 #這裏根據須要改爲本身的VIP地址 host=`/bin/hostname` case "$1" in start) # Start LVS-DR real server on this machine. /sbin/ifconfig lo down /sbin/ifconfig lo up 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 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 ;; stop) # Stop LVS-DR real server loopback device(s). /sbin/ifconfig lo:0 down 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 ;; status) # Status of LVS-DR real server. islothere=`/sbin/ifconfig lo:0 | grep $VIP` isrothere=`netstat -rn | grep "lo:0" | grep $VIP` if [ ! "$islothere" -o ! "isrothere" ];then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR real server Running." fi ;; *) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1 ;; esac
### nginx2
### nginx上要跑個腳本
#!/bin/bash # # Script to start LVS DR real server. # description: LVS DR real server # . /etc/rc.d/init.d/functions VIP=192.168.146.200 #這裏根據須要改爲本身的VIP地址 host=`/bin/hostname` case "$1" in start) # Start LVS-DR real server on this machine. /sbin/ifconfig lo down /sbin/ifconfig lo up 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 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 ;; stop) # Stop LVS-DR real server loopback device(s). /sbin/ifconfig lo:0 down 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 ;; status) # Status of LVS-DR real server. islothere=`/sbin/ifconfig lo:0 | grep $VIP` isrothere=`netstat -rn | grep "lo:0" | grep $VIP` if [ ! "$islothere" -o ! "isrothere" ];then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR real server Running." fi ;; *) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1 ;; esac
### 後端tomcat安裝省略
採用一機多實例模式(非多虛擬主機)
### 測試
### master上驗證
[root@lvs1 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.146.200:80 wrr -> 192.168.146.139:80 Route 1 0 0 -> 192.168.146.140:80 Route 1 0 0 [root@lvs1 ~]#
### slave上驗證
[root@lvs2 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.146.200:80 wrr -> 192.168.146.139:80 Route 1 0 0 -> 192.168.146.140:80 Route 1 0 0 [root@lvs2 ~]#
參考文章:
http://www.5ilinux.com/2014/05/lvs_keepalived_nginx_tomcat.html