1. 程式人生 > >RHEL6 搭建LVS/NAT 負載均衡集群 案例

RHEL6 搭建LVS/NAT 負載均衡集群 案例

html pan sys 指定 hit kcon pro 後端 outb

搭建LVS/NAT案例



實驗拓撲圖

技術分享圖片



操作流程

Director Server : 192.168.4.50 pc50

安裝並啟用ipvsadm

創建虛擬服務器

向虛擬服務器上加入節點


Real Server : 192.168.4.51 pc51 192.168.4.52 pc52

配置WEB 服務器


Clinet : 192.168.2.253 pc253

連接虛擬服務器測試


具體步驟

環境準備

配置yum源

# service iptables stop //關閉防火墻

# chkconfig iptables off //關閉開機自啟

# setenforce 0 //設置SELinux 為寬松模式


網站服務器 pc51 / pc52

# yum -y install httpd

[root@pc51 ~]# echo '192.168.4.51' > /var/www/html/test.html

[root@pc52 ~]# echo "192.168.4.52" > /var/www/html/test.html

# service httpd start

# chkconfig httpd on

# yum -y install elinks

[root@pc51 ~]# elinks --dump http://localhost/test.html

192.168.4.51

[root@pc52 ~]# elinks --dump http://localhost/test.html

192.168.4.52


配置分發器 pc50

# mount /dev/cdrom /mnt/

//安裝 ipvsadm rpm 包在光盤掛載文件下的LoadBalancer目錄下

#cd /mnt/LoadBalancer/

#yum -y install ipvsadm-1.26-4.el6.x86_64.rpm

//開啟內核的路由轉發功能

# sed -i '7s/0/1/' /etc/sysctl.conf

# sed -n '7p' /etc/sysctl.conf

net.ipv4.ip_forward = 1


網站服務器 pc51 / pc52

指定網關地址 192.168.4.50

# route -n//查看路由

# route add default gw 192.168.4.50//臨時配置網關 網卡重啟後生效

//永久配置網關

# vim /etc/sysconfig/network-scripts/ifcfg-eth0

# sed -n '7p' /etc/sysconfig/network-scripts/ifcfg-eth0

GATEWAY=192.168.4.50

# ifdown eth0 ; ifup eth0 //重新加載網卡


客戶端 192.168.2.253 配置

指定網關地址 192.168.2.50 :

# vim /etc/sysconfig/network-scripts/ifcfg-eth1

# sed -n '7p' /etc/sysconfig/network-scripts/ifcfg-eth1

GATEWAY=192.168.2.50

# ifdown eth1 ; ifup eth1

# ping -c 2 192.168.4.51

PING 192.168.4.51 (192.168.4.51) 56(84) bytes of data.

64 bytes from 192.168.4.51: icmp_seq=1 ttl=63 time=0.322 ms

64 bytes from 192.168.4.51: icmp_seq=2 ttl=63 time=0.503 ms


--- 192.168.4.51 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1000ms

rtt min/avg/max/mdev = 0.322/0.412/0.503/0.092 ms

# yum -y install elinks



配置分發器 pc50

# yum -y install ipvsadm-1.26-4.el6.x86_64.rpm

# rpm -q ipvsadm

ipvsadm-1.26-4.el6.x86_64


添加虛擬服務

# ipvsadm -L //查看 IPVS

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

# ipvsadm -A -t 192.168.2.50:80 -s rr//添加虛擬服務 調度算法為Round Robin

# ipvsadm -L

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 192.168.2.50:http rr

# ipvsadm -Ln //- n 數字顯示

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 192.168.2.50:80 rr

//向虛擬服務器中加入節點

# ipvsadm -a -t 192.168.2.50:80 -r 192.168.4.51:80 -m

# ipvsadm -a -t 192.168.2.50:80 -r 192.168.4.52:80 -m

# 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.2.50:80 rr

-> 192.168.4.51:80 Masq 1 0 0

-> 192.168.4.52:80 Masq 1 0 0

# /etc/init.d/ipvsadm save//使配置永久生效

ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [確定]

# cat /etc/sysconfig/ipvsadm

-A -t 192.168.2.50:80 -s rr

-a -t 192.168.2.50:80 -r 192.168.4.51:80 -m -w 1

-a -t 192.168.2.50:80 -r 192.168.4.52:80 -m -w 1


客戶端測試

# elinks --dump http://192.168.2.50/test.html

192.168.4.51

# elinks --dump http://192.168.2.50/test.html

192.168.4.52

//客戶端 輪詢到不同的後端真實服務器


[root@pc50 ~]# 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.2.50:80 2 10 10 846 1098

-> 192.168.4.51:80 1 5 5 423 549

-> 192.168.4.52:80 1 5 5 423 549


模擬pc51 web服務故障:

[root@pc51 ~]# service httpd stop

[root@pc50 ~]# ipvsadm -Z


//客戶端測試

# elinks --dump http://192.168.2.50/test.html

192.168.4.52

# elinks --dump http://192.168.2.50/test.html

192.168.4.52


[root@pc50 ~]# 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.2.50:80 3 11 11 906 1138

-> 192.168.4.51:80 1 1 1 60 40

-> 192.168.4.52:80 2 10 10 846 1098


會發現 LVS/NAT 單點故障時 並不能健康性檢查

可以 編寫一個腳本 監測兩臺Real Server 的服務 是否正常 如果監測到故障 將對應的服務在調度服務器 停掉

使用周期性計劃任務 定時運行監測腳本 到達 健康檢查的目的


RHEL6 搭建LVS/NAT 負載均衡集群 案例