日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Docker容器Consul部署概述_docker

作者:羽翔青空 ? 更新時(shí)間: 2022-09-30 編程語言

一、Consul概述

template 模板(更新)
registrator(自動(dòng)發(fā)現(xiàn))
后端每構(gòu)建出一個(gè)容器,會(huì)向registrator進(jìn)行注冊(cè),控制consul 完成更新操作,consul會(huì)觸發(fā)consul template模板進(jìn)行熱更新
核心機(jī)制:consul :自動(dòng)發(fā)現(xiàn)、自動(dòng)更新,為容器提供服務(wù)(添加、刪除、生命周期)

二、Consul的特性

  • 支持健康檢查、允許存儲(chǔ)鍵值對(duì)
  • 基于Golong語言,可移植性強(qiáng)
  • 支持ACL訪問控

三、Consul的使用場景

Consul的應(yīng)用場景包括服務(wù)發(fā)現(xiàn)、服務(wù)隔離、服務(wù)配置:

  • 服務(wù)發(fā)現(xiàn)場景中consul作為注冊(cè)中心,服務(wù)地址被注冊(cè)到consul中以后,可以使用consul提供的dns、http接口查詢,consul支持health check。
  • 服務(wù)隔離場景中consul支持以服務(wù)為單位設(shè)置訪問策略,能同時(shí)支持經(jīng)典的平臺(tái)和新興的平臺(tái),支持tls證書分發(fā),service-to-service加密。
  • 服務(wù)配置場景中consul提供key-value數(shù)據(jù)存儲(chǔ)功能,并且能將變動(dòng)迅速地通知出去,借助Consul可以實(shí)現(xiàn)配置共享,需要讀取配置的服務(wù)可以從Consul中讀取到準(zhǔn)確的配置信息。
  • Consul可以幫助系統(tǒng)管理者更清晰的了解復(fù)雜系統(tǒng)內(nèi)部的系統(tǒng)架構(gòu),運(yùn)維人員可以將Consul看成一種監(jiān)控軟件,也可以看成一種資產(chǎn)(資源)管理系統(tǒng)。

四、搭建consul集群

建立Consul服務(wù)

每個(gè)提供服務(wù)的節(jié)點(diǎn)上都要部署和運(yùn)行Consul的agent

Consul agent有兩種運(yùn)行模式
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ù)自動(dòng)加入nginx集群:

1、安裝Gliderlabs/Registrator Gliderlabs/Registrator
可檢查容器運(yùn)行狀態(tài)自動(dòng)注冊(cè),還可注銷docker容器的服務(wù) 到服務(wù)配置中心
目前支持Consul、Etcd和SkyDNS2

在192.168.184.12節(jié)點(diǎn)上,執(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

真機(jī)訪問http://192.168.109.11:8500
此時(shí)應(yīng)該可以發(fā)現(xiàn)5個(gè)服務(wù)

(3)配置template模板自動(dòng)更新

Consul-Template是一個(gè)守護(hù)進(jìn)程,用于實(shí)時(shí)查詢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

再打開另一個(gè)終端

 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

原文鏈接:https://blog.csdn.net/qq1356059950/article/details/126145773

欄目分類
最近更新