confd管理nginx配置


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個配置文件

新增 Keys,不能重復,增加注意文件格式

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

 配置nginx

cat > /usr/local/nginx/conf/vhost/up.conf <<EOF
server {
    listen 80;
    server_name localhost;

    location /view {
        check_status;
        access_log off;
    }

    location /api/kuasheng/behavior {
        proxy_pass http://behavior/;
    }

    location /interiorappapi/ {
        proxy_pass http://kuasheng-gzzs;
    }
    error_page  502 /502.json;
    location /502.json {
    }
    error_page  404 /404.json;
    location /404.json {
    }
}
EOF

3.測試

#添加behavior和kuasheng-gzzs值
etcdctl -C http://192.168.1.11:2379 set /behavior "[\"192.168.1.11:10900\", \"192.168.1.12:10900\"]"
etcdctl -C http://192.168.1.11:2379 set /kuasheng-gzzs "[\"192.168.1.11:80\", \"192.168.1.12:80\"]"


#自動生成 /usr/local/nginx/conf/vhost/up.conf文件
upstream behavior{

        server 192.168.1.11:10900 max_fails=2 fail_timeout=40s weight=10;

        server 192.168.1.20:10900 max_fails=2 fail_timeout=40s weight=10;

        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{
       
       server 192.168.1.11:10900 max_fails=2 fail_timeout=40s weight=10;
       
       server 192.168.1.20:10900 max_fails=2 fail_timeout=40s weight=10;
       
       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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM