Docker容器Consul部署概述
一、Consul概述
template 模板(更新)
registrator(自動發(fā)現(xiàn))
后端每構(gòu)建出一個容器,會向registrator進行注冊,控制consul 完成更新操作,consul會觸發(fā)consul template模板進行熱更新
核心機制:consul :自動發(fā)現(xiàn)、自動更新,為容器提供服務(wù)(添加、刪除、生命周期)
二、Consul的特性
- 支持健康檢查、允許存儲鍵值對
- 基于Golong語言,可移植性強
- 支持ACL訪問控
三、Consul的使用場景
Consul的應(yīng)用場景包括服務(wù)發(fā)現(xiàn)、服務(wù)隔離、服務(wù)配置:
- 服務(wù)發(fā)現(xiàn)場景中consul作為注冊中心,服務(wù)地址被注冊到consul中以后,可以使用consul提供的dns、http接口查詢,consul支持health check。
- 服務(wù)隔離場景中consul支持以服務(wù)為單位設(shè)置訪問策略,能同時支持經(jīng)典的平臺和新興的平臺,支持tls證書分發(fā),service-to-service加密。
- 服務(wù)配置場景中consul提供key-value數(shù)據(jù)存儲功能,并且能將變動迅速地通知出去,借助Consul可以實現(xiàn)配置共享,需要讀取配置的服務(wù)可以從Consul中讀取到準(zhǔn)確的配置信息。
- Consul可以幫助系統(tǒng)管理者更清晰的了解復(fù)雜系統(tǒng)內(nèi)部的系統(tǒng)架構(gòu),運維人員可以將Consul看成一種監(jiān)控軟件,也可以看成一種資產(chǎn)(資源)管理系統(tǒng)。
四、搭建consul集群
建立Consul服務(wù)
每個提供服務(wù)的節(jié)點上都要部署和運行Consul的agent
Consul agent有兩種運行模式
Server
Client
Server和Client只是Consul集群層面的區(qū)分,與搭建在Cluster之上的應(yīng)用服務(wù)無關(guān)
(1)server部署
mkdir /root/consul cd consul rz consul_0.9.2_linux_amd64.zip unzip consul_0.9.2_linux_amd64.zip mv consul /usr/bin
consul agent \ -server \ -bootstrap \ -ui \ -data-dir=/var/lib/consul-data \ -bind=192.168.109.11 \ -client=0.0.0.0 \ -node=consul-server01 &> /var/log/consul.log & consul members consul info | grep leader
(2)client部署
容器服務(wù)自動加入nginx集群:
1、安裝Gliderlabs/Registrator Gliderlabs/Registrator
可檢查容器運行狀態(tài)自動注冊,還可注銷docker容器的服務(wù) 到服務(wù)配置中心
目前支持Consul、Etcd和SkyDNS2
在192.168.184.12節(jié)點上,執(zhí)行以下操作:
docker run -d \ --name=registrator \ --net=host \ -v /var/run/docker.sock:/tmp/docker.sock \ --restart=always \ gliderlabs/registrator:latest \ -ip=192.168.109.12 \ consul://192.168.109.11:8500
systemctl restart docker docker run -itd -p:81:80 --name test-01 -h test01 nginx docker run -itd -p:82:80 --name test-02 -h test02 nginx docker run -itd -p:83:80 --name test-03 -h test03 httpd docker run -itd -p:84:80 --name test-04 -h test04 httpd
真機訪問http://192.168.109.11:8500
此時應(yīng)該可以發(fā)現(xiàn)5個服務(wù)
(3)配置template模板自動更新
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.109.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.0.tar.gz tar zxvf nginx-1.12.0.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;
cd /usr/local/nginx/conf/ mkdir vhost mkdir /var/log/nginx /usr/local/nginx/sbin/nginx cd /opt rz consul-template_0.19.3_linux_amd64.zip unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin consul-template -consul-addr 192.168.109.11:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/lic.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
再打開另一個終端
docker run -itd -p:85:80 --name test-05 -h test05 nginx
(4)測試訪問代理服務(wù)器
http://192.168.184.11:100/ docker logs -f test-01 docker logs -f test-02 docker logs -f test-05
到此這篇關(guān)于Docker容器Consul部署概述的文章就介紹到這了,更多相關(guān)Docker Consul部署內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows?Server?2012?R2?安裝?Docker的詳細步驟
這篇文章主要介紹了Windows?Server?2012?R2?安裝?Docker,在這個給大家說明下使用windows10、Windows?Server?2016以上系統(tǒng)可直接使用安裝包,低版本系統(tǒng)需要使用?Docker?Toolbox?來進行安裝使用?Docker,需要的朋友可以參考下2022-04-04