久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

nginx

共計(jì) 3863 個(gè)字符,預(yù)計(jì)需要花費(fèi) 10 分鐘才能閱讀完成。

這篇文章將為大家詳細(xì)講解有關(guān) nginx-template 如何實(shí)現(xiàn)動(dòng)態(tài)更新 Nginx upstream,文章內(nèi)容質(zhì)量較高,因此丸趣 TV 小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

Consul Template 提供一個(gè)方便的方式從 Consul 服務(wù)獲取數(shù)據(jù)通過(guò) consul-template 的后臺(tái)程序保存到文件系統(tǒng),這個(gè)后臺(tái)進(jìn)程監(jiān)控 Consul 中數(shù)據(jù)的變化并更新任意數(shù)量的模板到文件系統(tǒng)。模板更新完成后 consul-template 也可以觸發(fā)相關(guān)的指令或者腳本,此處通過(guò)簡(jiǎn)單的實(shí)踐動(dòng)態(tài)更新 Nginx 的 upstream server 并且觸發(fā) reloadnginx 服務(wù)。當(dāng)然對(duì)于小規(guī)模下的應(yīng)用場(chǎng)景還有很多,比如 Haproxy 配置文件動(dòng)態(tài)更新等。

這里小小的做個(gè)調(diào)查,你使用過(guò)以下那種方案:

下面,我們就開(kāi)始今天的實(shí)踐目的吧,通過(guò) consul-template 與 consul 完成 nginx 配置文件的自動(dòng)更新:

Docker 部署 Consul 集群

實(shí)驗(yàn)版本信息:

軟件版本主機(jī) CentOS Linux 7 (Core) 內(nèi)核 3.10.0-1160.15.2.el7.x86_64docker20.10.4Consul 鏡像 consul:latest – 1.9.1Consul-template0.20.0

準(zhǔn)備 consul 集群,你也可以使用單獨(dú)的一個(gè) consul 實(shí)例做基礎(chǔ)環(huán)境的支撐,可以參考通過(guò) Docker 一鍵部署 consul 集群

nginx
 Docker 一鍵部署 Consul 集群原文   安裝 consul-template 以及基礎(chǔ)環(huán)境

此處,安裝下 consul-template, 然后運(yùn)行一個(gè) nginx 容器,并且把 nginx 配置文件掛載到宿主機(jī)上:

wget https://releases.hashicorp.com/consul-template/0.20.0/consul-template_0.20.0_linux_amd64.zip
unzip consul-template_0.20.0_linux_amd64.zip
chmod +x consul-template
mv consul-template /usr/local/bin/
docker run -d --name nginx --restart=always -p 80:80 nginx:1.18
docker cp nginx:/etc/nginx /tmp/nginx
docker stop nginx   docker rm nginx
docker run -d --name nginx --restart=always -p 80:80 -v /tmp/nginx:/etc/nginx nginx:1.18

 

準(zhǔn)備一個(gè)腳本文件,當(dāng) consul-template 從 consul 中獲取到數(shù)據(jù)的更新后,觸發(fā)該腳本運(yùn)行 nginx_operator.sh,實(shí)現(xiàn) reloadnginx 的配置文件。

#!/bin/sh
docker ps -aq --filter  name=nginx
if [ $? -ne 0 ]; then
 echo  Starting Nginx Server...
 docker run -d --name nginx --restart=always -p 80:80 -v /tmp/nginx:/etc/nginx nginx:1.18
 echo  nginx start done.
else
 echo  Reloading nginx...
 docker exec -it nginx nginx -s reload;
 echo  nginx reloading done.
fi

 

接下來(lái)準(zhǔn)備 consul-template 運(yùn)行的配置文件 nginx.hcl,里面通過(guò) source 指定了我們要使用的模板文件,以及 destination 指定根據(jù)模板渲染后生成的配置文件存儲(chǔ)的路徑位置,最后一個(gè) command 就是當(dāng)完成一次渲染后,要觸發(fā)的動(dòng)作,這里是一個(gè)腳本,就是會(huì)觸發(fā)該腳本的內(nèi)容。

wait {
 min =  3s
 max =  9s
}

syslog {
 enabled = true
 facility =  LOCAL5
}

consul {
 address =  192.168.99.3:8500
}

template {
 source =  /tmp/nginx/conf.d/consul/backend_nginx.ctmpl
 destination =  /tmp/nginx/conf.d/consul/nginx01.conf
 command =  /root/start_nginx.sh
}

 

