LVS+Keepalived負載均衡配置部署

MrUse發表於2015-02-14

相關概念

一、準備工作
1. 伺服器、作業系統(當前CentOS6.6 x86_64)、網路環境(服務期間網路互通)

# 作業系統:CentOS6.6 x86_64
# 伺服器 (LVS主備各1臺,WEB伺服器3臺,圖片伺服器2臺)
WEBServer負載虛擬IP:192.168.0.20
IMGServer負載虛擬IP:192.168.0.30
LVS主:192.168.0.11
LVS備:192.168.0.12
WebServer1: 192.168.0.21
WebServer2: 192.168.0.22
WebServer3: 192.168.0.23
IMGServer1: 192.168.0.31
IMGServer2: 192.168.0.32 

2. 關閉SELinux(改完需重啟伺服器)

# 關閉SELinux
sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/sysconfig/selinux   
# 重啟伺服器
reboot  

3. Iptables開啟需要埠,如80;

# Iptables 開啟相應埠
iptables -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT   
iptables -A OUTPUT  -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT  

二、LVS主機安裝Keepalived,ipvsadm
1. 安裝依賴包

yum -y install wget popt popt-devel popt-static libnl libnl-devel kernel-devel;  
ln -s /usr/src/kernels/$(uname -r)/ /usr/src/linux;  

2. 安裝 Keepalived,ipvsadm(YUM安裝或TarBall安裝)

# YUM安裝 Keepalived,ipvsadm
yum -y install  keepalived ipvsadm;

# TarBall安裝Keepalived
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz;
tar xvf keepalived-1.2.15.tar.gz -C /usr/local/src/;
cd /usr/local/src/keepalived-1.2.15/;
./configure \
--bindir=/usr/bin \
--sbindir=/usr/sbin \
--sysconf=/etc \
--with-kernel-dir=/usr/src/kernels/$(uname -r)/;
make;
make install;
# TarBall安裝ipvsadm
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz;  
tar xvf ipvsadm-1.26.tar.gz -C /usr/local/src/;
cd /usr/local/src/ipvsadm-1.26/;
make;
make install;

3. 建立ipvsadm配置檔案,啟動並加入開機啟動

/etc/init.d/ipvsadm save;  
/etc/init.d/ipvsadm start;
chkconfig ipvsadm on;

4. 配置並啟動keepalived

# 備份keepalived配置檔案
cd /etc/keepalived/;
[ -f "keepalived.conf" -a ! -f "keepalived.conf.default" ] && mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.default;
[ -f "keepalived.conf" ] && mv keepalived.conf keepalived.conf.$(date +%F_%T);

# 建立 Keepalived 配置檔案(需先修改 virtual_ipaddress,virtual_server,real_server 配置的IP)
cat >> /etc/keepalived/keepalived.conf <<KEEPALIVED
! Configuration File for keepalived

global_defs {
    notification_email {
        mr@mruse.cn
    }
    notification_email_from xxx@163.com
    smtp_server smtp@163.com
    smtp_connect_timeout 30
    router_id LVS_1
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 60
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass MrUse
    }
    virtual_ipaddress {
        192.168.0.20
        192.168.0.30
    }
}

virtual_server 192.168.0.21 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    persistence_timeout 3
    protocol TCP

    real_server 192.168.0.22 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

    real_server 192.168.0.23 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

virtual_server 172.16.100.130 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    persistence_timeout 3
    protocol TCP

    real_server 192.168.0.31 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

    real_server 192.168.0.32 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}
KEEPALIVED

# 啟動Keepalived並加入開機啟動
/etc/init.d/keepalived restart;
chkconfig keepalived on;

5. 記錄Keepalived日誌

# 註釋Keepalived預設配置
grep ^KEEPALIVED_OPTIONS /etc/sysconfig/keepalived && sed -i 's#^KEEPALIVED_OPTIONS#\#KEEPALIVED_OPTIONS#g' /etc/sysconfig/keepalived;

# 更新keepalived配置
echo 'KEEPALIVED_OPTIONS="-D -d -S 0"' >> /etc/sysconfig/keepalived;
/etc/init.d/keepalived restart
grep "# CmsTop Keepalived" /etc/rsyslog.conf||cat >> /etc/rsyslog.conf <<SYSLOG
# CmsTop Keepalived $(date +%F)
# keepalived -D -d -S 0
local0.*                    /var/log/keepalived.log
SYSLOG
/etc/init.d/rsyslog restart

6. 為真實伺服器 WebServer, IMGServe建立配置啟動指令碼(需修改IP地址)

[ -f "/etc/init.d/lvsr" ] && mv /etc/init.d/lvsr /etc/init.d/lvsr.$(date +%F_%T)
cat >> /etc/init.d/lvsr <<LVSR
#!/bin/bash

SNS_VIP=172.16.100.120

/etc/rc.d/init.d/functions

start()
{
    /sbin/ifconfig lo:0 \$SNS_VIP netmask 255.255.255.255 broadcast \$SNS_VIP up
    /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 "LVS RealServer Start OK"
}
stop()
{
    /sbin/ifconfig lo:0 down
    /sbin/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 "LVS RealServer Stoped"
}

case "\$1" in
    start)
        start;
    ;;
    stop)
        stop;
    ;;
    restart)
        stop;
        start;
    ;;
    status)
        /sbin/ifconfig|tail -5
    ;;
    *)
        echo "Usage: \$0 {start|stop|restart|status}"
        exit 1
esac

exit 0
LVSR

# 修改依賴檔案及啟動指令碼許可權並啟動
chmod +x /etc/init.d/lvsr;
chmod +x /etc/rc.d/init.d/functions;
/etc/init.d/lvsr start

# 加入開機啟動
grep ^/etc/init.d/lvsr /etc/rc.local || echo '/etc/init.d/lvsr start' >> /etc/rc.local

三、檢視狀態、測試排錯
1. 重啟Keepalived,在LVS伺服器上檢視狀態

/etc/init.d/keepalived restart;
/etc/init.d/ipvsadm status;

2. 測試排查思路

# 檢視配置檔案,檢視ip
# 檢視Iptables是否開啟相應埠
# 檢視網路,伺服器間及與虛擬IP是否可以通訊
# 檢視Keepalived,ipvsadm是否正常啟動
# 檢視錯誤日誌

相關文章