Confd+etcd實現高可用自動發現


Confd是一個輕量級的配置管理工具。通過查詢Etcd,結合配置模板引擎,保持本地配置最新,同時具備定期探測機制,配置變更自動reload。其后端支持的數據類型有:etcd、consul、vault、environment variables、redis、zookeeper、dynamodb、stackengine、rancher。不過一般使用Confd和etcd的配合使用比較多。其常用架構如下:

etcd-confd-nginx

一、簡單配置

1、配置etcd數據

具體步驟這里略過,這里只配置兩條數據

 

  1. etcdctl set /myapp/database/url www.361way.com
  2. etcdctl set /myapp/database/user rob

2、confd安裝

confd比較簡單就一個文件,拿過來就可以執行,可以從github上下載:https://github.com/kelseyhightower/confd/releases ,並將其放到/usr/local/bin目錄下即可。不過使用前需要創建相應的配置目錄:

 

  1. [root@etcd1 bin]# mkdir -p /etc/confd/{conf.d,templates}

3、創建confd配置文件

 

  1. # vim /etc/confd/conf.d/myconfig.toml
  2. [template]
  3. src = "myconfig.conf.tmpl"
  4. dest = "/tmp/myconfig.conf"
  5. keys = [
  6. "/myapp/database/url",
  7. "/myapp/database/user",
  8. ]

4、創建模板文件

 

  1. # vim /etc/confd/templates/myconfig.conf.tmpl
  2. [myconfig]
  3. database_url = {{getv "/myapp/database/url"}}
  4. database_user = {{getv "/myapp/database/user"}}

5、執行生成配置文件

  1. confd -onetime -backend etcd -node http://127.0.0.1:2379 只一次
  2. confd -interval=60 -backend etcd -node http://127.0.0.1:2379 & 按時間輪詢

使用onetime參數的,配置文件生成一次后,confd程序就退出了,下面的那句,會每隔60秒輪詢一次。一旦后端etcd相應的值發生變化就會重新生成相應的配置文件。

6、驗證文件生成

 

  1. [root@etcd1 tmp]# cat /tmp/myconfig.conf
  2. [myconfig]
  3. database_url = www.361way.com
  4. database_user = rob

 

二、etcd+confd+nginx配置

1、創建數據

 

  1. etcdctl set /myapp/subdomain myapp
  2. etcdctl set /myapp/upstream/app2 "10.0.1.100:80"
  3. etcdctl set /myapp/upstream/app1 "10.0.1.101:80"
  4. etcdctl set /yourapp/subdomain yourapp
  5. etcdctl set /yourapp/upstream/app2 "10.0.1.102:80"
  6. etcdctl set /yourapp/upstream/app1 "10.0.1.103:80"

2、創建配置文件

 

  1. # cat /etc/confd/conf.d/myapp-nginx.toml
  2. [template]
  3. prefix = "/myapp"
  4. src = "nginx.tmpl"
  5. dest = "/tmp/myapp.conf"
  6. owner = "nginx"
  7. mode = "0644"
  8. keys = [
  9. "/subdomain",
  10. "/upstream",
  11. ]
  12. check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
  13. reload_cmd = "/usr/sbin/service nginx reload"
  14. # cat /etc/confd/conf.d/yourapp-nginx.toml
  15. [template]
  16. prefix = "/yourapp"
  17. src = "nginx.tmpl"
  18. dest = "/tmp/yourapp.conf"
  19. owner = "nginx"
  20. mode = "0644"
  21. keys = [
  22. "/subdomain",
  23. "/upstream",
  24. ]
  25. check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
  26. reload_cmd = "/usr/sbin/service nginx reload"

這里創建了兩個配置文件。接下來創建一個模板文件,兩個配置文件會根據該模板文件生成配置:

 

  1. # cat /etc/confd/templates/nginx.tmpl
  2. upstream {{getv "/subdomain"}} {
  3. {{range getvs "/upstream/*"}}
  4. server {{.}};
  5. {{end}}
  6. }
  7. server {
  8. server_name {{getv "/subdomain"}}.example.com;
  9. location / {
  10. proxy_pass http://{{getv "/subdomain"}};
  11. proxy_redirect off;
  12. proxy_set_header Host $host;
  13. proxy_set_header X-Real-IP $remote_addr;
  14. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  15. }
  16. }

