keepalived(三)LVS+Keepalived

十五十六發表於2018-09-15

實驗

內容

keepalived+lvs實現後端web服務的負載均衡與高可用

架構圖

這裡寫圖片描述

環境

ip 系統 軟體 角色
192.168.253.128 CentOS7 keepalived keepalived的MASTER節點
192.168.253.158 CentOS7 keepalived keepalived的BACKUP節點
192.168.253.145 CentOS7 nginx 後端web伺服器
192.168.253.129 CentOS6 nginx 後端web伺服器

第一步:192.168.253.145端安裝nginx

systemctl  stop firewalld 
setenforce 0
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo如果已經有epel源不需要執行
yum install nginx  -y  #nginx需要epel源才可以yum安裝

第二步:192.168.253.145上建立nginx起始頁,啟動nginx

echo "192.168.253.145" > /usr/share/nginx/html/index.html
systemctl  start nginx

curl 192.168.253.145 測試一下
這裡寫圖片描述

第三步:192.168.253.129上安裝nginx

/etc/init.d/iptables  stop
setenforce 0
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo 已經有epel源不需要執行
yum install nginx   -y

第四步:192.168.253.129上建立nginx起始頁,啟動nginx

echo "192.168.253.129" > /usr/share/nginx/html/index.html
/etc/init.d/nginx  start nginx

curl 192.168.253.129測試一下
這裡寫圖片描述

第五步:在192.168.253.129和192.168.253.145建立一個指令碼用於修改核心引數,阻止arp通告和響應,以及在本地環回介面建立vip192.168.253.190/32

[root@localhost ~]# cat RS.sh 
#!/bin/bash
#
vip=192.168.253.190  ##vip
mask='255.255.255.255' ##掩碼

case $1 in
start)
    ##阻止arp通告與響應
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore  
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

    ifconfig lo:0 $vip netmask $mask broadcast $vip up #設定vip
    route add -host $vip dev lo:0  #增加主機路由
    ;;
stop)
    ifconfig lo:0 down

    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce

    ;;
*) 
    echo "Usage $(basename $0) start|stop"
    exit 1
    ;;
esac

在兩臺機上都執行

sh RS.sh start #啟動指令碼

第六步:192.168.253.128端安裝keepalived並修改/etc/keepalived/keepalived.conf配置檔案

systemctl  stop firewalld 
setenforce  0
yum install keepalived -y
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    notification_email {
         root@localhost
    }
    notification_email_from keepalived@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id 192.168.253.128
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 15
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.253.190/32 dev ens33
    }
}
virtual_server 192.168.253.190 80 {
    delay_loop 1
    lb_algo rr
    lb_kind DR
    persistence_timeout 1
    protocol TCP
    real_server 192.168.253.145 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200 
            }
            nb_get_retry 3
            delay_before_retry 1
            connect_timeout 1
        }
    }
real_server 192.168.253.129 80  {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200 
            }
            nb_get_retry 3
            delay_before_retry 1
            connect_timeout 1
        }
    }
}

第七步:192.168.253.158端安裝keepalived並修改/etc/keepalived/keepalived.conf配置檔案

systemctl  stop firewalld 
setenforce  0
yum install keepalived -y

[root@localhost ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
    notification_email {
         root@localhost
    }
    notification_email_from keepalived@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id 192.168.253.158
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 15
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.253.190/32 dev ens33
    }
}
virtual_server 192.168.253.190 80 {
    delay_loop 1
    lb_algo rr
    lb_kind DR
    persistence_timeout 1
    protocol TCP
    real_server 192.168.253.145 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200 
            }
            nb_get_retry 3
            delay_before_retry 1
            connect_timeout 1
        }
    }
real_server 192.168.253.129 80  {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200 
            }
            nb_get_retry 3
            delay_before_retry 1
            connect_timeout 1
        }
    }
}

第八步:測試
啟動2臺keepalived

systemctl start keepalived

這裡寫圖片描述
可以看到ipvs規則已經產生了。以及vip在192.168.253.128上

然後我們curl一下192.168.253.190
這裡寫圖片描述

我們使用的排程方法是輪詢這裡一直是192.168.253.128
解決方法:將配置檔案/etc/keepalived/keepalived.conf的persistence_timeout 1這一項刪掉,這一項是表示持久連結
然後重啟2個keepalived

systemctl restart keepalived

這裡寫圖片描述

lvs測試沒問題。這時候試試高可用,我們把192.168.253.128這個主機的keepalived down掉

systemctl stop keepalived

這時候再繼續訪問192.168.253.190
這裡寫圖片描述
同時也可以看到192.168.253.190這個vip飄到192.168.253.158這個BACKUP節點上了。ipvsadm的規則也新增完成。
這裡寫圖片描述

相關文章