網(wǎng)站首頁 編程語言 正文
一、Docker consul概述
容器服務(wù)更新與發(fā)現(xiàn):先發(fā)現(xiàn)再更新,發(fā)現(xiàn)的是后端節(jié)點(diǎn)上容器的變化(registrator),更新的是nginx配置文件(agent)
registrator:是consul安插在docker容器里的眼線,用于監(jiān)聽監(jiān)控節(jié)點(diǎn)上容器的變化(增加或減少,或者宕機(jī)),一旦有變化會把這些信息告訴并注冊在consul server端(使用回調(diào)和協(xié)程的方式,所以它的延遲和資源消耗會很少),consul server發(fā)生一旦發(fā)生注冊列表的變化后,會把注冊的信息告訴agent
agent(代理):用來控制consul template模板,用template組件去和nginx.conf來進(jìn)行對接,模板里全是變量,用變量的方式去加載后端由注冊到consul server端之后,server端會把信息告訴agent,agent和template進(jìn)行對接,寫入template,template就有了鏡像,更新完之后會作為nginx.conf子配置文件被前端的nginx識別,consul agent會控制reload之后會識別nginx.conf配置文件中的變化,相當(dāng)于識別后端的節(jié)點(diǎn),就可以在地址池中動態(tài)調(diào)整自己后端資源。
Consul的特性
- 支持健康檢查、允許存儲鍵值對
- 基于Golong語言,可移植性強(qiáng)
- 支持ACL訪問控制
二、基于 nginx 與 consul 構(gòu)建自動發(fā)現(xiàn)即高可用的 Docker 服務(wù)架構(gòu)
1.項(xiàng)目需求
- 使用 Docker 將 Consul、Consul template、Registrator 與 Nginx 組成一個值得新人且可擴(kuò)展的服務(wù)架構(gòu)
- 在這個架構(gòu)中添加或移除服務(wù)時,不需要重寫任何配置,也不需要重啟任何服務(wù),一切都能夠正常運(yùn)行,以實(shí)現(xiàn)自動化運(yùn)維
2.環(huán)境準(zhǔn)備
主機(jī) | IP 地址 | 需要安裝的軟件 |
---|---|---|
主節(jié)點(diǎn) | 192.168.126.11 | docker-ce、consul、consul-template、nginx |
nginx | 192.168.126.12 | doker-ce |
3.部署步驟
#兩臺節(jié)點(diǎn)上都安裝 Docker-ce,記得關(guān)閉防火墻 systemctl stop firewalld && systemctl disable firewalld setenforce 0 && sed -i "s/SELINUX=*/SELINUX=disabled/g" /etc/selinux/config
①在主節(jié)點(diǎn)上部署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 運(yùn)行中 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 \ #綁定本機(jī)地址 > -client=0.0.0.0 \ #面對的客戶端地址(所有) > -node=consul-server01 &> /var/log/consul.log & #定義節(jié)點(diǎn)名稱,日志混合輸出到log,并且放到后臺運(yùn)行 jobs -l #查看當(dāng)前終端放入后臺的工作,并列出進(jìn)程的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é)點(diǎn)詳細(xì)信息'
②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選項(xiàng)表示指定host主機(jī)名稱 [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 以實(shí)現(xiàn)容器自動加入
Consul-Template是一個守護(hù)進(jìn)程,用于實(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.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行添加,虛擬主機(jī)目錄 [root@xjj nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost '//創(chuàng)建虛擬主機(jī)目錄' [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,之后定義日志級別,進(jìn)入監(jiān)控狀態(tài) -- #主節(jié)點(diǎn)打開新終端查看配置文件: [root@xjj ~]# cat /usr/local/nginx/conf/vhost/xjj.conf #訪問這個池子里面的Web頁面,得訪問192.168.126.11:8080,且是輪詢機(jī)制,這里若訪問不了,可以重載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é)點(diǎn)以測試自動更新
[root@localhost ~]# docker run -itd -p 85:80 --name xjj05 -h xcf05 nginx #增加一個nginx容器節(jié)點(diǎn),測試服務(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é)點(diǎn)配置
#添加一臺已有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: 加入到已有的集群中
原文鏈接:https://blog.csdn.net/weixin_53560205/article/details/121738920
相關(guān)推薦
- 2022-06-16 zap接收gin框架默認(rèn)的日志并配置日志歸檔示例_Golang
- 2021-12-02 Android?NDK開發(fā)(C語言--動態(tài)內(nèi)存分配)_Android
- 2022-09-13 關(guān)于c語言中輸出字符指針的相關(guān)問題_C 語言
- 2023-01-05 Flutter?Widget之NavigationBar使用詳解_Android
- 2022-06-17 C語言深入講解棧與堆和靜態(tài)存儲區(qū)的使用_C 語言
- 2022-05-02 C/C++的文件IO函數(shù)你知道嗎_C 語言
- 2023-07-03 Docker之容器導(dǎo)出為鏡像問題_docker
- 2022-11-05 關(guān)于Python?Tkinter?復(fù)選框?->Checkbutton_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支