實驗環境---ubuntu16.04html
四臺機器:10.211.55.13—55.16linux
-
具體實驗環境配置以下:web
10.211.55.102 LVS_VIPubuntu
10.211.55.13 LVS_MASTER瀏覽器
10.211.55.14 LVS_SALVEtomcat
10.211.55.15 WEB1服務器
10.211.55.16 WEB2app
安裝LVS與Keepalived
sudo apt install ipvsadm
打開ip forward功能:文件/etc/sysctl.conf負載均衡
打開後修改裏面「net.ipv4.ip_forward = 1」webapp
執行以下命令使設置生效:
sudo sysctl -p
sudo apt install ipvsadm
打開ip forward功能:文件/etc/sysctl.conf
打開後修改裏面「net.ipv4.ip_forward = 1」
執行以下命令使設置生效:
sysctl -p
接下來開啓keepalived服務:LVS MASTER. LVS SLAVE都要操做以下:55.13 55.14
sudo systemctl start keepalived.service
查看keepalived進程:
[root@lvs keepalived-1.2.7]# sudo ps aux|grep keepalived
root 18485 0.0 0.0 42164 972 ? Ss 11:43 0:00 keepalived -D
root 18486 0.0 0.2 46464 2376 ? S 11:43 0:00 keepalived -D
root 18487 0.0 0.1 46340 1740 ? S 11:43 0:00 keepalived -D
去master查看虛擬ip是否加上:注意若是虛擬ip沒有自動加上,請重啓下keepalived服務。虛擬ip :55.102(自定義)
parallels@parallels-vm:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:1c:42:41:65:f6 brd ff:ff:ff:ff:ff:ff
inet 10.211.55.13/24 brd 10.211.55.255 scope global dynamic enp0s5
valid_lft 1117sec preferred_lft 1117sec
inet 10.211.55.102/32 scope global enp0s5
valid_lft forever preferred_lft forever
inet6 fdb2:2c26:f4e4:0:2d91:9984:fe45:63ca/64 scope global temporary dynamic
valid_lft 602038sec preferred_lft 83527sec
inet6 fdb2:2c26:f4e4:0:e00e:d6:1c91:3bea/64 scope global mngtmpaddr noprefixroute dynamic
valid_lft 2591853sec preferred_lft 604653sec
inet6 fe80::5806:a54:e63b:90c0/64 scope link
valid_lft forever preferred_lft forever
上述顯示55.102虛擬ip加上了!
接下里配置部署兩臺web服務器:
配置部署10.211.55.15 web1:
配置虛擬IP啓動腳本:
parallels@parallels-vm:~$ pwd
/home/parallels
parallels@parallels-vm:~$ cat realserver.sh
#Written by :NetSeek http://www.linuxtone.org
VIP=10.211.55.102
. /lib/init/vars.sh
. /lib/lsb/init-functions
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/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 >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $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 "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
配置部署tomcat:
sudo apt-get install tomcat7 -y
啓動tomcat服務:
sudo systemctl start tomcat7.service
爲了測試負載均衡,咱們將這個頁面改下,以更好的標識這個網頁是本服務器的
Tomcat7安裝目錄位於/usr/share/tomcat7,因此咱們要編輯tomcat下的webapps/ROOT/index.html這個文件。
parallels@parallels-vm:/usr/share/tomcat7-root/default_root$ pwd
/usr/share/tomcat7-root/default_root
刪掉此目錄的index.html文件:sudo rm -f index.html
從新建立此目錄的index.html文件,內容以下:
Sudo touch /usr/share/tomcat7-root/default_root/index.html
parallels@parallels-vm:/usr/share/tomcat7-root/default_root$ cat index.html
web1 10.211.55.15
啓動虛擬IP的腳本:
sudo sh realserver.sh start
去LVS_MASTER服務器的終端查看下ipvsadm,查看WEB1(55.15)服務器加到集羣裏:
sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.211.55.102:8080 rr persistent 50
-> 10.211.55.15:8080 Route 1 0 0
上述可見:55.15已經加進來了!
此時咱們訪問網頁http://10.211.55.15:8080,出現界面顯示web1 10.211.55.15;或者使用命令行測試轉發狀況均可以查看。
配置部署10.211.55.16 web2:
配置虛擬IP啓動腳本:
parallels@parallels-vm:~$ pwd
/home/parallels
parallels@parallels-vm:~$ cat realserver.sh
#Written by :NetSeek http://www.linuxtone.org
VIP=10.211.55.102
. /lib/init/vars.sh
. /lib/lsb/init-functions
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/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 >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $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 "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
配置部署tomcat:
sudo apt-get install tomcat7 -y
啓動tomcat服務:
sudo systemctl start tomcat7.service
爲了測試負載均衡,咱們將這個頁面改下,以更好的標識這個網頁是本服務器的
Tomcat7安裝目錄位於/usr/share/tomcat7,因此咱們要編輯tomcat下的webapps/ROOT/index.html這個文件。
parallels@parallels-vm:/usr/share/tomcat7-root/default_root$ pwd
/usr/share/tomcat7-root/default_root
刪掉此目錄的index.html文件:sudo rm -f index.html
從新建立此目錄的index.html文件,內容以下:
Sudo touch /usr/share/tomcat7-root/default_root/index.html
parallels@parallels-vm:/usr/share/tomcat7-root/default_root$ cat index.html
Web2 10.211.55.16
啓動虛擬IP的腳本:
sudo sh realserver.sh start
去LVS_MASTER服務器的終端查看下ipvsadm,查看WEB2服務器加到集羣裏:
sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.211.55.102:8080 rr persistent 50
-> 10.211.55.15:8080 Route 1 0 0
-> 10.211.55.16:8080 Route 1 0 0
上述55.15 55.16兩臺web都已經加進來了
最後客戶端測試:
瀏覽器輸入:vip地址即10.211.55.102:8080
停掉其中一臺keepalived,服務正常訪問!