nginx + lvs (dr)

環境: html

      兩臺   centos7.1 部署 nginx  linux

            ( web1   ip 172.17.12.108 hostname:primordial nginx

               web2  ip  172.17.12.111 hostname:copy ) web

      一臺   centos 7.1 部署 lvs centos

( lvs   ip   172.17.12.110  hostname:lvs ) 瀏覽器

       vip  172.17.12.121 緩存

primordial /copy:服務器

 
 

yum install nginx -ycurl

[root@primordial ~]#echo "web1" >/usr/share/nginx/html/index.html  tcp

[root@copy ~]# echo "web2" >/usr/share/nginx/html/index.html

systemctl restart nginx

[root@copy ~]# netstat -ntlp |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2838/nginx: master

在瀏覽器中測試訪問nginx服務器IP,顯示以下:

8cb7095d-37d7-4f7c-a3f0-ba503fb3b6ec7450f937-d55d-49e1-ad1a-eec924e4e6a5

若不能正常訪問,請檢查 selinux是否爲 disabled;

                                      iptables規則是否限制

                                      firewall是否關閉

                                      80端口是否被佔用

lvs:

 
 

[root@lvs ~]# ifconfig eno16777736:0 172.17.12.121 #配置vip

[root@lvs ~]# ifconfig

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 172.17.12.110 netmask 255.255.255.0 broadcast 172.17.12.255

inet6 fe80::20c:29ff:fee9:31c7 prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:e9:31:c7 txqueuelen 1000 (Ethernet)

RX packets 5624 bytes 934642 (912.7 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 1333 bytes 127248 (124.2 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

 

eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 172.17.12.121 netmask 255.255.0.0 broadcast 172.17.255.255

ether 00:0c:29:e9:31:c7 txqueuelen 1000 (Ethernet)

 

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

inet 127.0.0.1 netmask 255.0.0.0

inet6 ::1 prefixlen 128 scopeid 0x10<host>

loop txqueuelen 0 (Local Loopback)

RX packets 12 bytes 1260 (1.2 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 12 bytes 1260 (1.2 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

 

上述配置不生效可嘗試括號中方法:

( ifconfig eno16777736:0 down

ifconfig eno16777736:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eno16777736:0 )
 
 

yum install ipvasdm -y

[root@lvs ~]# ipvsadm -A -t 172.17.12.121:80 -s rr

[root@lvs ~]# ipvsadm -a -t 172.17.12.121:80 -r 172.17.12.108 -g

[root@lvs ~]# ipvsadm -a -t 172.17.12.121:80 -r 172.17.12.111 -g

[root@lvs ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.17.12.121:80 rr -> 172.17.12.108:80 Route 1 0 0 -> 172.17.12.111:80 Route 1 0 0


echo "1" >/proc/sys/net/ipv4/ip_forward #開啓端口間轉發功能

sysctl -p  #生效

primordial /copy:

 
 

[root@primordial ~]# ifconfig lo:0 172.17.12.121 netmask 255.255.255.255 broadcast 172.17.12.121

[root@primordial ~]# ifconfig

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 172.17.12.108 netmask 255.255.255.0 broadcast 172.17.12.255

inet6 fe80::20c:29ff:feb0:5eaa prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:b0:5e:aa txqueuelen 1000 (Ethernet)

RX packets 20343 bytes 8995179 (8.5 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 6642 bytes 488054 (476.6 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

 

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

inet 127.0.0.1 netmask 255.0.0.0

inet6 ::1 prefixlen 128 scopeid 0x10<host>

loop txqueuelen 0 (Local Loopback)

RX packets 26 bytes 2684 (2.6 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 26 bytes 2684 (2.6 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

 

lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

inet 172.17.12.121 netmask 255.255.255.255

loop txqueuelen 0 (Local Loopback)

 
 

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

 
 

添加路由:

route add default gw 172.17.12.121

route add -host 172.17.12.121 dev lo:0

後記:

   後面因爲場地問題實驗環境不變,vip換成192.168.37.131,web1 ip換成192.168.37.128,web2 ip換成192.168.37.129,lvs ip換成192.168.37.130

實驗中出現的問題:

1.當配置沒有問題,瀏覽器始終訪問至同一臺web時。可清理Windows本地ARP緩存,在cmd界面輸入:arp -d 或在瀏覽器強制刷新 :Ctrl+F5

2.當在lvs服務器上curl  vip能夠實現下圖中輪詢的調度而瀏覽器訪問始終在同一臺web服務器上的狀況,緣由是lvs配置vip的虛擬網卡down掉了

d78032b0-1b40-45f7-bdfb-8cbdf60aa7cd

從新生效後lvs本機不能curl vip,瀏覽器可實現負載輪詢

f63bf720-2674-4839-9122-d483ea6ba4bc

0e402829-b76a-42db-a1ad-caebea984f36525daab3-075e-4f6d-a515-90f09f24c042

3.對於ifconfig配置的ip是臨死生效不永久的問題,可採用將配置信息寫成腳本,放在開機啓動目錄下

4.ip_forward 開啓路由轉發功能在lvs服務器上配置

5.新建集羣時,ipvsadm -A -t $vip -s scheduler [-p ]

      只要後面跟上-p選項,即表明開啓長鏈接

相關文章
相關標籤/搜索