Docker+keepalived+nginx實現主從熱備

前言

爲解決單點故障,咱們須要配置主從熱備方案,服務器數量有限,故使用Docker模擬安裝配置。html

本次配置默認已經安裝了Docker。nginx

配置環境:centos7 64位c++

docker版本:Docker version 17.12.1-ce, build 7390fc6docker



1,拉取centos7鏡像

docker pull centos:7複製代碼

2,建立容器

docker run -it -d --name centos1 -d centos:7複製代碼

3,進入容器centos1

docker exec -it centos1 bash
複製代碼

4,安裝經常使用工具

yum update
yum install -y vim
yum install -y wget
yum install -y  gcc-c++  
yum install -y pcre pcre-devel  
yum install -y zlib zlib-devel  
yum install -y  openssl-devel
yum install -y popt-devel
yum install -y initscripts
yum install -y net-tools
複製代碼

5,將容器打包成新的鏡像,之後直接以該鏡像建立容器

docker commit -a 'cfh' -m 'centos with common tools' centos1 centos_base
複製代碼

6,刪除以前建立的centos1 容器,從新以基礎鏡像建立容器,安裝keepalived+nginx

docker rm -f centos1
#容器內須要使用systemctl服務,須要加上/usr/sbin/init
docker run -it --name centos_temp -d --privileged centos_base /usr/sbin/init
docker exec -it centos_temp bash複製代碼

7,安裝nginx

#使用yum安裝nginx須要包括Nginx的庫,安裝Nginx的庫
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 使用下面命令安裝nginx
yum install -y nginx
#啓動nginx
systemctl start nginx.service
#查看是否啓動成功,出現nginx歡迎界面表示安裝成功
curl 172.17.0.2


複製代碼

8,安裝keepalived

1.下載keepalived
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz

2.解壓安裝:
tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/

3.下載插件openssl

yum install -y openssl openssl-devel(須要安裝一個軟件包)

4.開始編譯keepalived 
cd  /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived

5.make一下
make && make install
複製代碼

9,將keepalived 安裝成系統服務

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf  /etc/keepalived/
而後複製keepalived腳本文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
能夠設置開機啓動:chkconfig keepalived on
到此咱們安裝完畢!

#若啓動報錯,則執行下面命令
cd /usr/sbin/ 
rm -f keepalived  
cp /usr/local/keepalived/sbin/keepalived  /usr/sbin/ 

#經常使用命令
systemctl daemon-reload  從新加載
systemctl enable keepalived.service  設置開機自動啓動
systemctl disable keepalived.service 取消開機自動啓動
systemctl start keepalived.service 啓動
systemctl stop keepalived.service中止
systemctl status keepalived.service  查看服務狀態
複製代碼

10,修改/etc/keepalived/keepalived.conf文件

#備份配置文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.backup

rm -f keepalived.conf
vim keepalived.conf
#配置文件以下

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 121
    mcast_src_ip 172.17.0.6
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        chk_nginx
    }

    virtual_ipaddress {
        172.17.0.100
    }
}

複製代碼

11,添加心跳檢測文件

vim nginx_check.sh
#如下是腳本內容
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi
複製代碼

12,給腳本賦予執行權限

chmod +x nginx_check.sh複製代碼

13,設置開機啓動

systemctl enable keepalived.service

#開啓keepalived
systemctl start keepalived.service

複製代碼

14,檢測虛擬IP是否成功,在宿主機裏面執行下面命令,若是出現nginx歡迎界面表示成功

curl 172.17.0.100
複製代碼


15,將centos_temp 容器從新打包成鏡像,而後利用這個新鏡像再建立兩個容器,實現熱備效果

docker commit -a 'cfh' -m 'centos with keepalived nginx' centos_temp centos_kn
複製代碼

16,刪除全部容器

docker rm -f `docker ps -a -q`
複製代碼

17,用centos_kn 鏡像建立主服務器容器

docker run --privileged  -tid --name centos_master --restart=always  centos_kn /usr/sbin/init

docker exec -it centos_master bash
複製代碼

18,修改centos_master裏面nginx 歡迎頁,

vim /usr/share/nginx/html/index.html
複製代碼


19,建立從服務器容器

docker run --privileged  -tid --name centos_slave --restart=always  centos_kn /usr/sbin/init
docker exec -it centos_slave bash

#修改keepalived.conf 配置文件,主要是state和priority兩個參數的調整,其中master節點的priority值必定要比slave大才行

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state SLAVE
    interface eth0
    virtual_router_id 121
    mcast_src_ip 172.17.0.6
    priority 80
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        chk_nginx
    }

    virtual_ipaddress {
        172.17.0.100
    }
}



複製代碼

20,修改完成以後從新加載

systemctl daemon-reload
systemctl restart keepalived.service複製代碼

21,修改nginx歡迎頁(若nginx沒啓動則執行 systemctl start nginx.service)

vim /usr/share/nginx/html/index.html
複製代碼



22,測試

A> 分別在宿主機,centos_master,centos_slave中進行一下命令測試,若是顯示都爲Master的歡迎頁面,說明配置成功1/3vim

curl 172.17.0.100
複製代碼

B> 此時中止centos_master容器(docker stop centos_master),保留centos_slave容器,執行如下命令,若切換到Slave頁面,則說明keepalived配置成功2/3centos

curl 172.17.0.100
複製代碼

C> 重啓centos_master 容器,此時執行如下命令,看是從Slave切換到了Master,若是切換成功,說明咱們配置到此成功了。bash

curl 172.17.0.100
複製代碼

說明,測試過程當中,重啓容器以後,nginx沒有啓動,須要進入容器啓動一下,否則訪問不到Master頁面了,可是能夠Ping通。服務器

執行下面命令,配置nginx隨機啓動,這樣不用每次重啓容器還須要手動啓動nginxcurl

chkconfig nginx on
複製代碼

以上就是整個配置過程工具

相關文章
相關標籤/搜索