如何使用confd+ACM管理Nginx配置
Nginx 作為優秀的開源軟件,憑借其高性能高并發等特點,常常作為web和反向代理服務部署在生產環境中。但是當 Nginx 的規模較大時, Nginx 的運維成本也是不斷上升。本文介紹如何通過confd+ACM來管理 Nginx 配置,通過集中式的配置管理方式解決 Nginx 的大規模運維問題,運維和開發人員不用登陸到 Nginx 機器上,只需要配置好confd,然后在ACM上操作就可以動態修改 Nginx 的配置參數。
準備工作
在操作本文的示例之前需要配置好開通ACM和對confd的使用有基本概念。
Nginx 在日常開發中使用得比較多的功能是負載均衡、限流、緩存等, Nginx 的使用和安裝可以在網上查閱相關資料。本文結合負載均衡和限流功能講解如何使用confd+ACM實現 Nginx 的大規模運維操作。
創建confd配置文件
創建confd所需的toml格式配置文件
vim /etc/confd/conf.d/myapp.tomlcheck_cmd用于檢驗 Nginx 配置的正確性,當src配置錯誤則不會覆蓋 Nginx 配置
reload_cmd用于reload Nginx 配置
創建模版文件
vim /etc/confd/templates/ Nginx .conf.tmplgetv從ACM中獲取對應dataId的配置,/myapp/ Nginx /conf對應的dataId為myapp. Nginx .conf,配置格式為json格式,模版文件包含了 Nginx 的upstream、限流、黑白名單配置內容,通過json指令解析配置文件。upstream后端ip通過從ACM的配置的backends數組中獲取,同樣地,白名單和黑名單ip分別存儲在whiteList和blackList的數組中,限流的速率和并發數通過rateLimit和connectionLimit設置
... {{$data := json (getv "/myapp/ Nginx /conf")}} geo $whiteiplist {default 1;{{range $data.whiteList}}{{.}} 0;{{end}} }map $whiteiplist $limit {1 $binary_remote_addr;0 ""; } limit_req_zone $limit zone=rateLimit:10m rate={{$data.rateLimit}}r/s; limit_conn_zone $limit zone=connectionLimit:10m;{{range $data.blackList}} deny {{.}}; {{end}} upstream myapp {server 11.160.65.95:8080; } server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;proxy_pass http://myapp;limit_conn connectionLimit {{$data.connectionLimit}};limit_req zone=rateLimit burst={{$data.burst}} nodelay;} ... } ...在ACM上創建所需的配置文件
創建dataId為myapp. Nginx .conf的配置文件,group使用默認的DEFAULT_GROUP即可,配置內容設置好上游節點、黑白名單以及限流閾值
{ "backends":["10.0.1.100:80","10.0.1.101:80"], "whiteList":["10.0.1.102","10.0.1.103"], "blackList":["10.0.1.104","10.0.1.104"], "rateLimit":"10", "connectionLimit":"10", "burst":"10" }啟動confd
啟動confd,設置好backend、endpoint、命名空間namespace和阿里云賬號accessKey/secretKey
confd -backend nacos -endpoint {endpoint}:8080 -namespace {namespace} -accessKey {accessKey} -secretKey {secretKey}生成配置文件
confd將ACM中的參數通過模板文件渲染生成新的 Nginx 配置文件,查看生成的/usr/local/ Nginx / Nginx .conf配置文件是否符合預期,并檢查 Nginx 是否成功reload配置。
... geo $whiteiplist {default 1;10.0.1.102 0;10.0.1.103 0;}map $whiteiplist $limit {1 $binary_remote_addr;0 ""; }limit_req_zone $limit zone=rateLimit:10m rate=10r/s; limit_conn_zone $limit zone=connectionLimit:10m;deny 30.5.125.74;deny 10.0.1.105;upstream myapp {server 11.160.65.95:8080; } server {listen 80;server_name localhost;location / {root html;index index.html index.htm;proxy_pass http://myapp;limit_conn connectionLimit 10;limit_req zone=rateLimit burst=10 nodelay;} ... } ...動態修改 Nginx 配置
運行時當需要調節 Nginx 的名單或者限流閾值的時候,可以在ACM上修改配置的內容。當然在生產環境可以使用ACM的灰度發布功能(Beta發布)驗證沒問題再全量發布下去。
本文演示了如何使用confd+ACM管理 Nginx 配置,降低 Nginx 的運維成本。
原文鏈接
本文為云棲社區原創內容,未經允許不得轉載。
總結
以上是生活随笔為你收集整理的如何使用confd+ACM管理Nginx配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何带领团队“攻城略地”?优秀的架构师这
- 下一篇: 云原生数据库POLARDB专场“硬核”解