LVS+Keepalived實現高可用集羣nginx
LVS+Keepalived 介紹算法
LVSshell
LVS是Linux Virtual Server的簡寫,意即Linux虛擬服務器,是一個虛擬的服務器集羣 vim
系統。本項目在1998年5月由章文嵩博士成立,是中國國內最先出現的自由軟件項 目之 bash
一。目前有三種IP負載均衡技術(VS/NAT、VS/TUN和VS/DR);服務器
十種調度算法(rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。負載均衡
Keepalviedtcp
Keepalived在這裏主要用做RealServer的健康狀態檢查以及LoadBalance主機和BackUP oop
主機之間failover的實現測試
IP配置信息:
LVS-DR-Master 172.16.1.1
LVS-DR-BACKUP 172.16.1.8
LVS-DR-VIP 172.16.1.80
WEB1-Realserver01 172.16.1.2
WEB2-Realserver02 172.16.1.3
WEB3-Realserver03 172.16.1.4
一,安裝LVS和Keepalvied軟件包
1. Install ipvsadm
yum –y ipvsadm
2. 安裝LVS和Keepalived
yum –y install gcc kernel-devel openssl-devel tar zxvf keepalived-1.1.15.tar.gz cd keepalived-1.1.15 ./configure -–sysconfdir=/etc/ --with-kernel-dir=/usr/src.kernel/2.6XXXX make && make install ln -s /usr/local/sbin/keepalived /sbin/ chkconfig --add keepalived chkconfig --level 2345 keepalived on service keepalived restart
二. 配置LVS實現負載均衡
lvs-dr
#!/bin/bash VIP=172.16.1.80 ./etc/rc.d/init.d/functions case "$1" in start) ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev eth0:1 echo "DirectoryServer Start OK" ;; stop) ipvsadm -C ipvsadm -Z ifconfig eth0:1 down /sbin/route del $VIP >/dev/null 2>&1 echo "DirectoryServer Stop OK" ;; *) echo "Usage: $0 {start|stop}" exit 1 esca exit0
lvs-realserver
#!/bin/bash
VIP=10.207.238.82 . /etc/rc.d/init.d/functions case "$1" in start) /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /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) /sbin/ifconfig lo:0 down /sbin/route del $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 "Usage: $0 {start|stop}" exit 1 esac exit 0 #end
三.利用Keepalvied結合lvs實現高可用實現負載均衡和和高可用性
1.配置在主負載均衡服務器上配置keepalived.conf
#vi /etc/keepalived/keepalived.conf (主調度器)
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.1.80 } } virtual_server 172.16.1.80 80 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 172.16.1.2 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 172.16.1.3 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 172.16.1.4 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
在備用調度器上:
#vi /etc/keepalived/keepalived.conf (備調度器)
vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.1.80 } } virtual_server 172.16.1.80 80 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 172.16.1.2 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 172.16.1.3 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 172.16.1.4 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
若是報錯
Jan 11 09:06:26 hnxz-vm-dg-s1 Keepalived_vrrp[2436]: ip address associated with VRID not present in received packet : 10.207.238.82 Jan 11 09:06:26 hnxz-vm-dg-s1 Keepalived_vrrp[2436]: one or more VIP associated with VRID mismatch actual MASTER advert
應該為virtual id 問題能夠通過命令查看
tcpdump -nn -i any net 224.0.0.0/8
#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 監聽日誌,查看狀態,測試LVS負載均衡及高可用性是否
有效。
5.停Master服務器的keepalived服務,查看BAKCUP服務器是否能正常接管服務。
6.單純keepalived 實現高可用
! Configuration File for keepalived global_defs { notification_email { jason.zr.zhang@gmail.com } notification_email_from jason.zr.chang@mail.foxconn.com smtp_server 10.195.225.126 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "/etc/keepalived/check-httpd.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 80 priority 100 authentication { auth_type PASS auth_pass eric } track_script { chk_http_port } virtual_ipaddress { 10.207.238.82 } }
檢測腳本文件
vim /etc/keepalived/check-httpd.sh
#!/bin/bash A=`ps -C httpd –no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx #nginx命令的路徑 sleep 3 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fi fi
測試是否正常