Docker Consul概述以及集群環(huán)境搭建步驟(圖文詳解)
一、Docker consul概述
容器服務(wù)更新與發(fā)現(xiàn):先發(fā)現(xiàn)再更新,發(fā)現(xiàn)的是后端節(jié)點上容器的變化(registrator),更新的是nginx配置文件(agent)
registrator:是consul安插在docker容器里的眼線,用于監(jiān)聽監(jiān)控節(jié)點上容器的變化(增加或減少,或者宕機),一旦有變化會把這些信息告訴并注冊在consul server端(使用回調(diào)和協(xié)程的方式,所以它的延遲和資源消耗會很少),consul server發(fā)生一旦發(fā)生注冊列表的變化后,會把注冊的信息告訴agent
agent(代理):用來控制consul template模板,用template組件去和nginx.conf來進行對接,模板里全是變量,用變量的方式去加載后端由注冊到consul server端之后,server端會把信息告訴agent,agent和template進行對接,寫入template,template就有了鏡像,更新完之后會作為nginx.conf子配置文件被前端的nginx識別,consul agent會控制reload之后會識別nginx.conf配置文件中的變化,相當于識別后端的節(jié)點,就可以在地址池中動態(tài)調(diào)整自己后端資源。
Consul的特性
- 支持健康檢查、允許存儲鍵值對
- 基于Golong語言,可移植性強
- 支持ACL訪問控制
二、基于 nginx 與 consul 構(gòu)建自動發(fā)現(xiàn)即高可用的 Docker 服務(wù)架構(gòu)
1.項目需求
- 使用 Docker 將 Consul、Consul template、Registrator 與 Nginx 組成一個值得新人且可擴展的服務(wù)架構(gòu)
- 在這個架構(gòu)中添加或移除服務(wù)時,不需要重寫任何配置,也不需要重啟任何服務(wù),一切都能夠正常運行,以實現(xiàn)自動化運維
2.環(huán)境準備
主機 | IP 地址 | 需要安裝的軟件 |
---|---|---|
主節(jié)點 | 192.168.126.11 | docker-ce、consul、consul-template、nginx |
nginx | 192.168.126.12 | doker-ce |
3.部署步驟
#兩臺節(jié)點上都安裝 Docker-ce,記得關(guān)閉防火墻 systemctl stop firewalld && systemctl disable firewalld setenforce 0 && sed -i "s/SELINUX=*/SELINUX=disabled/g" /etc/selinux/config
①在主節(jié)點上部署consul
[root@xjj ~]# mkdir /consul [root@xjj ~]# cd /consul/ [root@xjj consul]# rz [root@xjj consul]# ls consul_0.9.2_linux_amd64.zip [root@xjj consul]# unzip consul_0.9.2_linux_amd64.zip -d /usr/bin/ Archive: consul_0.9.2_linux_amd64.zip inflating: /usr/bin/consul [root@xjj consul]# consul agent \ > -server \ > -bootstrap \ > -ui \ > -data-dir=/var/lib/consul-data \ > -bind=192.168.126.11 \ > -client=0.0.0.0 \ > -node=consul-server01 &> /var/log/consul.log & [1] 100683 [root@xjj consul]# jobs -l [1]+ 100683 運行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.126.11 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log & [root@xjj consul]# consul members Node Address Status Type Build Protocol DC consul-server01 192.168.126.11:8301 alive server 0.9.2 2 dc1 [root@xjj consul]# consul info|grep leader leader = true leader_addr = 192.168.126.11:8300 [root@xjj consul]# netstat -natp|grep 8500 tcp 0 0 127.0.0.1:34120 127.0.0.1:8500 TIME_WAIT - tcp 0 0 127.0.0.1:34118 127.0.0.1:8500 TIME_WAIT - tcp6 0 0 :::8500 :::* LISTEN 100683/consul
參數(shù)詳解
[root@xjj consul]# consul agent \ #設(shè)置代理 > -server \ #服務(wù)功能 > -bootstrap \ #參與選舉領(lǐng)袖 > -ui \ #提供web訪問界面 > -data-dir=/var/lib/consul-data \ #提供一個代理存儲數(shù)據(jù)目錄 > -bind=192.168.126.16 \ #綁定本機地址 > -client=0.0.0.0 \ #面對的客戶端地址(所有) > -node=consul-server01 &> /var/log/consul.log & #定義節(jié)點名稱,日志混合輸出到log,并且放到后臺運行 jobs -l #查看當前終端放入后臺的工作,并列出進程的PID號 consul members #查看群集信息 consul info|grep leader #查看管理信息,leader就是領(lǐng)袖 #可通過HTTP API獲取群集信息: curl 127.0.0.1:8500/v1/status/peers '//查看集群server成員' curl 127.0.0.1:8500/v1/status/leader '//集群Raf leader' curl 127.0.0.1:8500/v1/catalog/services '//注冊的所有服務(wù)' curl 127.0.0.1:8500/v1/catalog/nginx '//查看(nginx)服務(wù)信息' curl 127.0.0.1:8500/v1/catalog/nodes '//集群節(jié)點詳細信息'
②nginx 服務(wù)器連接 consul 并創(chuàng)建 nginx 容器服務(wù)
[root@localhost ~]# docker run -d \ > --name=registrator \ > --net=host \ > -v /var/run/docker.sock:/tmp/docker.sock \ > --restart=always \ > gliderlabs/registrator:latest \ > -ip=192.168.126.12 \ > consul://192.168.126.11:8500 ----- Digest: sha256:6e708681dd52e28f4f39d048ac75376c9a762c44b3d75b2824173f8364e52c10 Status: Downloaded newer image for gliderlabs/registrator:latest db4cb9d6a56ce8b9a2155b1113e48e6c974889cd4cca363b410c116b75be5d59 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES db4cb9d6a56c gliderlabs/registrator:latest "/bin/registrator -i…" 5 seconds ago Up 5 seconds registrator #創(chuàng)建容器,用來測試服務(wù)發(fā)現(xiàn)功能是否正常: [root@localhost ~]# docker run -itd -p 81:80 --name xjj01 -h xcf01 nginx #-h選項表示指定host主機名稱 [root@localhost ~]# docker run -itd -p 82:80 --name xjj02 -h xcf02 nginx [root@localhost ~]# docker run -itd -p 83:80 --name xjj03 -h xcf03 httpd [root@localhost ~]# docker run -itd -p 84:80 --name xjj0 -h xcf04 httpd [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8940a9b03dbb httpd "httpd-foreground" 4 seconds ago Up 3 seconds 0.0.0.0:84->80/tcp, :::84->80/tcp xjj0 9ac4d089eb14 httpd "httpd-foreground" 39 seconds ago Up 38 seconds 0.0.0.0:83->80/tcp, :::83->80/tcp xjj03 3d626fd61639 nginx "/docker-entrypoint.…" 25 minutes ago Created xjj02 263aa9deb346 nginx "/docker-entrypoint.…" 26 minutes ago Up 26 minutes 0.0.0.0:81->80/tcp, :::81->80/tcp xjj01 db4cb9d6a56c gliderlabs/registrator:latest "/bin/registrator -i…" 29 minutes ago Up 29 minutes registrator 瀏覽器訪問測試:`192.168.126.11:8500`
[root@xjj consul]# curl 127.0.0.1:8500/v1/catalog/services {"consul":[],"httpd":[],"nginx":[]}[root@xjj consul]#
③consul 群集添加 consul-template 以實現(xiàn)容器自動加入
Consul-Template是一個守護進程,用于實時查詢Consul集群信息,并更新文件系統(tǒng)上任意數(shù)量的指定模板,生成配置文件,更新完成以后,可以查詢Consul中的服務(wù)目錄,Key、Key-values等
cd consul/ vim nginx.ctmpl upstream http_backend { {{range service "nginx"}} server {{.Address}}:{{.Port}}; {{end}} } server { listen 100; server_name localhost 192.168.126.11; access_log /var/log/nginx/lic.com-access.log; index index.html index.php; location / { proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http_backend; } }
yum -y install gcc pcre-devel zlib-devel rz nginx-1.12.2.tar.gz tar zxvf nginx-1.12.2.tar.gz -C /opt cd /opt/nginx-1.12.10 ./configure --prefix=/usr/local/nginx make && make install -- vim /usr/local/nginx/conf/nginx.conf 第19 include vhost/*.conf; #19行添加,虛擬主機目錄 [root@xjj nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost '//創(chuàng)建虛擬主機目錄' [root@xjj nginx-1.12.2]# mkdir /var/log/nginx '//創(chuàng)建日志文件目錄' [root@xjj nginx-1.12.2]# /usr/local/nginx/sbin/nginx '//啟動nginx' [root@xjj nginx-1.12.2]# netstat -natp|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 104348/nginx: maste -- #啟動template,指定template模板文件及生成路徑: [root@xjj consul]# consul-template -consul-addr 192.168.126.16:8500 -template "/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/xjj.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info #指定模板路徑/consul/nginx.ctmpl,生成到/usr/locla/nginx/conf/vhost/xjj.conf,然后重載nginx -s reload,之后定義日志級別,進入監(jiān)控狀態(tài) -- #主節(jié)點打開新終端查看配置文件: [root@xjj ~]# cat /usr/local/nginx/conf/vhost/xjj.conf #訪問這個池子里面的Web頁面,得訪問192.168.126.11:8080,且是輪詢機制,這里若訪問不了,可以重載nginx再試試 upstream http_backend { server 192.168.126.12:81; server 192.168.126.12:82; } server { listen 8080; server_name localhost 192.168.126.11; access_log /var/log/nginx/xjj.cn-access.log; index index.html index.php; location / { proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http_backend; } }
④新增一個 nginx 容器節(jié)點以測試自動更新
[root@localhost ~]# docker run -itd -p 85:80 --name xjj05 -h xcf05 nginx #增加一個nginx容器節(jié)點,測試服務(wù)發(fā)現(xiàn)及配置更新功能 [root@xjj ~]# cat /usr/local/nginx/conf/vhost/xcf.conf upstream http_backend { server 192.168.126.12:81; server 192.168.126.12:82; server 192.168.126.12:85; } server { listen 8080; server_name localhost 192.168.126.11; access_log /var/log/nginx/xcf.cn-access.log; index index.html index.php; location / { proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
⑤測試訪問代理服務(wù)器 是否可以完成代理訪問輪詢
http://192.168.126.11:80/ docker logs -f xjj01 docker logs -f xjj02 docker logs -f xjj05
⑥consul 多節(jié)點配置
#添加一臺已有docker環(huán)境的服務(wù)器加入已有的群集中: consul agent \ -server \ -bootstrap \ -ui \ -data-dir=/var/ib/consul-data \ -bind=192.168.126.11 \ -client=0.0.0.0 \ -node=consul-server02 \ -enable-script-checks=true \ -datacenter=dc1 \ -join 192.168.126.15 &> /var/log/consul.log & --解釋-- -enable-script-checks=true: 設(shè)置檢查服務(wù)為可用 -datacenter: 數(shù)據(jù)中心名稱 -join: 加入到已有的集群中
到此這篇關(guān)于Docker-Consul概述以及集群環(huán)境搭建步驟(圖文詳解)的文章就介紹到這了,更多相關(guān)Docker-Consul 集群環(huán)境搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在docker容器中調(diào)用和執(zhí)行宿主機的docker操作
這篇文章主要介紹了在docker容器中調(diào)用和執(zhí)行宿主機的docker操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11spring-boot構(gòu)建docker鏡像上傳倉庫的示例教程
這篇文章主要介紹了spring-boot構(gòu)建docker鏡像上傳倉庫,受限創(chuàng)建一個簡單spring-boot-web項目,查看鏡像上傳倉庫這時候有兩種解決方案,對docker鏡像上傳倉庫相關(guān)知識感興趣的朋友跟隨小編一起看看吧2022-12-12如何在centos的docker里安裝jupyter并開放端口
上次有一朋友問小編如何在centos的docker里安裝jupyter并開放端口呢?在這就不一一回復(fù)大家了,下面小編把我的個人經(jīng)驗分享到腳本之家平臺,感興趣的朋友一起看看吧2021-08-08基于docker-compose構(gòu)建Mongodb副本集的示例詳解
副本集是?MongoDB?高可用性和數(shù)據(jù)安全性策略的基礎(chǔ),適用于對數(shù)據(jù)安全性和服務(wù)可用性有較高要求的場景,本文給大家介紹了如何基于docker-compose構(gòu)建Mongodb副本集,文中通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2024-01-01