1. 程式人生 > >Centos7-LVS叢集-NAT配置

Centos7-LVS叢集-NAT配置

1.準備環境。

1)克隆 四臺虛擬機器 pc10, lvs20, web30,web40.

 分別設定好IP 192.168.4.10,192.168.4.20(192.168.2.20),192.168.2.30,192.168.2.40.

  10位客戶機,20為轉發和排程機,需設定兩張網絡卡eth0和eth1,30和40為web伺服器。

2)每臺機器上的firewalld和selinux都關掉。

3)web30和40上都安裝httpd 並開啟服務:

 #yum -y install httpd

#systemctl start httpd

4)lvs20上開啟IP轉發:

# vim /etc/sysctl.conf

將以下內容新增進去

#net.ipv4.ip_forward = 1

安裝ipvsadm:

#yum -y install ipvsadm

然後啟動ipvsadm

#systemctl start ipvsadm

如果啟動不成功可以執行以下命令:

#ipvsadm --save > /etc/sysconfig/ipvsadm

5)web30和web40上指定閘道器為192.168.2.20

route add default gw  192.168.2.20

pc10上指定閘道器為192.168.4.20

2. 配置ipvsadm

在lvs20上操作;

1)新增虛擬服務

[[email protected] ~]# ipvsadm -A -t 192.168.4.20:80 -s rr
[[email protected] ~]# 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.4.20:80 rr


2)新增 realserver  web30&40

[[email protected] ~]# ipvsadm -a -t 192.168.4.20:80 -r 192.168.2.30:80 -m
[[email protected] ~]# 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.4.20:80 rr
  -> 192.168.2.30:80              Masq    1      0          0     

[[email protected] ~]# ipvsadm -a -t 192.168.4.20:80 -r 192.168.2.40:80 -m

[[email protected] ~]# 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.4.20:80 rr
  -> 192.168.2.30:80              Masq    1      0          0         
  -> 192.168.2.40:80              Masq    1      0          0  

3)儲存策略

[[email protected] ~]# ipvsadm-save
-A -t 192.168.4.20:http -s rr
-a -t 192.168.4.20:http -r 192.168.2.30:http -m -w 1
-a -t 192.168.4.20:http -r 192.168.2.40:http -m -w 1

3.測試 ,pc10上進行測試

安裝elinks

[[email protected] ~]# yum -y install elinks

使用elinks測試 lvs20分發效果

[[email protected] ~]# elinks --dump http://192.168.4.20/test.html
   web40
[[email protected] ~]# elinks --dump http://192.168.4.20/test.html
   web30
 

可以在分發機器上檢視連線資訊彙總

[[email protected] ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.4.20:80                     8       48       33     3800     4332
  -> 192.168.2.30:80                     4       24       17     1900     2192
  -> 192.168.2.40:80                     4       24       16     1900     2140
 

改權重值: -e -w

[[email protected] ~]# ipvsadm -e -t 192.168.4.20:80 -r 192.168.2.40:80 -w 3
[[email protected] ~]# 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.4.20:80 rr
  -> 192.168.2.30:80              Masq    1      0          0         
  -> 192.168.2.40:80              Route   3      0          0   

改排程演算法: -E   -m NAT模式  -g DR模式   -i TUN模式

[[email protected] ~]# ipvsadm -E -t 192.168.4.20:80 -s wrr

刪除realserver: -d

[[email protected] ~]# ipvsadm -d -t 192.168.4.20:80 -r 192.168.2.30:80
[[email protected] ~]# 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.4.20:80 wrr
  -> 192.168.2.40:80              Route   3      0          0  

清空虛擬服務:  -C

[[email protected] ~]# ipvsadm -C
[[email protected] ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn