Consul Template 提供一個方便的方式從Consul服務獲取數據經過consul-template的後臺程序保存到文件系統,這個後臺進程監控Consul中數據的變化並更新任意數量的模板
到文件系統。模板更新完成後consul-template也能夠觸發相關的指令或者腳本,此處經過簡單的實踐動態更新Nginx的upstream server
而且觸發reload
nginx服務。固然對於小規模下的應用場景還有不少,好比Haproxy
配置文件動態更新等。html
這裏小小的作個調查,你使用過如下那種方案:node
下面,咱們就開始今天的實踐目的吧,經過consul-template與consul完成nginx配置文件的自動更新:linux
Docker部署Consul集羣
實驗版本信息:nginx
軟件 | 版本 |
---|---|
主機 | CentOS Linux 7 (Core) |
內核 | 3.10.0-1160.15.2.el7.x86_64 |
docker | 20.10.4 |
Consul鏡像 | consul:latest -> 1.9.1 |
Consul-template | 0.20.0 |
準備consul集羣,你也可使用單獨的一個consul實例作基礎環境的支撐,能夠參考經過Docker一鍵部署consul集羣web
安裝consul-template
以及基礎環境
此處,安裝下consul-template,而後運行一個nginx容器,而且把nginx配置文件掛載到宿主機上:docker
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
準備一個腳本文件,當consul-template從consul中獲取到數據的更新後,觸發該腳本運行nginx_operator.sh
,實現reload
nginx的配置文件。json
#!/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
接下來準備consul-template運行的配置文件nginx.hcl
,裏面經過source
指定了咱們要使用的模板文件,以及destination
指定根據模板渲染後生成的配置文件存儲的路徑位置,最後一個command
就是當完成一次渲染後,要觸發的動做,這裏是一個腳本,就是會觸發該腳本的內容。後端
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"
}
而後,須要準備一下consul-template要監聽的模板文件backend_nginx.ctmpl
,consul-template能夠同時指定多個模板文件,以下所示:瀏覽器
consul-template \
-consul $consul_cluster_address:8500 \
-template "$template1:$file1:$command1" \
-template "$template2:$file2:$comand2" \
-template "$template3:$file3"
此處只用一個模板作一下測試,模板的中的語法是go template
語法,這裏實現的比較簡單,只是作了upstream.server
的渲染,對於實際使用來講,能夠把模板文件中的nginx1
與server_name
渲染的數據也存在consul集羣中,這樣也是能夠渲染的。bash
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;
}
}
在這個實驗中,consul-template經過consul拿到變化後的數據,將數據成功的渲染到配置文件以後,咱們能夠經過www.kubeamster.top
能正常訪問upstream後的服務,因此還須要準備一個上游服務,這裏直接使用docker快速的運行一個便可
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
此時,咱們就能夠把consul-template運行起來,做爲後端進程運行着:
[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
...
最後咱們經過curl
或者postman
把運行的nginx1
服務往consul集羣中註冊一下
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}}'
經過hostctl增長一條本地DNS解析
最後,咱們經過瀏覽器訪問一下http://www.kubemaster.top
查看返回的內容就是nginx1的服務。這說明consul-template按照預期完成了工做。
到這裏,基本上完成了使用consul-template與consul實現nginx配置文件的自動更新。
本文分享自微信公衆號 - 雲原生生態圈(CloudNativeEcoSystem)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。