您好,登錄后才能下訂單哦!
如何在Docker中使用keepalived+nginx實現主從熱備?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
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 updateyum install -y vimyum install -y wgetyum install -y gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl--develyum install -y popt-develyum 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 作者:江湖救急 鏈接:https://juejin.im/post/5dc517386fb9a04a9272110b 來源:掘金 著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
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.開始編譯keepalivedcd /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
5.make一下 make && make install
9,將keepalived 安裝成系統服務
mkdir /etc/keepalivedcp /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/3
curl 172.17.0.100
B> 此時停止centos_master容器( docker stop centos_master ),保留centos_slave容器,執行以下命令,若切換到Slave頁面,則說明keepalived配置成功2/3
curl 172.17.0.100
C> 重啟centos_master 容器,此時執行以下命令,看是從Slave切換到了Master,如果切換成功,說明我們配置到此成功了。
curl 172.17.0.100
說明,測試過程中,重啟容器之后,nginx沒有啟動,需要進入容器啟動一下,不然訪問不到Master頁面了,但是可以Ping通。
執行下面命令,配置nginx隨機啟動,這樣不用每次重啟容器還需要手動啟動nginx
chkconfig nginx on
關于如何在Docker中使用keepalived+nginx實現主從熱備問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。