LVS+keepalived負載均衡
背景:
隨著你的網站業務量的增長你網站的伺服器壓力越來越大?需要負載均衡方案!商業的硬體如F5又太貴,你們又是創業型互聯公司如何有效節約成本,節省不必要的浪費?同時實現商業硬體一樣的高效能高可用的功能?有什麼好的負載均衡可伸張可擴充套件的方案嗎?答案是肯定的!有!我們利用 LVS+Keepalived基於完整開源軟體的架構可以為你提供一個負載均衡及高可用的伺服器。
LVS+Keepalived 介紹
LVS
LVS是Linux Virtual Server的簡寫,意即Linux虛擬伺服器,是一個虛擬的伺服器叢集系統。本專案在1998年5月由章文嵩博士成立,是中國國內最早出現的自由軟體專案之一.目前有三種IP負載均衡技術(VS/NAT、VS/TUN和VS/DR);
八種排程演算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。
Keepalvied
Keepalived在這裡主要用作RealServer的健康狀態檢查以及LoadBalance主機和BackUP主機之間failover的實現
二. 網站負載均衡構架圖
IP資訊列表:
名稱 IP
LVS-DR-Master 61.164.122.6
LVS-DR-BACKUP 61.164.122.7
LVS-DR-VIP 61.164.122.8
WEB1-Realserver 61.164.122.9
WEB2-Realserver 61.164.122.10
GateWay 61.164.122.1
三. 安裝LVS和Keepalvied軟體包
1. 下載相關軟體包
2.安裝LVS和Keepalived
四. 配置LVS實現負載均衡
1. LVS-DR,配置LVS指令碼實現負載均衡
2. 配置Realserver指令碼.
或者採用secondary ip address方式配置
3. 啟動lvs-dr指令碼和realserver啟本,在DR上可以檢視LVS當前狀態:
五.利用Keepalvied實現負載均衡和和高可用性
1.配置在主負載均衡伺服器上配置keepalived.conf
2. BACKUP伺服器同上配置,先安裝lvs再按裝keepalived,仍後配置/etc/keepalived/keepalived.conf,只需將紅色標示的部分改一下即可.
3.
keepalived.conf 配置檔案,實現負載均衡和高可用.
4. 檢視lvs服務是否正常
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 61.164.122.8:80 wrr persistent 60
-> 61.164.122.10:80 Route 3 0 0
-> 61.164.122.9:80 Route 3 0 0
監聽日誌,檢視狀態,測試LVS負載均衡及高可用性是否有效。
5.停Master伺服器的keepalived服務,檢視BAKCUP伺服器是否能正常接管服務。
四.相關參考
1.LVS 基礎知識彙總
LVS的演算法介紹
學習LVS的三種轉發模式
LVS中的IP負載均衡技術
更多的請到 負載均衡版檢視
Keepalived 相關參考資料。
http://www.keepalived.org/documentation.html
隨著你的網站業務量的增長你網站的伺服器壓力越來越大?需要負載均衡方案!商業的硬體如F5又太貴,你們又是創業型互聯公司如何有效節約成本,節省不必要的浪費?同時實現商業硬體一樣的高效能高可用的功能?有什麼好的負載均衡可伸張可擴充套件的方案嗎?答案是肯定的!有!我們利用 LVS+Keepalived基於完整開源軟體的架構可以為你提供一個負載均衡及高可用的伺服器。
LVS+Keepalived 介紹
LVS
LVS是Linux Virtual Server的簡寫,意即Linux虛擬伺服器,是一個虛擬的伺服器叢集系統。本專案在1998年5月由章文嵩博士成立,是中國國內最早出現的自由軟體專案之一.目前有三種IP負載均衡技術(VS/NAT、VS/TUN和VS/DR);
八種排程演算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。
Keepalvied
Keepalived在這裡主要用作RealServer的健康狀態檢查以及LoadBalance主機和BackUP主機之間failover的實現
二. 網站負載均衡構架圖
IP資訊列表:
名稱 IP
LVS-DR-Master 61.164.122.6
LVS-DR-BACKUP 61.164.122.7
LVS-DR-VIP 61.164.122.8
WEB1-Realserver 61.164.122.9
WEB2-Realserver 61.164.122.10
GateWay 61.164.122.1
三. 安裝LVS和Keepalvied軟體包
1. 下載相關軟體包
- #mkdir /usr/local/src/lvs
- #cd /usr/local/src/lvs
- #wget http://
- #wget http://
2.安裝LVS和Keepalived
- #lsmod |grep ip_vs
- #uname -r
- 2.6.18-53.el5PAE
- #ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux
- #tar zxvf ipvsadm-1.24.tar.gz
- #cd ipvsadm-1.24
- #make && make install
- #find / -name ipvsadm # 檢視ipvsadm的位置
- #tar zxvf keepalived-1.1.15.tar.gz
- #cd keepalived-1.1.15
- #./configure && make && make install
- #find / -name keepalived # 檢視keepalived位置
- #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
- #cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
- #mkdir /etc/keepalived
- #cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
- #cp /usr/local/sbin/keepalived /usr/sbin/
- #service keepalived start|stop #做成系統啟動服務方便管理.
四. 配置LVS實現負載均衡
1. LVS-DR,配置LVS指令碼實現負載均衡
- #vi /usr/local/sbin/lvs-dr.sh
- #!/bin/bash
- # description: start LVS of DirectorServer
- #Written by :NetSeek http://
- GW=61.164.122.1
- # website director vip.
- SNS_VIP=61.164.122.8
- SNS_RIP1=61.164.122.9
- SNS_RIP2=61.164.122.10
- ./etc/rc.d/init.d/functions
- logger $0 called with $1
- case "$1" in
- start)
- # set squid vip
- /sbin/ipvsadm --set 30 5 60
- /sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255
- broadcast $SNS_VIP up
- /sbin/route add -host $SNS_VIP dev eth0:0
- /sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3
- /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
- /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
- touch /var/lock/subsys/ipvsadm >/dev/null 2>&1
- ;;
- stop)
- /sbin/ipvsadm -C
- /sbin/ipvsadm -Z
- ifconfig eth0:0 down
- ifconfig eth0:1 down
- route del $SNS_VIP
- route del $SS_VIP
- rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
- echo "ipvsadm stoped"
- ;;
- status)
- if [ ! -e /var/lock/subsys/ipvsadm ];then
- echo "ipvsadm stoped"
- exit 1
- else
- echo "ipvsadm OK"
- fi
- ;;
- *)
- echo "Usage: $0 {start|stop|status}"
- exit 1
- esac
- exit 0
2. 配置Realserver指令碼.
- #vi /usr/local/sbin/realserver.sh
- #!/bin/bash
- # description: Config realserver lo and apply noarp
- #Written by :NetSeek http://
- SNS_VIP=61.164.122.8
- . /etc/rc.d/init.d/functions
- case "$1" in
- start)
- ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
- /sbin/route add -host $SNS_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 $SNS_VIP >/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
- exit 0
或者採用secondary ip address方式配置
- # vi /etc/sysctl.conf
- net.ipv4.conf.lo.arp_ignore = 1
- net.ipv4.conf.lo.arp_announce = 2
- net.ipv4.conf.all.arp_ignore = 1
- net.ipv4.conf.all.arp_announce = 2
- #sysctl –p
- #ip addr add 61.164.122.8/32 dev lo
- #ip add list 檢視是否繫結
3. 啟動lvs-dr指令碼和realserver啟本,在DR上可以檢視LVS當前狀態:
- #watch ipvsadm –ln
五.利用Keepalvied實現負載均衡和和高可用性
1.配置在主負載均衡伺服器上配置keepalived.conf
- #vi /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- cnseek@gmail.com
- }
- notification_email_from sns-lvs@gmail.com
- smtp_server 127.0.0.1
- # smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- # 20081013 written by :netseek
- # VIP1
- vrrp_instance VI_1 {
- state MASTER #備份伺服器上將MASTER改為BACKUP
- interface eth0
- virtual_router_id 51
- priority 100 # 備份服務上將100改為99
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 61.164.122.8
- #(如果有多個VIP,繼續換行填寫.)
- }
- }
- virtual_server 61.164.122.8 80 {
- delay_loop 6 #(每隔10秒查詢realserver狀態)
- lb_algo wrr #(lvs 演算法)
- lb_kind DR #(Direct Route)
- persistence_timeout 60 #(同一IP的連線60秒內被分配到同一臺realserver)
- protocol TCP #(用TCP協議檢查realserver狀態)
- real_server 61.164.122.9 80 {
- weight 3 #(權重)
- TCP_CHECK {
- connect_timeout 10 #(10秒無響應超時)
- nb_get_retry 3
- delay_before_retry 3
- connect_port 80
- }
- }
- real_server 61.164.122.10 80 {
- weight 3
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 80
- }
- }
- }
2. BACKUP伺服器同上配置,先安裝lvs再按裝keepalived,仍後配置/etc/keepalived/keepalived.conf,只需將紅色標示的部分改一下即可.
3.
- vi /etc/rc.local
- #/usr/local/sbin/lvs-dr.sh 將lvs-dr.sh這個指令碼註釋掉。步
- #/usr/local/sbin/lvs-dr.sh stop 停止lvs-dr指令碼
- #/etc/init.d/keepalived start 啟動keepalived 服務,keepalived就能利用
4. 檢視lvs服務是否正常
- #watch ipvsadm –ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 61.164.122.8:80 wrr persistent 60
-> 61.164.122.10:80 Route 3 0 0
-> 61.164.122.9:80 Route 3 0 0
- #tail –f /var/log/message
5.停Master伺服器的keepalived服務,檢視BAKCUP伺服器是否能正常接管服務。
四.相關參考
1.LVS 基礎知識彙總
LVS的演算法介紹
學習LVS的三種轉發模式
LVS中的IP負載均衡技術
更多的請到 負載均衡版檢視
Keepalived 相關參考資料。
http://www.keepalived.org/documentation.html
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30496894/viewspace-1874462/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- LVS+KEEPALIVED負載均衡實驗負載
- lvs+keepAlived→效率最高的負載均衡負載
- LVS+Keepalived 實現高可用負載均衡負載
- 基於MySQL雙主複製架構下部署LVS+KeepAlived負載均衡MySql架構負載
- gRPC負載均衡(自定義負載均衡策略)RPC負載
- gRPC負載均衡(客戶端負載均衡)RPC負載客戶端
- 負載均衡負載
- nginx負載均衡Nginx負載
- LVS 負載均衡負載
- 【Nginx】負載均衡Nginx負載
- NGINX 負載均衡Nginx負載
- LoadBalancer負載均衡負載
- IP負載均衡負載
- WebSocket負載均衡Web負載
- 負載均衡---ribbon負載
- 解密負載均衡技術和負載均衡演算法解密負載演算法
- 負載均衡技術(一)———負載均衡技術介紹負載
- 【知識分享】四層負載均衡和七層負載均衡負載
- 負載均衡技術(二)———常用負載均衡服務介紹負載
- 負載均衡補充負載
- 負載均衡4層負載
- 負載均衡詳解負載
- gRPC的負載均衡RPC負載
- 負載均衡之keepalived負載
- 負載均衡知多少?負載
- Linux LVS 負載均衡Linux負載
- 淺談負載均衡負載
- 負載均衡叢集負載
- LVS負載均衡群集負載
- Nginx負載均衡模式Nginx負載模式
- 漫談負載均衡負載
- 負載均衡簡介負載
- golang grpc 負載均衡GolangRPC負載
- 負載均衡和動態負載均衡分別是什麼?-VeCloud負載Cloud
- LVS負載均衡群集概念、NAT模式LVS負載均衡實戰部署負載模式
- 做了反向代理和負載均衡的nginx配置檔案簡單示例(nginx.conf) HTTP負載均衡/TCP負載均衡負載NginxHTTPTCP
- 05-LoadBalancer負載均衡負載
- keepalived高可用負載均衡負載
- Nginx負載均衡高可用Nginx負載