1. 程式人生 > >keepalived lvs 高可用負載均衡

keepalived lvs 高可用負載均衡

keepalived lvs 高可用 負載均衡

主分發器設置

安裝keepalived和nginx
yum install -y keepalived
yum install -y nginx
關閉防火墻和SELinux
systemctl stop firewalld
setenforce 0
配置網卡ens33

[root@lynn-04 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.116
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

編輯/etc/keepalived/keepalived.conf
寫入如下內容:

[root@lynn-04 ~]# vim /etc/keepalived/keepalived.conf

    script "/usr/local/sbin/check_ng.sh"
    interval 3
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aminglinux>com
    }
    virtual_ipaddress {
        192.168.130.100
    }
    track_script {
        chk_nginx
    }
}
virtual_server 192.168.130.100 80 {
    #(每隔10秒查詢realserver狀態)
    delay_loop 10
    #(lvs 算法)
    lb_algo wlc
    #(DR模式)
    lb_kind DR
    #(同一IP的連接0秒內被分配到同一臺realserver)
    persistence_timeout 0
    #(用TCP協議檢查realserver狀態)
    protocol TCP
    real_server 192.168.130.118 80 {
        #(權重)
        weight 100
        TCP_CHECK {
        #(10秒無響應超時)
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.130.119 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

定義監控腳本/usr/local/sbin/check_ng.sh

[root@lynn-04 ~]# vim /usr/local/sbin/check_ng.sh

寫入下面內容

#!/bin/bash
#時間變量,用於記錄日誌
d=`date --date today +%Y%m%d_%H:%M:%S`
#計算nginx進程數量
n=`ps -C nginx --no-heading|wc -l`
#如果進程為0,則啟動nginx,並且再次檢測nginx進程數量,
#如果還為0,說明nginx無法啟動,此時需要關閉keepalived
if [ $n -eq "0" ]; then
        /etc/init.d/nginx start
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                systemctl stop keepalived
        fi
fi

更改/usr/local/sbin/check_ng.sh權限

[root@lynn-04 ~]# chmod 755 /usr/local/sbin/check_ng.sh

編寫腳本 /usr/local/sbin/lvs_dr.sh

[root@lynn-04 ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.130.100
rs1=192.168.130.118
rs2=192.168.130.119
#註意這裏的網卡名字
ifdown ens33
ifup ens33
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:2

執行腳本 /usr/local/sbin/lvs_dr.sh

[root@lynn-04 ~]# sh /usr/local/sbin/lvs_dr.sh
成功斷開設備 ‘ens33‘。
連接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/4)

啟動keepalived

[root@lynn-04 ~]# systemctl start keepalived
[root@lynn-04 ~]# ps aux |grep keepalived
root       2190  0.1  0.1 120740  1404 ?        Ss   14:33   0:00 /usr/sbin/keepalived -D
root       2191  0.1  0.3 127480  3336 ?        S    14:33   0:00 /usr/sbin/keepalived -D
root       2192  0.0  0.3 131780  3108 ?        S    14:33   0:00 /usr/sbin/keepalived -D
root       2206  0.0  0.0 112676   988 pts/0    S+   14:33   0:00 grep --color=auto keepalived

啟動nginx

[root@lynn-04 ~]# /etc/init.d/nginx start
[root@lynn-04 ~]# ps aux |grep nginx
root        912  0.0  0.1  45988  1240 ?        Ss   08:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      913  0.0  0.4  48476  4388 ?        S    08:06   0:00 nginx: worker process
nobody      915  0.0  0.4  48476  4392 ?        S    08:06   0:00 nginx: worker process
root       2072  0.0  0.0 112680   980 pts/0    S+   10:23   0:00 grep --color=auto nginx

把nginx的默認頁做個標記

[root@lynn-04 ~]# echo master > /data/wwwroot/default/index.html

在另一臺機器使用從ip訪問
得到下面結果

[root@lynn-02 ~]# curl 192.168.130.116
master

從分發器設置

安裝keepalived和nginx
yum install -y keepalived
yum install -y nginx
關閉防火墻和SELinux
systemctl stop firewalld
setenforce 0
配置網卡ens33

[root@lynn-06 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=fef8a686-6a1e-49bc-b03e-7797fc4c58bb
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.128
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

編輯/etc/keepalived/keepalived.conf
寫入如下內容:

[root@lynn-06 ~]# vim /etc/keepalived/keepalived.conf

    interval 3
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aminglinux>com
    }
    virtual_ipaddress {
        192.168.130.100
    }
    track_script {
        chk_nginx
    }
}
virtual_server 192.168.130.100 80 {
    #(每隔10秒查詢realserver狀態)
    delay_loop 10
    #(lvs 算法)
    lb_algo wlc
    #(DR模式)
    lb_kind DR
    #(同一IP的連接0秒內被分配到同一臺realserver)
    persistence_timeout 0
    #(用TCP協議檢查realserver狀態)
    protocol TCP
    real_server 192.168.130.118 80 {
        #(權重)
        weight 100
        TCP_CHECK {
        #(10秒無響應超時)
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.130.119 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

定義監控腳本/usr/local/sbin/check_ng.sh

[root@lynn-06 ~]# vim /usr/local/sbin/check_ng.sh

#時間變量,用於記錄日誌
d=`date --date today +%Y%m%d_%H:%M:%S`
#計算nginx進程數量
n=`ps -C nginx --no-heading|wc -l`
#如果進程為0,則啟動nginx,並且再次檢測nginx進程數量,
#如果還為0,說明nginx無法啟動,此時需要關閉keepalived
if [ $n -eq "0" ]; then
        systemctl start nginx
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                systemctl stop keepalived
        fi
fi

更改/usr/local/sbin/check_ng.sh權限

[root@lynn-06 ~]# chmod 755 /usr/local/sbin/check_ng.sh

編寫腳本 /usr/local/sbin/lvs_dr.sh

[root@lynn-06 ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.130.100
rs1=192.168.130.118
rs2=192.168.130.119
#註意這裏的網卡名字
ifdown ens33
ifup ens33
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:2

執行腳本 /usr/local/sbin/lvs_dr.sh

[root@lynn-06 ~]# sh /usr/local/sbin/lvs_dr.sh
成功斷開設備 ‘ens33‘。
連接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/4)

啟動keepalived

[root@lynn-06 ~]# systemctl start keepalived
[root@lynn-06 ~]# ps aux |grep keepalived
root       2190  0.1  0.1 120740  1404 ?        Ss   14:33   0:00 /usr/sbin/keepalived -D
root       2191  0.1  0.3 127480  3336 ?        S    14:33   0:00 /usr/sbin/keepalived -D
root       2192  0.0  0.3 131780  3108 ?        S    14:33   0:00 /usr/sbin/keepalived -D
root       2206  0.0  0.0 112676   988 pts/0    S+   14:33   0:00 grep --color=auto keepalived

啟動nginx

[root@lynn-06 ~]# systemctl start nginx
[root@lynn-06 ~]# ps aux |grep nginx
root        912  0.0  0.1  45988  1240 ?        Ss   08:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      913  0.0  0.4  48476  4388 ?        S    08:06   0:00 nginx: worker process
nobody      915  0.0  0.4  48476  4392 ?        S    08:06   0:00 nginx: worker process
root       2072  0.0  0.0 112680   980 pts/0    S+   10:23   0:00 grep --color=auto nginx

把nginx的默認頁做個標記

[root@lynn-06 ~]# echo backup > /usr/share/nginx/html/index.html

在另一臺機器使用從ip訪問
得到下面結果

[root@lynn-02 ~]# curl 192.168.130.128
backup

rs1設置

安裝nginx
yum install -y nginx
關閉防火墻和SELinux
systemctl stop firewalld
setenforce 0
配置網卡ens33

[root@lynn-05 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.118
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

編寫腳本/usr/local/sbin/lvs_rs.sh

[root@lynn-05 ~]# vim /usr/local/sbin/lvs_rs.sh

#/bin/bash
vip=192.168.130.100
#把vip綁定在lo上,是為了實現rs直接把結果返回給客戶端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作為更改arp內核參數,目的是為了讓rs順利發送mac地址給客戶端
#參考文檔www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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

執行腳本/usr/local/sbin/lvs_rs.sh

[root@lynn-05 ~]# sh /usr/local/sbin/lvs_rs.sh

啟動nginx

[root@lynn-05 ~]# systemctl start nginx
[root@lynn-05 ~]# ps aux |grep nginx
root        995  0.0  0.2 122908  2236 ?        Ss   14:29   0:00 nginx: master process /usr/sbin/nginx
nginx       996  0.0  0.3 123292  3136 ?        S    14:29   0:00 nginx: worker process
nginx       997  0.0  0.3 123292  3136 ?        S    14:29   0:00 nginx: worker process
nginx       998  0.0  0.3 123292  3136 ?        S    14:29   0:00 nginx: worker process
nginx       999  0.0  0.3 123292  3136 ?        S    14:29   0:00 nginx: worker process
root       1006  0.0  0.0 112676   980 pts/0    R+   14:30   0:00 grep --color=auto nginx

把nginx的默認頁做個標記

[root@lynn-05 ~]# echo This is lynn-05 > /usr/share/nginx/html/index.html

在另一臺機器使用從ip訪問
得到下面結果

[root@lynn-02 ~]# curl 192.168.130.118
This is lynn-05

rs2設置

安裝nginx
yum install -y nginx
關閉防火墻和SELinux
systemctl stop firewalld
setenforce 0
配置網卡ens33

[root@lynn-07 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.119
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29

編寫腳本/usr/local/sbin/lvs_rs.sh

[root@lynn-07 ~]# vim /usr/local/sbin/lvs_rs.sh

#/bin/bash
vip=192.168.130.100
#把vip綁定在lo上,是為了實現rs直接把結果返回給客戶端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作為更改arp內核參數,目的是為了讓rs順利發送mac地址給客戶端
#參考文檔www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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

執行腳本/usr/local/sbin/lvs_rs.sh

[root@lynn-07 ~]# sh /usr/local/sbin/lvs_rs.sh

啟動nginx

[root@lynn-07 ~]# systemctl start nginx
[root@lynn-07 ~]# ps aux |grep nginx
root        990  0.0  0.2 122908  2244 ?        Ss   14:30   0:00 nginx: master process /usr/sbin/nginx
nginx       991  0.0  0.3 123292  3144 ?        S    14:30   0:00 nginx: worker process
nginx       992  0.0  0.3 123292  3144 ?        S    14:30   0:00 nginx: worker process
nginx       993  0.0  0.3 123292  3144 ?        S    14:30   0:00 nginx: worker process
nginx       994  0.0  0.3 123292  3144 ?        S    14:30   0:00 nginx: worker process
root        996  0.0  0.0 112676   980 pts/0    S+   14:30   0:00 grep --color=auto nginx

把nginx的默認頁做個標記

[root@lynn-07 ~]# echo This is lynn-07 > /usr/share/nginx/html/index.html

在另一臺機器使用從ip訪問
得到下面結果

[root@lynn-02 ~]# curl 192.168.130.119
This is lynn-07

測試

從下圖可以看出當vip在主機上時可以實現負載均衡
技術分享圖片
從下圖可以看出當主機keepalived停止後vip就出現在從機上時也能可以實現負載均衡
技術分享圖片
從下圖可以看出當主機keepalived再次啟動時vip又回到主機負載均衡還是可以實現
技術分享圖片
綜上這裏就同時實現了高可用和負載均衡

keepalived lvs 高可用負載均衡