1. 程式人生 > >LVS+keepalived+nginx+tomcat部署實現

LVS+keepalived+nginx+tomcat部署實現

run nbsp nor pass machine protoc keep ets ahp

拓撲如下所示

技術分享

# 節點分布情況

LVS-dr-master

eth0: 192.168.146.141


LVS-dr-slave

eth0: 192.168.146.142

nginx1:

eth0: 192.168.146.139

nginx2:

eth0: 192.168.146.140

tomcat1:

eth0: 192.168.146.138 啟用了4個tomcat

VIP: 192.168.146.200

# 具體配置

### lvs master

### 1、安裝ipvsadm、keepalived

1 yum install -y keepalived ipvsadm

### 2、使用keepalived來管理lvs

腳本:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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

### 1、安裝ipvsadm、keepalived

1 yum install -y keepalived ipvsadm

### 2、使用keepalived來管理lvs

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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上要跑個腳本

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #!/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上要跑個腳本

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #!/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上驗證

1 2 3 4 5 6 7 8 [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上驗證

1 2 3 4 5 6 7 8 [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

http://lovelace.blog.51cto.com/1028430/1550188

LVS+keepalived+nginx+tomcat部署實現