3、驗證

在進行驗證啟用的時候,會發現有如下報錯:

 

  1. [root@etcd1 conf.d]# 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: INFO Target config /tmp/myapp.conf out of sync
  2. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR "nginx: [emerg] \"upstream\" directive is not allowed here in /tmp/.myapp.conf835093196:1\nnginx: configuration file /tmp/.myapp.conf835093196 test failed\n"
  3. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR Config check failed: exit status 1
  4. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: INFO Target config /tmp/yourapp.conf out of sync
  5. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR "nginx: [emerg] \"upstream\" directive is not allowed here in /tmp/.yourapp.conf196880350:1\nnginx: configuration file /tmp/.yourapp.conf196880350 test failed\n"
  6. 2017-05-08T19:06:07+08:00 etcd1 confd[10949]: ERROR Config check failed: exit status 1

原因很簡單,注意配置文件中的check_cmd 命令,該命令會進行配置文件檢測,檢測不通過時,配置文件不會修改,且不會執行后面的reload_cmd命令。這里想不報錯也很簡單,將配置文件中nginx的配置指向正確的位置,而且讓nginx可以正常檢測,且檢測結果沒有錯誤。再次執行命令,並修改配置文件,會發現有如下信息:

 

  1. # cat myapp.conf
  2. upstream myapp {
  3. server 10.0.1.100:80;
  4. server 10.0.1.101:80;
  5. }
  6. server {
  7. server_name myapp.example.com;
  8. location / {
  9. proxy_pass http://myapp;
  10. proxy_redirect off;
  11. proxy_set_header Host $host;
  12. proxy_set_header X-Real-IP $remote_addr;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. }
  15. }

三、其他

在模板文件中,會用到一些函數:map、base、exists、get、gets、getenv、datetime等 ,關於這些函數的使用,可以參看官方文檔templates 。

etcd+confd 基本上可以和任何應用進行組合,如網上比較常見的etcd+confd+nginx 、etcd+confd+haproxy 、etcd+confd+k8s、etcd+confd+tomcat等等。和tomcat的整合,在官方上也有相應的示例,具體可以參看官方文檔tomcat-sample 。

  1)清空etcd :etcdctl rm --recursive  registry  (registry為自己set的名稱)

       2)備份與恢復

         2.1)對於 API 2 備份與恢復方法官方 v2 admin guide

etcd的數據默認會存放在我們的命令工作目錄中,我們發現數據所在的目錄,會被分為兩個文件夾中:

  • snap: 存放快照數據,etcd防止WAL文件過多而設置的快照,存儲etcd數據狀態。

  • wal: 存放預寫式日志,最大的作用是記錄了整個數據變化的全部歷程。在etcd中,所有數據的修改在提交前,都要先寫入到WAL中。

# etcdctl backup --data-dir /home/etcd/ --backup-dir /home/etcd_backup # etcd -data-dir=/home/etcd_backup/ -force-new-cluster 

恢復時會覆蓋 snapshot 的元數據(member ID 和 cluster ID),所以需要啟動一個新的集群。

    2.2)對於 API 3 備份與恢復方法官方 v3 admin guide

在使用 API 3 時需要使用環境變量 ETCDCTL_API 明確指定。

在命令行設置:

# export ETCDCTL_API=3 

備份數據:

# etcdctl --endpoints localhost:2379 snapshot save snapshot.db 

恢復:

# etcdctl snapshot restore snapshot.db --name m3 --data-dir=/home/etcd_data 

恢復后的文件需要修改權限為 etcd:etcd
--name:重新指定一個數據目錄,可以不指定,默認為 default.etcd
--data-dir:指定數據目錄
建議使用時不指定 name 但指定 data-dir,並將 data-dir 對應於 etcd 服務中配置的 data-dir

etcd 集群都是至少 3 台機器,官方也說明了集群容錯為 (N-1)/2,所以備份數據一般都是用不到,但是鑒上次 gitlab 出現的問題,對於備份數據也要非常重視。

 


免責聲明!

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



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