LVS介紹
lvs 核心ipvs
Ipvs(IP Virtual Server)是整個負載均衡的基礎,如果沒有這個基礎,故障隔離與失敗切換就毫無意義了。Ipvs 具體實現是由ipvsadm 這個程序來完成,因此判斷一個系統是否具備ipvs 功能,只需要察看ipvsadm 程序是否被安裝。察看ipvsadm 程序最簡單的辦法就是在任意路徑執行命令ipvsadm。
1 [root@LVS-DR-Backup ~]# ipvsadm(已安裝) 2 IP Virtual Server version 1.2.1 (size=4096) 3 Prot LocalAddress:Port Scheduler Flags 4 -> RemoteAddress:Port Forward Weight ActiveConn InActConn 5 [root@LVS-DR-Backup ~]# ipvsadm (未安裝) 6 -bash: ipvsadm: command not found
安裝lvs
1、下載ipvsadm,wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
2、 創建一個連接文件,其命令為: ln -s /usr/src/kernels/2.6.32-573.el6.x86_64/ /usr/src/linux注意一定要與當前的運行的內核相一致,因為/usr/src/kernels目錄下可多個目錄。如果不創建這個連接文件,在編譯時會出錯,從而不能繼續進行安裝。
3、 解包。 tar zxf ipvsadm-1.26.tar.gz
4、 編譯並安裝。 cd ipvsadm-1.26; make;make install
5、檢驗ipvsadm 是否被正確安裝:a.執行ipvsadm,看是否有如上的輸出。b. 檢查當前加載的內核模塊,看是否存在ip_vs 模塊。
1 [root@LVS-DR-Backup ~]# lsmod|grep ip_vs 2 ip_vs 126534 0 3 libcrc32c 1246 1 ip_vs 4 ipv6 335589 137 ip_vs (注:只有執行ipvsadm 以後,才會在內核加載ip_vs 模塊,也不能以查進程的方式判斷ipvs 是否運行。)
lvs客戶端
不需要安裝軟件,只運行一個腳本就可以,腳本如下:
1 #!/bin/bash 2 3 VIP=此處設置你的vip地址 4 5 . /etc/rc.d/init.d/functions 6 7 case "$1" in 8 start) 9 action " start LVS of REALServer" /bin/true 10 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up 11 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore 12 echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce 13 echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore 14 echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 15 ;; 16 stop) 17 /sbin/ifconfig lo:0 down 18 action "close LVS Directorserver" /bin/true 19 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore 20 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce 21 echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore 22 echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce 23 ;; 24 *) 25 echo "Usage: $0 {start|stop}" 26 exit 1 27 esac
keepalived介紹
1、 keepalived 是lvs 的擴展項目,因此它們之間具備良好的兼容性。
2、 通過對服務器池對象的健康檢查,實現對失效機器/服務的故障隔離。
3、 負載均衡器之間的失敗切換failover,是通過VRRPv2(Virtual Router Redundancy Protocol)stack 實現的。
keepalived內部結構圖
在這個結構圖裡,處於下端的是內核空間,它包括ipvs 和NETLINK 兩個部分。Ipvs 的作用在前面的章節已經做過描述,不再重復敘述;netlink 提供高級路由及其他相關的網絡功能,如果我們在負載均衡器上啟用netfilter/iptable,將會直接影響它的性能。出於圖形上方的組件為用戶空間,由它來實現具體的功能,下面選取幾個重要的來做說明:
1、 WatchDog 負責監控checkers 和VRRP 進程的狀況。
2、 Checkers 負責真實服務器的健康檢查healthchecking,是keepalived 最主要的功能。換句話說—可以沒有VRRP Stack,但健康檢查healthchecking 是一定要有的。
3、 VRRP Stack 負責負載均衡器之間的失敗切換FailOver.如果只用一個負載均衡器,則VRRP 不是必須的。
4、 IPVS wrapper 用來發送設定的規則到內核ipvs 代碼。
5、 Netlink Reflector 用來設定 vrrp 的vip 地址等。
安裝keepalived(因為比較簡單,不做詳細介紹)
1、 下載最新穩定版 http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
2、 解包 tar zxf keepalived-1.2.23.tar.gz
3、 切換目錄 cd keepalived-1.2.23
4、 安裝依賴包 yum install -y libnfnetlink-devel openssl-devel
5、 配置並編譯安裝./configure ; make ; make install
6、 拷貝相應文件到/etc下,命令如下:
1 cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ 2 cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ 3 cp /usr/local/sbin/keepalived /usr/sbin/ 4 mkdir /etc/keepalived -p 5 cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
7、啟動並查看keepalived
1 [root@LVS-DR-Backup script]# /etc/init.d/keepalived start 2 正在啟動 keepalived: [確定] 3 [root@LVS-DR-Backup script]# ipvsadm 4 IP Virtual Server version 1.2.1 (size=4096) 5 Prot LocalAddress:Port Scheduler Flags 6 -> RemoteAddress:Port Forward Weight ActiveConn InActConn 7 TCP 192.168.200.100:https rr persistent 50 8 -> 192.168.201.100:https Masq 1 0 0 9 TCP 10.10.10.2:connlcli rr persistent 50 10 -> 192.168.200.2:connlcli Masq 1 0 0 11 -> 192.168.200.3:connlcli Masq 1 0 0 12 TCP 10.10.10.3:connlcli rr persistent 50 13 -> 192.168.200.4:connlcli Masq 1 0 0 14 -> 192.168.200.5:connlcli Masq 1 0 0 15 [root@LVS-DR-Backup script]#
8、至此安裝完成,下一章,keepalived.conf詳解。
參考:http://www.keepalived.org/documentation.html
附:lvs和keepalived一鍵安裝腳本
1 #!/bin/bash 2 # Filename :Install_lvs.sh 3 # Description : 安裝lvs+keepalived軟件腳本 4 5 #參數設置 6 Toolsdir=/home/test/tools 7 IPVSADM=ipvsadm-1.26 8 Kernel=uname -r 9 KEEPALIVED=keepalived-1.2.23 10 11 12 #定義安裝ipvsadm函數 13 function IPvsadm(){ 14 cd ${Toolsdir} 15 #下載ipvsadm軟件 16 [ ! -f ${IPVSADM}.tar.gz ] && wget http://www.linuxvirtualserver.org/software/kernel-2.6/${IPVSADM}.tar.gz 17 #安裝依賴包 18 yum install -y popt libnl-devel popt-static 19 20 #解壓並安裝ipvsadm 21 tar zxf ${IPVSADM}.tar.gz 22 if [ $? == 0 ];then 23 cd ${IPVSADM} 24 ln -s /usr/src/kernel/${Kernel}/ /usr/src/linux 25 make 26 if [ $? == 0 ];then 27 make install 28 if [ $? == 0 ];then 29 echo "IPVSADM INSTALL SUCCESSFUL." 30 else 31 echo "make install error." 32 eixt 0 33 sleep 5 34 fi 35 else 36 echo "make error." 37 exit 1 38 fi 39 else 40 echo "tar error." 41 exit 2 42 fi 43 } 44 45 #定義安裝keepalived函數 46 47 function Keepalived(){ 48 #下載軟件 49 cd ${Toolsdir} 50 [ ! -f ${KEEPALIVED}.tar.gz ] && wget http://www.keepalived.org/software/${KEEPALIVED}.tar.gz 51 52 #安裝依賴包 53 yum install -y libnfnetlink-devel openssl-devel 54 55 #安裝keepalived 56 tar zxf ${KEEPALIVED}.tar.gz 57 if [ $? == 0 ];then 58 cd ${KEEPALIVED} 59 ./configure 60 if [ $? == 0 ];then 61 make 62 if [ $? == 0 ];then 63 make install 64 if [ $? == 0 ];then 65 echo "Keepalived install successful." 66 else 67 echo "make install error" 68 fi 69 else 70 echo "make error." 71 fi 72 else 73 echo "configure error." 74 fi 75 else 76 echo "tar error" 77 fi 78 79 #拷貝配置文件 80 cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ 81 cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ 82 cp /usr/local/sbin/keepalived /usr/sbin/ 83 mkdir /etc/keepalived -p 84 cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ 85 86 #開啟內核轉發 87 sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' sysctl.conf 88 sysctl -p 89 } 90 91 IPvsadm 92 Keepalived View Code
http://xxxxxx/Linuxjc/1154581.html TechArticle