然后,需要準(zhǔn)備一下 consul-template 要監(jiān)聽(tīng)的模板文件 backend_nginx.ctmpl,consul-template 可以同時(shí)指定多個(gè)模板文件,如下所示:

consul-template \
 -consul $consul_cluster_address:8500 \
 -template  $template1:$file1:$command1  \
 -template  $template2:$file2:$comand2  \
 -template  $template3:$file3

 

此處只用一個(gè)模板做一下測(cè)試,模板的中的語(yǔ)法是 go template 語(yǔ)法,這里實(shí)現(xiàn)的比較簡(jiǎn)單,只是做了 upstream.server 的渲染,對(duì)于實(shí)際使用來(lái)說(shuō),可以把模板文件中的 nginx1 與 server_name 渲染的數(shù)據(jù)也存在 consul 集群中,這樣也是可以渲染的。

upstream nginx1 {
 server 127.0.0.1:2000 down;
 {{ range service  local.www@dc1  }}
 server {{.Address}}:{{.Port}} weight=1;
 {{end}}
}

server {
 listen 80;
 server_name www.kubemaster.top;
 location / {
 proxy_pass http://nginx1;
 }
}

 

在這個(gè)實(shí)驗(yàn)中,consul-template 通過(guò) consul 拿到變化后的數(shù)據(jù),將數(shù)據(jù)成功的渲染到配置文件之后,我們可以通過(guò) www.kubeamster.top 能正常訪(fǎng)問(wèn) upstream 后的服務(wù),所以還需要準(zhǔn)備一個(gè)上游服務(wù),這里直接使用 docker 快速的運(yùn)行一個(gè)即可

docker run -d --name nginx1 -p 8081:80 nginx:1.18
docker exec -it nginx1 bash
echo  backend server for consul-template    /usr/share/nginx/html/index.html

 

此時(shí),我們就可以把 consul-template 運(yùn)行起來(lái),作為后端進(jìn)程運(yùn)行著:

[root@node1 consul]# consul-template -config  nginx.hcl  --log-level=info
2021/03/04 19:25:21 [DEBUG] (logging) enabling syslog on LOCAL5
2021/03/04 11:25:21.098622 [INFO] consul-template v0.20.0 (b709612c)
2021/03/04 11:25:21.098767 [INFO] (runner) creating new runner (dry: false, once: false)
2021/03/04 11:25:21.099244 [INFO] (runner) creating watcher
2021/03/04 11:25:21.101210 [INFO] (runner) starting
...

 

最后我們通過(guò) curl 或者 postman 把運(yùn)行的 nginx1 服務(wù)往 consul 集群中注冊(cè)一下

curl --location --request PUT  http://192.168.99.3:8500/v1/catalog/register  \
--header  Content-Type: application/json  \
--data-raw  {Datacenter :  dc1 , Node :  721d401b9555 , Address :  192.168.99.3 , Service : {  Id :  172.17.0.12:80 ,  Service :  www , tags : [  local  ], Port : 8081}}

 

通過(guò) hostctl 增加一條本地 DNS 解析

nginx
  通過(guò) hostctl 增加一條本地 DNS 解析

最后,我們通過(guò)瀏覽器訪(fǎng)問(wèn)一下 http://www.kubemaster.top 查看返回的內(nèi)容就是 nginx1 的服務(wù)。這說(shuō)明 consul-template 按照預(yù)期完成了工作。

nginx
 Access Backend server by consul-template

到這里,基本上完成了使用 consul-template 與 consul 實(shí)現(xiàn) nginx 配置文件的自動(dòng)更新。

關(guān)于 nginx-template 如何實(shí)現(xiàn)動(dòng)態(tài)更新 Nginx upstream 就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-08-25發(fā)表,共計(jì)3863字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒(méi)有評(píng)論)
主站蜘蛛池模板: 彰武县| 呼和浩特市| 高碑店市| 柳河县| 车险| 科技| 贺兰县| 彭阳县| 海南省| 绥江县| 恭城| 三穗县| 宾阳县| 盘山县| 吉水县| 左权县| 武冈市| 汉阴县| 建阳市| 资兴市| 乌恰县| 峨边| 二连浩特市| 安龙县| 长宁县| 天祝| 陕西省| 潮安县| 澄迈县| 鹰潭市| 泰兴市| 乌兰浩特市| 营山县| 永春县| 土默特右旗| 吴忠市| 平和县| 凌云县| 建湖县| 喜德县| 盱眙县|