LVS+Keepalived負載均衡操做手冊 html
1、LVS+Keepalived架構linux
信息列表:
算法
資料下載(沒法下載mailto:1013269096@qq.com): vim
ipvsadm-1.26.tar.gz bash
2、LVS+Keepalived安裝與配置 架構
一、 配置環境: app
操做系統:CentOS6.4 64位 負載均衡
軟件:ipvsadm-1.26.tar.gz、keepalived-1.2.16.tar.gz
二、 安裝LVS(在LVS-Master和LVS-Backup上執行)
安裝前的準備:[root@lvs-master ipvsadm-1.26]# make && make install
三、安裝Keepalived(在LVS-Master和LVS-Backup上執行)
[root@lvs-master ~]# tar xf keepalived-1.2.16.tar.gz
[root@lvs-master ~]# cd keepalived-1.2.16
[root@lvs-master keepalived-1.2.16]# ./configure --prefix=/usr/local/keepalived
[root@lvs-master keepalived-1.2.16]# make && make install
[root@lvs-master keepalived-1.2.16]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@lvs-master keepalived-1.2.16]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@lvs-master keepalived-1.2.16]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@lvs-master keepalived-1.2.16]#mkdir /etc/keepalived
#!/bin/bashVIP=192.168.0.2 #定義虛擬IP地址RIP1=192.168.0.5 #定義Real IP1地址RIP2=192.168.0.6 #定義Real IP2地址/etc/rc.d/init.d/functions #IP啓動提供基礎功能case "$1" instart)echo "start LVS of DirectorServer"#Set the Virtual IP Address/sbin/ifconfig eth1:0 $VIP broadcast $VIP netmask 255.255.255.255 up #在網卡eth1上綁定虛擬IP地址/sbin/route add -host $VIP dev eth1:0 #添加虛擬IP地址路由進路由表#Clear IPVS Table/sbin/ipvsadm –C #刪除以前IPVS表中信息/sbin/ipvsadm -A -t $VIP:80 -s rr #配置使用RR調度算法進行負載調度/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g #配置使用DR負載均衡技術/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g#Run Lvs #啓用LVS服務/sbin/ipvsadm ;;stop) echo "Close LVS Directorserver" #關閉LVS服務/sbin/ifconfig eth1:0 down/sbin/ipvsadm -C ;;*)echo "Usage: $0 {start|stop}"exit 1esac該腳本在LVS-Master和LVS-Backup上分別建立,並做以下設置:
global_defs {notification_email { 1013269096@qq.com #email 通知 } notification_email_from 1013269096@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS-Master # 設置lvs的id,在一個網絡內應該是惟一的,備用服務器設置爲LVS-Backup}vrrp_sync_group test { #設置vrrp組group { loadbalance}}vrrp_instance loadbalance { state MASTER #設置lvs的狀態,報錯MASTER和BACKUP兩種,必須大寫,備用服務器設置爲BACKUP interface eth1 #設置對外服務的接口 lvs_sync_daemon_inteface eth1 #設置lvs監聽的接口 virtual_router_id 51 #設置虛擬路由表示 priority 180 #設置優先級,數值越大,優先級越高 advert_int 5 #設置同步時間間隔authentication { #設置驗證類型和密碼 auth_type PASS auth_pass 1111}virtual_ipaddress { #設置lvs vip 192.168.0.2}}virtual_server 192.168.0.2 80 { delay_loop 6 #健康檢查時間間隔 lb_algo rr #負載均衡調度算法 lb_kind DR #負載均衡轉發規則 #persistence_timeout 20 #設置會話保持時間,對bbs等頗有用 protocol TCP #協議 real_server 192.168.0.5 80 { weight 3 #設置權重 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80}} real_server 192.168.0.6 80 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80}}}該腳本在LVS-Master和LVS-Backup上分別建立,並做以下設置:
RealServer01設置:
[root@realserver01 ~]# yum install httpd –y
[root@realserver01 ~]# service httpd restart
[root@realserver01 ~]# echo "I am RealServer01" > /var/www/html/index.html
RealServer02設置:
[root@realserver02 ~]# yum install httpd –y
[root@realserver02 ~]# service httpd restart
[root@realserver02 ~]# echo "I am RealServer02" > /var/www/html/index.html
在兩臺RealServer服務器上創建腳本:
[root@realserver01 ~]# vim /etc/rc.d/init.d/realserver.sh
#!/bin/bash# description: Config realserver lo and apply noarpSNS_VIP=192.168.0.2/etc/rc.d/init.d/functionscase "$1" instart) 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 1esacexit 0
給該文件賦予可執行權限:
[root@realserver01 ~]# chmod +x /etc/rc.d/init.d/realserver.sh
設置開機啓動,在/etc/rc.local加入:
/etc/rc.d/init.d/realserver.sh start
防火牆設置(RealServer01和RealServer02一樣設置):
[root@realserver01 ~]# iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
[root@realserver01 ~]# service iptables save
修改內核參數:
net.nf_conntrack_max=655360
net.netfilter.nf_conntrack_tcp_timeout_established=36000
1、在Win主機上新增環回虛擬網卡(Microsoft Loopback Adapter)
以管理員身份運行cmd後,在cmd命令窗口中執行:hdwwiz,彈出以下界面:
下一步後,勾選手動選擇:
點選網絡適配器,繼續下一步:
左側選擇Microsoft,右側找到如圖硬件(Windows Server 2008/7中則叫:Microsoft Loopback Adapter)點擊下一步開始安裝,直至完成,則成功添加了環回虛擬網卡。 成功安裝該適配器後,與其餘任何適配器同樣,您能夠手動配置其選項。若是TCP/IP 屬性配置爲使用 DHCP,則該適配器最終將使用一個 autonet 地址 (169.254.x.x/16),由於該適配器並無與任何物理媒體實際相鏈接。
注意:默認狀況下,TCP/IP 屬性配置爲使用 DHCP。
2、設置環回網卡TCP/IP信息
打開Windows 7/8/Server 2008的網絡和共享中心,左側點開「更改適配器設置」,找到新增的環回鏈接,重命名爲 realserver(推薦重命名)。 右鍵realserver鏈接屬性,開始設置TCP/IP:
和Linux中的lo:0一個樣,將IP地址設置爲VIP,將掩碼設置爲255.255.255.255,其他留空便可。
3、修改客戶端網卡接口、環回接口鏈接模式
將如下代碼保存爲bat執行,或直接在CMD中依次執行2~5行命令便可(雙引號中需根據實際連線名稱修改)
netsh interface ipv4 set interface"realserver" weakhostreceive=enabled
netsh interface ipv4 set interface"realserver" weakhostsend=enabled
netsh interface ipv4 set interface "本地鏈接" weakhostreceive=enabled
netsh interface ipv4 set interface "本地鏈接" weakhostsend=enabled
「本地鏈接」和「realserver」在同一網段。