LVS(DR) + Keepalived 實現負載均衡高可用html
1、爲何要使 用負載均衡技術?前端
一、 系統高可用性linux
二、 系統可擴展性web
三、 負載均衡能力算法
LVS+keepalived能很好的實現以上的要求,LVS提 供負載均衡,keepalived提供健康檢查,故障轉移,提升系統的可用性!採用這樣的架構之後 很容易對現有系統進行擴展,只要在後端添加或者減小realserver,只要更改lvs的 配置文件,並能實現無縫配置變動!apache
2、LVS+Keepalived介紹vim
一、 LVS後端
LVS是一個開源的軟件,能夠實現LINUX平臺下的簡單負載均衡。LVS是Linux Virtual Server的縮寫,意思是Linux虛擬服務器。目前有三種IP負 載均衡技術(VS/NAT、VS/TUN和VS/DR);八種調度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。centos
二、 keepalivedbash
Keepalived 是運行在lvs 之上,它的主要功能是實現真實機的故障隔離及負載均衡器間的失敗 切換,提升系統的可用性。
1、環境說明
本次試驗使用的是2臺前端代理服務器(LVS) 加上 keepalived,後端爲了方便,使用apache來發佈網頁,達到測試的目的。
操做系統:centos7 64位
軟件源:阿里雲
2臺服務器(LVS一、LVS2)安裝 keepalived 和安裝LVS 來反向代理
2臺服務器(web一、web2)安裝 apache 來提供服務
服務器的防火牆和selinux所有關閉
LVS1 IP:192.168.163.158
LVS2 IP:192.168.163.161
web1 IP:192.168.163.159
web2 IP:192.168.163.160
虛擬IP: 192.168.163.200
拓撲圖以下:
2、環境安裝
首先配置web服務器
一、首先爲後端的2臺web服務器安裝apache
[root@web1 ~]# yum install -y httpd
[root@web2 ~]# yum install -y httpd
二、建立各自的網頁
[root@web1 ~]# echo "web1:192.168.163.159" > /var/www/html/index.html
[root@web2 ~]# echo "web2:192.168.163.160" > /var/www/html/index.html
三、啓動服務
[root@web1 ~]# systemctl start httpd
[root@web2 ~]# systemctl start httpd
四、編寫腳本,爲web服務器設置虛擬IP以及設置抑制ARP包
[root@web1 ~]# vim /root/VIP.sh
#!/bin/bash VIP=192.168.163.200 #這個爲你要設置的VIP case $1 in start) ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP /sbin/route add -host $VIP dev lo:0 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 >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $IP >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac
添加執行權限並執行該腳本
[root@web1 ~]# chmod a+x VIP.sh
[root@web1 ~]# bash /root/VIP.sh start
在web2上也執行該操做
在LVS服務器安裝 LVS 和 Keepalived
一、安裝軟件包
[root@LVS1 ~]# yum install -y ipvsadm keepalived
[root@LVS2 ~]# yum install -y ipvsadm keepalived
二、編輯 LVS1的 keepalived 配置文件
[root@LVS1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS vrrp_mcast_group4 224.0.100.19 } vrrp_instance VI_1 { state BACKUP interface ens32 virtual_router_id 51 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass xhk } virtual_ipaddress { 192.168.163.200 dev ens32 } } virtual_server 192.168.163.200 80 { # 定義轉移ip端口80的集羣服務 delay_loop 3 lb_algo rr lb_kind DR protocol TCP sorry_server 127.0.0.1 80 real_server 192.168.163.159 80 { # 定義集羣服務包含的RS 1 weight 1 # 權重爲1 HTTP_GET { # 定義RS1的健康狀態檢測 url { path / status_code 200 } connect_timeout 1 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.163.160 80 { # 定義集羣服務包含的RS 2 weight 1 # 權重爲1 HTTP_GET { # 定義RS2的健康狀態檢測 url { path / status_code 200 } connect_timeout 1 nb_get_retry 3 delay_before_retry 1 } } }
三、編輯 LVS2的 keepalived 配置文件
[root@LVS2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS vrrp_mcast_group4 224.0.100.19 } vrrp_instance VI_1 { state BACKUP interface ens32 virtual_router_id 51 priority 99 nopreempt advert_int 1 authentication { auth_type PASS auth_pass xhk } virtual_ipaddress { 192.168.163.200 dev ens32 } } virtual_server 192.168.163.200 80 { # 定義轉移ip端口80的集羣服務 delay_loop 3 lb_algo rr lb_kind DR protocol TCP sorry_server 127.0.0.1 80 real_server 192.168.163.159 80 { # 定義集羣服務包含的RS 1 weight 1 # 權重爲1 HTTP_GET { # 定義RS1的健康狀態檢測 url { path / status_code 200 } connect_timeout 1 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.163.160 80 { # 定義集羣服務包含的RS 2 weight 1 # 權重爲1 HTTP_GET { # 定義RS2的健康狀態檢測 url { path / status_code 200 } connect_timeout 1 nb_get_retry 3 delay_before_retry 1 } } }
2個配置文件不一樣的只是優先級的不一樣,分出MASTER,而且設置了不搶佔模式,爲的是防止業務切換國語頻繁!
四、開啓路由轉發
[root@LVS1 ~]# echo "1" /proc/sys/net/ipv4/ip_forward
[root@LVS2 ~]# echo "1" /proc/sys/net/ipv4/ip_forward
五、設置虛擬IP
[root@LVS1 ~]# ifconfig lo:0 192.168.163.200 broadcast 192.168.163.200 netmask 255.255.255.255 up
[root@LVS2 ~]# ifconfig lo:0 192.168.163.200 broadcast 192.168.163.200 netmask 255.255.255.255 up
六、啓動keepalived服務
[root@LVS1 ~]# systemctl restart keepalived
[root@LVS2 ~]# systemctl restart keepalived
3、測試環節
一、查看keepalived狀態
[root@LVS1 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2017-10-22 01:26:32 EDT; 3min 48s ago
Process: 2656 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 2657 (keepalived)
CGroup: /system.slice/keepalived.service
├─2657 /usr/sbin/keepalived -D
├─2658 /usr/sbin/keepalived -D
└─2659 /usr/sbin/keepalived -D
Oct 22 01:26:37 LVS1 Keepalived_vrrp[2659]: Sending gratuitous ARP on ens32 for 192.168.163.200
查看虛擬IP
[root@LVS1 ~]# ip addr sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.163.200/32 brd 192.168.163.200 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:5f:46:a6 brd ff:ff:ff:ff:ff:ff
inet 192.168.163.158/24 brd 192.168.163.255 scope global dynamic ens32
valid_lft 1654sec preferred_lft 1654sec
inet 192.168.163.200/32 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe5f:46a6/64 scope link
valid_lft forever preferred_lft forever
訪問網頁
[root@client ~]# curl 192.168.163.200
web1:192.168.163.159
[root@client ~]# curl 192.168.163.200
web2:192.168.163.160
將LVS1的Keepalived停掉
[root@LVS1 ~]# systemctl stop keepalived
能夠看到LVS拿到了VIP
[root@LVS2 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2017-10-22 01:26:38 EDT; 6min ago
Process: 2553 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 2555 (keepalived)
CGroup: /system.slice/keepalived.service
├─2555 /usr/sbin/keepalived -D
├─2556 /usr/sbin/keepalived -D
└─2557 /usr/sbin/keepalived -D
Oct 22 01:32:50 LVS2 Keepalived_vrrp[2557]: Sending gratuitous ARP on ens32 for 192.168.163.200
繼續訪問網頁
[root@client ~]# curl 192.168.163.200
web1:192.168.163.159
[root@client ~]# curl 192.168.163.200
web2:192.168.163.160
測試成功!!!!!!!!!!!!!!!!!!!