consul-template 是 Consul 的一個守護程序,使用 consul-template 能夠方便快速的實現對 Consul Key/Value 存儲系統的訪問,能夠從 KV 系統中讀取數據、監視變更、同步本地文件;還能夠在執行模板更新的同時,執行本地系統命令,好比常見的就是在同步數據到本地模板文件後,生成 Nginx 配置文件,並執行 nginx -s reload 命令,使 Nginx 從新加載配置文件。html
參考文檔:
https://www.google.com
https://github.com/hashicorp/consul-template
https://www.consul.io
https://www.hi-linux.com/posts/36431.htmllinux
執行如下命令nginx
// 添加源 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm // 執行安裝 yum install -y nginx // 修改 nginx 默認端口爲 8010 vi /etc/nginx/conf.d/default.conf // 加入開機啓動 systemctl enable nginx.service // 啓動 nginx systemctl start nginx.service
mkdir -p /etc/nginx/conf.d/consul
編輯 nginx.conf 文件,將上面的目錄 /etc/nginx/conf.d/consul 做爲配置文件目錄導入到 nginx 中github
vi /etc/nginx/nginx.conf // 將下面這句代碼加入到nginx倒數第二行 include /etc/nginx/consul/*.conf
由於 consul-template 只有一個二進制包,因此其安裝過程很是的簡單;首先是下載二進制包編程
wget https://releases.hashicorp.com/consul-template/0.20.0/consul-template_0.20.0_linux_amd64.tgz
解壓到當前目錄centos
tar -zxf consul-template_0.20.0_linux_amd64.tgz
移動到 consul 相同目錄下瀏覽器
mkdir /usr/local/bin/consul-template mv consul-template /usr/local/bin/consul-template
設置環境變量,輸入命令:vi /etc/profile安全
// 將這裏的代碼添加到 profile 文件末尾 export CONSUL_TEMPLATE_HOME=/usr/local/bin/consul-template export PATH=$PATH:$CONSUL_HOME:$CONSUL_TEMPLATE_HOME // 執行命令使環境變量生效 source /etc/profile
爲了得到變動自動通知,咱們呢須要將 consul-template 模板文件添加到 Consul 的 Key/Value 存儲系統中服務器
代碼格式選擇 HCL,會自動高亮喔。關於 consul-template 的語法,請參考:https://github.com/hashicorp/consul-template,爲了方便,我直接複製了官方的 examples/nginx.md 文件內容
回到 consul-template 服務器上的目錄 /usr/local/bin/consul-template ,建立一個 find_adress.tpl 文件內容
// 建立文件 vi find_adress.tpl // 輸入如下內容 {{ key "hashicorp/street_address" }} // 保存退出
// 執行命令 consul-template --template "find_address.tpl:hashicorp_address.tpl" -once
好了,準備工做進行到這裏,已基本結束;通過這麼長時間的命令執行,相信不少同窗已經把網頁關閉了,可是沒辦法,這這些事情仍是得一步一步的來。
終於能夠啓動了 consul-template 了,激動人心哪,consul-template 提供兩種啓動方式,命令行參數/加載配置文件 方式啓動,就便利性來講,仍是推薦「加載配置文件」方式啓動,一次配置,終身有效嘛。
consul-template --consul-addr 172.16.1.218:8500 --template "hashicorp_address.tpl:/etc/nginx/consul/vhost.conf:/usr/sbin/nginx -s reload" --log-level=info
上面的代碼參數這裏作簡單的解釋
編寫 hashicorp 的 nginx 配置文件
wait { min = "3s" max = "9s" } syslog { enabled = true facility = "LOCAL5" } consul { address = "172.16.1.218:8500" } template { source = "hashicorp_address.tpl" destination = "/etc/nginx/consul/vhost.conf" command = "service nginx reload" }
命令行參數和配置文件中的參數名稱是一致的,配置文件編寫完成,以加載配置文件形式啓動 consul-template
consul-template -config "nginx.hcl" --log-level=info
經過上圖看到,consul-template 啓動後即刻進入偵聽狀態,在偵聽 Consul 服務變更,若是有服務註冊進來,立刻就會更新 nginx 文件;下面,咱們嘗試啓動一個 Ron.Consul.dll:12008 的服務,該服務將會註冊到 Consul,讓咱們來看看在不從新啓動 consul-template 的狀況下,是否會自動更新 vhost.conf 文件
12008註冊爲 home 節點的服務已經啓動了,下面查看 /etc/nginx/consul/vhost.conf 配置文件
上圖紅框處內容就是自動更新的配置文件
還記得最開始的時候,咱們設置 nginx 的默認偵聽端口爲 8010 嗎;如今,咱們在瀏覽器中打開下面的地址
http://172.16.1.218:8010/home/add/10/20
獲得返回值:30,表示本次搭建 consul-template+nginx 成功!
寫了這麼長時間的博客,這篇文章應該是輸入命令最多的一篇文章了,這也是和 Windows 下編程的最大的不一樣,頗有快感,但願你們也樂在其中。