主機環境 redhat6.5 64位html
實驗環境 服務端1 ip172.25.25.113 主機名:server3.example.comvim
服務端2 ip 172.25.25.114 主機名:server4.example.com 後端
調度端2 ip 172.25.25.112 主機名:server2.example.com服務器
調度端1 ip 172.25.25.111 主機名:server1.example.comtcp
防火牆狀態:關閉ide
虛擬ip(vip): 172.25.25.200/24工具
1.LVS之DR的配置及測試oop
1.添加vip、將調度策略寫進內核(調度器端)測試
1.添加vipspa
[root@server2 ~]# ip addr add 172.25.25.200/24 dev eth0 #添加一個vip
[root@server2 ~]# ip addr show #查看
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueuestate UNKNOWN
link/loopback 00:00:00:00:00:00brd 00:00:00:00:00:00
inet 127.0.0.1/8scope host lo
inet6 ::1/128 scopehost
valid_lft foreverpreferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscpfifo_fast state UP qlen 1000
link/ether 52:54:00:85:1a:3bbrd ff:ff:ff:ff:ff:ff
inet 172.25.25.112/24brd 172.25.25.255 scope global eth0
inet 172.25.25.200/24scope global secondary eth0 #添加成功
inet6fe80::5054:ff:fe85:1a3b/64 scope link
valid_lft foreverpreferred_lft forever
2.用ipvsadm工具將策略寫進內核
[root@server2 ~]# yum install ipvsadm -y #安裝ipvsadm
[root@server2 ~]# ipvsadm -A -t 172.25.25.200:80 -s rr #寫策略,添加httpd服務,機制輪叫
[root@server2 ~]# ipvsadm -a -t 172.25.25.200:80 -r172.25.25.113:80 -g #添加服務器
[root@server2 ~]# ipvsadm -a -t 172.25.25.200:80 -r172.25.25.114:80 -g
[root@server2 ~]# ipvsadm -ln #查看,添加成功
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.25.25.200:80 rr
->172.25.25.113:80 Route 1 0 0
->172.25.25.114:80 Route 1 0 0
2.安裝httpd、添加虛擬ip、添加arp火牆策略(服務器端)
1.寫測試頁、開啓httpd
[root@server3 ~]# yum install -y httpd #安裝httpd
[root@server3 ~]# vim /var/www/html/index.html #寫測試頁
server3.example.com
[root@server3 ~]# /etc/init.d/httpd start #開啓httpd
Starting httpd: [ OK ]
2.用arptables工具添加火牆策略
[root@server3 ~]# yum install arptables_jf -y #安裝arptables_jf工具
[root@server3 ~]# ip addr add 172.25.25.200/24 dev eth0 #添加虛擬ip
#添加arp火牆策略(禁止從172.25.25.200進來的訪問,容許從72.25.25.200出去的訪問且經出去的ip變成本身的ip172.25.25.113)
[root@server3 ~]# arptables -A IN -d 172.25.25.200 -j DROP
[root@server3 ~]# arptables -A OUT -s 172.25.25.200 -j mangle--mangle-ip-s 172.25.25.113
[root@server3 ~]# /etc/init.d/arptables_jf save #保存
Saving current rules to /etc/sysconfig/arptables: [ OK ]
[root@server3 ~]# arptables -L #查看
Chain IN (policy ACCEPT)
target source-ip destination-ip source-hw destination-hw hlen op hrd pro
DROP anywhere 172.25.25.200 anywhere anywhere any any any any
Chain OUT (policy ACCEPT)
target source-ip destination-ip source-hw destination-hw hlen op hrd pro
mangle 172.25.25.200 anywhere anywhere anywhere any any any any --mangle-ip-s server3.example.com
Chain FORWARD (policy ACCEPT)
target source-ip destination-ip source-hw destination-hw hlen op hrd pro
3.測試
#剛開始是服務端1
#刷新以後,服務端2
[root@server3 ~]# /etc/init.d/httpd stop #將服務端1的httpd停掉
Stopping httpd: [ OK ]
#服務在服務端2,刷新也沒變
爲了不單點故障,要將DR添加到高可用集羣(HA)中,下面介紹的是添加到HA的heartbeat中。因爲heartheat對後端沒有健康檢查,這就須要藉助ldirectord對heartbeat進行後端檢查。即將DR添加到ldirectord中,再將ldirectord服務添加到heartbeat中,前面博文中已經有heartdbeat的安裝和測試,能夠參考前面的博文。
2.將DR添加到ldirectord服務中,再將ldirectord服務添加到高可用集羣(HA)的heartbeat中、測試
1.配置ldirectord、測試(調度端)
#在管理端1和管理端2安裝、配置好heartbeat的狀況下
1.配置ldirectord、查看策略是否寫進內核
[root@server2 yum.repos.d]# cd/usr/share/doc/ldirectord-3.9.5/
[root@server2 ldirectord-3.9.5]# ls
COPYING ldirectord.cf
[root@server2 ldirectord-3.9.5]# cp ldirectord.cf/etc/ha.d/ #複製
[root@server2 ldirectord-3.9.5]# cd /etc/ha.d/
[root@server2 ha.d]# vim ldirectord.cf #進入配置文件
25virtual=172.25.25.200:80 #虛擬ip
26 real=172.25.25.113:80 gate #真正的服務端
27 real=172.25.25.114:80 gate #同上
28 fallback=127.0.0.1:80 gate #當全部服務端都出故障以後,使用本地迴環
29 service=http #服務httpd
30 scheduler=rr #機制輪叫
31 #persistent=600
32 #netmask=255.255.255.255
33 protocol=tcp #協議
34 checktype=negotiate
35 checkport=80 端口
36 request="index.html" #服務的測試頁名稱
37 # receive="Test Page"
38 # virtualhost=www.x.y.z
[root@server2 ha.d]# /etc/init.d/ipvsadm stop #停掉ipvsadm
ipvsadm: Clearing the current IPVS table: [ OK ]
ipvsadm: Unloading modules: [ OK ]
[root@server2 ha.d]# ipvsadm -L #查看,沒有策略
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
[root@server2 ha.d]# /etc/init.d/ldirectord restart #開啓ldirectord
Restarting ldirectord... success
[root@server2 ha.d]# ipvsadm -L #查看
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.25.25.200:httprr
->server3.example.com:http Route 1 0 0
->172.25.25.114:http Route 1 0 0
2.測試
#剛開始是服務端1
#刷新以後,服務端2
#將停掉服務端1和服務端2的httpd,測試
[root@server3 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
[root@sever4 yum.repos.d]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
#本地迴環
2.將ldirectord服務添加到heartbeat中
[root@server2 ha.d]# vim haresources
150 server1.example.com IPaddr::172.25.25.200/24/eth0ldirectord httpd #添加虛擬ip,調度,httpd服務
[root@server2 ha.d]# /etc/init.d/ldirectord stop #將ldirectord服中止(不能手動開啓)
Stopping ldirectord... Success
[root@server2 ha.d]# ip addr del 172.25.25.200/24 dev #將vip刪掉
[root@server2 ha.d]# /etc/init.d/heartbeat start #開啓heartbeat
Starting High-Availability services: INFO: Resource is stopped
Done.
[root@server2 ha.d]# scp haresources 1ldirectord.cf72.25.25.111:/etc/ha.d/ #將修改過的文件傳給服務端1
root@172.25.25.111's password:
haresources 100%5972 5.8KB/s 00:00
ldirectord.cf 100%8281 8.1KB/s 00:00
[root@server1 ha.d]# /etc/init.d/heartbeat start #開啓服務端1的heartbeat
Starting High-Availability services: INFO: Resource is stopped
Done.
3.總體測試
#將服務開啓以後,服務在服務端1(主)
[root@server1 ha.d]# ip addr show #查看ip
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueuestate UNKNOWN
link/loopback00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8scope host lo
inet6 ::1/128 scopehost
valid_lft foreverpreferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscpfifo_fast state UP qlen 1000
link/ether 52:54:00:ec:8b:36brd ff:ff:ff:ff:ff:ff
inet 172.25.25.111/24brd 172.25.25.255 scope global eth0
inet 172.25.25.200/24brd 172.25.25.255 scope global secondary eth0 #虛擬ip成功
inet6fe80::5054:ff:feec:8b36/64 scope link
valid_lft foreverpreferred_lft forever
[root@server1 ha.d]# /etc/init.d/heartbeat stop #若將服務端1的heartbeat停掉
Stopping High-Availability services: Done.
#測試,服務到了服務端2(備)
[root@server2 ha.d]# ip addr show #查看ip,虛擬ip到了服務端2
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueuestate UNKNOWN
link/loopback00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8scope host lo
inet6 ::1/128 scopehost
valid_lft foreverpreferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscpfifo_fast state UP qlen 1000
link/ether52:54:00:85:1a:3b brd ff:ff:ff:ff:ff:ff
inet 172.25.25.112/24brd 172.25.25.255 scope global eth0
inet 172.25.25.200/24brd 172.25.25.255 scope global secondary eth0#虛擬ip
inet6fe80::5054:ff:fe85:1a3b/64 scope link
valid_lft foreverpreferred_lft forever
[root@server1 ha.d]# /etc/init.d/heartbeat start #將服務端1的heartbeat開啓
Starting High-Availability services: INFO: Resource is stopped
Done.
#測試,服務自動回到了服務端1(主)
[root@server1 ha.d]# ip addr show #查看ip,虛擬ip自動回到到了服務端1
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueuestate UNKNOWN
link/loopback00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8scope host lo
inet6 ::1/128 scopehost
valid_lft foreverpreferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscpfifo_fast state UP qlen 1000
link/ether52:54:00:ec:8b:36 brd ff:ff:ff:ff:ff:ff
inet 172.25.25.111/24brd 172.25.25.255 scope global eth0
inet 172.25.25.200/24brd 172.25.25.255 scope global secondary eth0
inet6fe80::5054:ff:feec:8b36/64 scope link
valid_lft foreverpreferred_lft forever