使用etcd+confd管理nginx配置

一、前言node

  最近在項目中用nginx作反向代理,須要動態生成nginx的配置。大概流程是用戶在頁面上新增域名、http或https協議以及端口信息,後臺會根據域名自動生成一個nginx的server配置,在nginx.conf配置文件中使用include將全部的server配置加載進來。遇到一個問題就是如何動態生成nginx的配置,以及配置更新。在此以前也接觸過配置生成及更新,當時配置的格式都是基於xml的,使用protobuf與xml結合,先採用protobuf定義好配置的數據結構,而後將protobuf轉換爲xml配置文件。配置更新經過判斷配置文件的md5是否發生變化進行。而現在接觸的配置是nginx的配置,是一套自定義的配置。看了一眼nginx的配置源碼,用一個void**** conf 來表示,頓時嚇死寶寶了,膜拜做者編程技能。nginx配置代碼內存組織是至關的牛逼,通用性很是強,有興趣能夠認真研究一下。我要自動生成的配置是以下所示:mysql

upstream www_test {
    server 196.75.121.112:443;     (動態生成)
}

server {
    listen       443 ssl; (動態生成)
    server_name  www.test.com; (動態生成)
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;; 
    ssl_certificate             /home/build/openresty/nginx/cert/dealssl/www.bestenover.com.crt; (動態生成)

    location / { 
        proxy_pass https://www_test; (動態生成)
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect off;
    }   
}

二、實現框架nginx

  看完nginx的配置不難發現,nginx是一個典型的key value類型的,並且與文件系統的很是相似,一個目錄下面能夠包含其餘配置,目錄下還能夠有目錄,嵌套多層。現在key value類型的數據庫很是多,redis、leveldb等,最近新秀etcd也是key-value分佈式數據庫,提供相似文件系統操做,使用raft協議保持數據一致性,很是適合雲計算分佈式部署場景,將confd與etcd搭配,很是適合nginx這樣的配置格式。git

使用etcd新建與nginx配置對應的目錄以下:github

爲了配合webui展現配置和後臺生成nginx配置,整體的配置流程圖以下所示:web

 三、生成配置redis

  WEBUI經過API將配置寫入mysql和etcd,confd註冊監控etcd的key爲/nginx/,只要發生變化就通知confd根據模板生成配置。confd默認的配置路徑爲/etc/confd/,建立conf.d和template兩個目錄,分別存放配置資源和配置模板。sql

nginx的配置資源以下所示test.conf.toml數據庫

[template]
src = "test.conf.tmpl"
dest = "/tmp/test.conf"
keys = [ 
    "/nginx",
]
check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
reload_cmd = "/usr/sbin/service nginx reload"

nginx的配置模板以下所示:test.conf.tmpl編程

upstream www_{{getv "/nginx/https/www/server/server_name"}} {
    {{range getvs "/nginx/https/www/upstream/*"}}server {{.}};{{end}}
}

server {
    server_name         {{getv "/nginx/https/www/server/server_name"}}:443;
    ssl on
    ssl_certificate     {{getv "/nginx/https/www/server/ssl_certificate"}};
    ssl_certificate_key {{getv "/nginx/https/www/server/ssl_certificate_key"}};
    location / { 
        proxy_pass        http://www_{{getv "/nginx/https/www/server/server_name"}};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect    off;
    }   
}

開啓confd,執行 ./confd -watch -backend etcd -node http://127.0.0.1:2379

使用ectdctl添加配置以下:

./etcdctl set /nginx/https/www/server/server_name test.com
./etcdctl set /nginx/https/www/server/ssl_certificate /home/waf/build/openresty/nginx/cert/client/client.crt
./etcdctl set /nginx/https/www/server/ssl_certificate_key /home/waf/build/openresty/nginx/cert/client/client.key;
/etcdctl set /nginx/https/www/upstream/server1 192.168.1.2:443
./etcdctl set /nginx/https/www/upstream/server2 192.168.4.2:443

confd的執行結果以下所示:

生成位置文件以下所示:

upstream www_test.com {
    server 192.168.1.2:443;
    server 192.168.4.2:443;
}

server {
    server_name         test.com:443;
    ssl on
    ssl_certificate     /home/waf/build/openresty/nginx/cert/client/client.crt;
    ssl_certificate_key /home/waf/build/openresty/nginx/cert/client/client.key;
    location / { 
        proxy_pass        http://www_test.com;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect    off;
    }   
}

與模板生成的保持一致。

四、參考資料

cond的github上文檔介紹

https://github.com/kelseyhightower/confd/blob/master/docs/template-resources.md

etcd與confd實現配置管理

http://xiaorui.cc/2015/01/25/confd%E5%92%8Cetcd%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86%E5%8F%8A%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0/

http://www.tuicool.com/articles/eeiAve

etcd的參考:

https://github.com/coreos/etcd

https://yq.aliyun.com/articles/11035

http://www.infoq.com/cn/articles/coreos-analyse-etcd/

相關文章
相關標籤/搜索