confd管理nginx配置
阿新 • • 發佈:2020-08-03
1.環境準備
confd需要和nginx安裝在同一臺伺服器上
主機名 | IP地址 | CPU | 記憶體 | 硬碟 |
gztxy-prd-nginx01 | 192.168.1.21 | 4 | 8 | 100GB |
gztxy-prd-nginx01 | 192.168.1.31 | 4 | 8 | 100GB |
2.安裝並配置
安裝
#下載confd wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64 mv confd-0.16.0-linux-amd64 /usr/bin/confd chmod +x /usr/bin/confd #檢測是否安裝成功 confd --version confd 0.16.0 (Git SHA: 7217b0ca, Go Version: go1.10.2) #confd的配置檔案,主要包含配置的生成邏輯,例如模板源,後端儲存對應的keys,命令執行等。 #templates:配置模板Template,即基於不同元件的配置,修改為go語言的模板檔案 mkdir -p /etc/confd/{conf.d,templates} #nginx需配置check_status後端檢測(模組的編譯以及配置已經在nginx初始化腳本里,按道理是應該是妥妥的) #獲取方式為http://IP/view/?format=json location /view { check_status; access_log off; } #啟動confd,每隔5秒輪詢一次,並根據CMDB的etcd傳入的json陣列動態生成配置檔案 nohup confd -interval=5 -backend etcd -node http://192.168.1.11:2379 -node http://192.168.1.12:2379 -node http://192.168.1.13:2379 >> /data/confd.log &
配置
在Nginx閘道器裡,配置Confd的2個配置檔案
cat /etc/confd/conf.d/behavior-nginx.toml
新增 Keys,這個Key就是在CMDB中配置的Key,不能重複,增加註意檔案格式
cat > /etc/confd/conf.d/behavior-nginx.toml <<EOF [template] src = "nginx.tmpl" dest = "/usr/local/nginx/conf/vhost/up.conf" owner = "root" keys = [ "/behavior", "/kuasheng-gzzs" ] reload_cmd = "/usr/local/nginx/sbin/nginx -s reload" EOF #cat /etc/confd/templates/nginx.tmpl #增加完Key後,修改UPstream的配置模板,複製存在的UPstream,修改對應的UPstream名字即可,其它保持一樣 cat > /etc/confd/templates/nginx.tmpl <<EOF upstream behavior{ {{range jsonArray (getv "/behavior")}} server {{ .}} max_fails=2 fail_timeout=40s weight=10; {{end}} check_http_send "HEAD /info HTTP/1.0\r\n\r\n"; check interval=3000 rise=2 fall=5 timeout=1000 type=http; } upstream kuasheng-gzzs{ {{range jsonArray (getv "/kuasheng-gzzs")}} server {{ .}} max_fails=2 fail_timeout=40s weight=10; {{end}} check_http_send "HEAD /interiorappapi/actuator/info HTTP/1.0\r\n\r\n"; check interval=3000 rise=2 fall=5 timeout=1000 type=http; } EOF