網(wǎng)站首頁 編程語言 正文
一、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
相關(guān)推薦
- 2022-05-08 jquery實(shí)現(xiàn)淘寶詳情頁選擇套餐_jquery
- 2022-09-02 ORACLE正則匹配查詢LIKE查詢多個(gè)值檢索數(shù)據(jù)庫對(duì)象_oracle
- 2022-06-15 C#實(shí)現(xiàn)歸并排序_C#教程
- 2022-12-28 C++關(guān)鍵字const使用方法詳解_C 語言
- 2023-01-26 Android?源碼淺析RecyclerView?ItemAnimator_Android
- 2022-10-24 通過5個(gè)例子讓你學(xué)會(huì)Pandas中的字符串過濾_python
- 2022-03-26 在ASP.Net?Core應(yīng)用程序中使用Bootstrap4_實(shí)用技巧
- 2023-01-15 PyQt5+QtChart實(shí)現(xiàn)繪制極坐標(biāo)圖_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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錯(cuò)誤: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)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支