一、 使用LVS負載均衡用戶請求到後端web服務器,而且實現健康狀態檢查html
二、 使用keepalived高可用LVS,避免LVS單點故障web
三、 集羣中分別在LK-01和LK-02運行一個VIP地址,實現LVS雙主算法
四、 用戶經過DNS輪訓的方式實現訪問集羣的負載均衡(不演示)vim
IP地址後端 |
功能描述bash |
|
LK-01服務器 |
172.16.4.100負載均衡 |
調度用戶請求到後端web服務器,而且和LK-02互爲備份curl |
LK-02ide |
172.16.4.101 |
調度用戶請求到後端web服務器,而且和LK-01互爲備份 |
WEB-01 |
172.16.4.102 |
提供web服務 |
WEB-02 |
172.16.4.103 |
提供web服務 |
DNS |
172.16.4.10 |
實現DNS輪訓解析用戶請求域名地址 |
VIP1 |
172.16.4.1 |
用戶訪問集羣的入口地址,可能在LK-01,也可能在LK-02 |
VIP2 |
172.16.4.2 |
用戶訪問集羣的入口地址,可能在LK-01,也可能在LK-02 |
Web服務器的配置極其簡單,只須要提供測試頁面啓動web服務便可,配置以下:
Web-01配置
[root@WEB-01 ~]# echo "web-01" >/var/www/html/index.html [root@WEB-01 ~]# service httpd start
Web-02配置
[root@WEB-02 ~]# echo "web-02" >/var/www/html/index.html [root@WEB-02 ~]# service httpd start
LVS訪問後端web服務器,驗證web服務提供成功
[root@LK-01 ~]# curl 172.16.4.102 web-01 [root@LK-01 ~]# curl 172.16.4.103 web-02
出現設置的頁面,就說明web服務是正常
WEB服務器使用腳本進行配置LVS-DR模式客戶端環境
因爲是雙主模式,客戶端須要啓用兩個子接口配置兩個VIP地址。
#!/bin/bash # vip1=172.16.4.1 vip2=172.16.4.2 interface1="lo:0" interface2="lo:1" case $1 in start) echo 1> /proc/sys/net/ipv4/conf/all/arp_ignore echo 1> /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2> /proc/sys/net/ipv4/conf/all/arp_announce echo 2> /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig$interface1 $vip1 broadcast $vip1 netmask 255.255.255.255 up ifconfig$interface2 $vip2 broadcast $vip2 netmask 255.255.255.255 up route add-host $vip1 dev $interface1 route add-host $vip2 dev $interface2 ;; stop) echo 0 >/proc/sys/net/ipv4/conf/all/arp_ignore echo 0 >/proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 >/proc/sys/net/ipv4/conf/all/arp_announce echo 0 >/proc/sys/net/ipv4/conf/lo/arp_announce ifconfig$interface1 down ifconfig$interface2 down ;; status) if ifconfiglo:0 |grep $vip1 &> /dev/null; then echo"ipvs1 is running." else echo"ipvs1 is stopped." fi if ifconfiglo:1 |grep $vip2 &> /dev/null; then echo"ipvs2 is running." else echo"ipvs2 is stopped." fi ;; *) echo"Usage: `basename $0` {start|stop|status}" exit 1 esac
Keepalived安裝
Centos6.6已經收錄了keepalived,直接使用yum安裝便可
[root@LK-01 ~]# yum -y install keepalived [root@LK-02 ~]# yum -y install keepalived
HK-01節點配置
[root@LK-01 ~]# vim /etc/keepalived/keepalived.conf global_defs { router_idLVS_DEVEL } vrrp_script chk_mt_down { script"[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #定義一個腳本,判斷down文件是否存在,存在就退出執行,不存在繼續執行 interval 1 #1秒檢測一次 weight -5 #若是down文件存在,VRRP實例優先級減5 } vrrp_instance VI_1 { #定義VRRP熱備實例 state MASTER #MASTER表示主節點 interfaceeth 0 #承載VIP的物理接口 virtual_router_id 51 #虛擬路由器的ID號 priority 100 #優先級 advert_int 1 #通知間隔秒數(心跳頻率) authentication { #設置主備節點直接的認證信息 auth_type PASS # auth_pass asdfgh # } virtual_ipaddress { #設置集羣中的VIP地址 172.16.4.1/32 brd 172.16.4.1 dev eth0 label eth0:0 } track_script { chk_mt_down #調用上面定義的腳本,若是這裏沒有調用,那麼上面定義的腳本是沒法生效的 } } vrrp_instance VI_2 { #定義實例爲HK-02的備份節點 state BACKUP interfaceeth 0 virtual_router_id 52 priority 99 #設置優先級,低於主服務器 advert_int 1 authentication { auth_type PASS auth_pass qwerty } virtual_ipaddress { 172.16.4.2 } track_script { chk_mt_down } } virtual_server 172.16.4.1 80 { #定義一個VIP地址和端口 delay_loop 6 lb_algo rr #輪訓算法 lb_kind DR #調度類型 nat_mask 255.255.0.0 # persistence_timeout 50 #設置長鏈接時長,單位秒 protocolTCP real_server 172.16.4.102 80 { #定義後端RS服務器的相關信息 weight 1 #設置權重 HTTP_GET { #設置健康狀態檢測方式 url { #健康檢查判斷後端服務器首頁是否訪問正常(返回200狀態碼),若是正常則認爲服務器正常 path /index.html status 200 } connect_timeout 3 #鏈接超時 nb_get_retry 3 #重試次數 delay_before_retry 1 #重試時間間隔 } } real_server 172.16.4.103 80 { weight 1 HTTP_GET { url { path /index.html status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 1 } } } virtual_server 172.16.4.2 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.0.0 # persistence_timeout 50 protocol TCP real_server 172.16.4.102 80 { weight 1 HTTP_GET { url { path /index.html status 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 1 } } real_server 172.16.4.103 80 { weight 1 HTTP_GET { url { path /index.html status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 1 } } }
LK-02節點設置
LK-02節點設置中的參數在LK-01節點都有註釋
[root@LK-02 ~]# vim /etc/keepalived/keepalived.conf global_defs { router_idLVS_DEVEL } vrrp_script chk_mt_down { script"[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 weight -5 } vrrp_instance VI_1 { state BACKUP interfaceeth 0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass asdfgh } virtual_ipaddress { 172.16.4.1/32 brd 172.16.4.1 dev eth0 label eth0:0 } track_script { chk_mt_down } } vrrp_instance VI_2 { state MASTER interfaceeth 0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass qwerty } virtual_ipaddress { 172.16.4.2 } track_script{ chk_mt_down } } virtual_server 172.16.4.1 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.0.0 # persistence_timeout 50 protocol TCP real_server 172.16.4.102 80 { weight 1 HTTP_GET{ url { path /index.html status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 1 } } real_server 172.16.4.103 80 { weight 1 HTTP_GET{ url { path /index.html status 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 1 } } } virtual_server 172.16.4.2 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.0.0 # persistence_timeout 50 protocol TCP real_server 172.16.4.102 80 { weight 1 HTTP_GET{ url { path /index.html status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 1 } } real_server 172.16.4.103 80 { weight 1 HTTP_GET{ url { path /index.html status 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 1 } } }
設置完成啓動keepalived
[root@LK-01 ~]# service keepalived start [root@LK-02 ~]# service keepalived start
LK-01查看,VIP地址啓動正常,並且ipvs命令自動設置成功
[root@LK-01 ~]# ip addr show dev eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:22:c5:c2 brd ff:ff:ff:ff:ff:ff inet172.16.4.100/16 brd 172.16.255.255 scope global eth0 inet172.16.4.1/32 brd 172.16.4.1 scope global eth0:0 inet6fe80::20c:29ff:fe22:c5c2/64 scope link valid_lft forever preferred_lft forever [root@LK-01 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->RemoteAddress:Port ForwardWeight ActiveConn InActConn TCP 172.16.4.1:80 rr ->172.16.4.102:80 Route 1 0 0 ->172.16.4.103:80 Route 1 0 0 TCP 172.16.4.2:80 rr ->172.16.4.102:80 Route 1 0 0 ->172.16.4.103:80 Route 1 0 0
LK-02查看,VIP和ipvs策略都有
[root@LK-02 ~]# ip addr show dev eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:f1:dd:b2 brd ff:ff:ff:ff:ff:ff inet172.16.4.101/16 brd 172.16.255.255 scope global eth0 inet172.16.4.2/32 scope global eth0 inet6fe80::20c:29ff:fef1:ddb2/64 scope link valid_lft forever preferred_lft forever [root@LK-02 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->RemoteAddress:Port ForwardWeight ActiveConn InActConn TCP 172.16.4.1:80 rr ->172.16.4.102:80 Route 1 0 0 ->172.16.4.103:80 Route 1 0 0 TCP 172.16.4.2:80 rr ->172.16.4.102:80 Route 1 0 0 ->172.16.4.103:80 Route 1 0 0
不管訪問vip1仍是vip2都已經實現了負載均衡功能
[root@localhost ~]# curl 172.16.4.1 web-02 [root@localhost ~]# curl 172.16.4.1 web-01 [root@localhost ~]# curl 172.16.4.1 web-02 [root@localhost ~]# curl 172.16.4.1 web-01 [root@localhost ~]# curl 172.16.4.2 web-02 [root@localhost ~]# curl 172.16.4.2 web-01 [root@localhost ~]# curl 172.16.4.2 web-02 [root@localhost ~]# curl 172.16.4.2 web-01
使用down文件關閉LK-02節點
[root@LK-02 ~]# touch /etc/keepalived/down
查看LK-01節點的VIP地址,發現VIP1和VIP2都已經在LK-01節點啓動了
[root@LK-01 ~]# ip addr show dev eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:22:c5:c2 brd ff:ff:ff:ff:ff:ff inet172.16.4.100/16 brd 172.16.255.255 scope global eth0 inet172.16.4.1/32 brd 172.16.4.1 scope global eth0:0 inet172.16.4.2/32 scope global eth0 inet6fe80::20c:29ff:fe22:c5c2/64 scope link valid_lft forever preferred_lft forever
集羣訪問正常
[root@localhost ~]# curl 172.16.4.1 web-02 [root@localhost ~]# curl 172.16.4.1 web-01 [root@localhost ~]# curl 172.16.4.2 web-02 [root@localhost ~]# curl 172.16.4.2 web-01
手動關閉web-02節點
[root@WEB-02 ~]# service httpd stop
查看集羣規則設置,已經自動移除了web-02節點
[root@LK-01 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->RemoteAddress:Port ForwardWeight ActiveConn InActConn TCP 172.16.4.1:80 rr ->172.16.4.102:80 Route 1 0 1 TCP 172.16.4.2:80 rr ->172.16.4.102:80 Route 1 0 1 [root@LK-02 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->RemoteAddress:Port ForwardWeight ActiveConn InActConn TCP 172.16.4.1:80 rr ->172.16.4.102:80 Route 1 0 0 TCP 172.16.4.2:80 rr ->172.16.4.102:80 Route 1 0 0