Skip to content

Instantly share code, notes, and snippets.

@vqiu
Last active December 13, 2016 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vqiu/c09edcb0b330b6057dfa5d138fce62c3 to your computer and use it in GitHub Desktop.
Save vqiu/c09edcb0b330b6057dfa5d138fce62c3 to your computer and use it in GitHub Desktop.
- https://github.com/coreos/etcd - https://github.com/kelseyhightower/confd ### 一、配置 etcd 数据 - 安装 etcd <……略……> - 往 etcd 添加 KV数据,配置文件生成用 ``` # Set KV for etcd etcdctl mkdir /dev/http etcdctl set /dev/http/domain dev.vqiu.cn etcdctl set /dev/http/port 1989 etcdctl set /dev/http/upstream/server1 172.16.4.150:80 etcdctl set /dev/http/listen 172.16.4.151 etcdctl set /dev/http/access_log /data/log/access/dev.vqiu.cn-access.log etcdctl set /dev/http/error_log /data/log/error/dev.vqiu.cn-error.log ``` ### 二、配置 confd ``` # wget -c https://github.com/kelseyhightower/confd/releases/download/v0.11.0/confd-0.11.0-linux-amd64 # ln -sv confd-0.11.0-linux-amd64 /usr/bin/confd ``` - confd 主配置文件 ``` # cat /etc/confd/confd.toml backend = "etcd" #client_cert = "/etc/confd/ssl/client.crt" #client_key = "/etc/confd/ssl/client.key" confdir = "/etc/confd" log-level = "info" #interval = 600 nodes = [ "http://172.16.4.151:2379" ] noop = false prefix = "/" scheme = "http" watch = true ``` ### 三、配置资源配置与模版文件 - 配置资源文件 ``` cat /etc/confd/conf.d/nginx.toml [template] src = "vhost.tmpl" dest = "/etc/nginx/conf.d/app.conf" keys = [ "/dev" ] owner = "root" mode = "0644" check_cmd = "/usr/sbin/nginx -t" reload_cmd = "/usr/sbin/nginx -s reload" ``` - 模版生成文件 ``` # cat /etc/confd/templates/vhost.tmpl {{ if ls "/dev/http" }} upstream www_{{ getv "/dev/http/domain" }} { ip_hash; {{ range getvs "/dev/http/upstream/*" }} server {{ . }}; {{ end }} } server { listen {{ getv "/dev/http/listen" }}:{{ getv "/dev/http/port" }}; server_name {{ getv "/dev/http/domain" }}; location / { proxy_pass http://www_{{ getv "/dev/http/domain" }}; proxy_http_version 1.1; proxy_read_timeout 86400s; proxy_send_timeout 86400s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404; } access_log {{ getv "/dev/http/access_log" }}; error_log {{ getv "/dev/http/error_log" }}; } {{ end }} ``` ### 启动 confd - 简单启动 ``` # confd Dec 13 15:31:27 xx confd[28744]: 2016-12-13T15:31:27+08:00 xx /usr/bin/confd[28744]: INFO Target config /etc/nginx/conf.d/app.conf out of sync Dec 13 15:31:27 xx confd[28744]: 2016-12-13T15:31:27+08:00 xx /usr/bin/confd[28744]: INFO Target config /etc/nginx/conf.d/app.conf has been updated Dec 13 15:31:46 xx confd[28744]: 2016-12-13T15:31:46+08:00 xx /usr/bin/confd[28744]: INFO /etc/nginx/conf.d/app.conf has md5sum 15ea1681ec105308e0b0875e7e7a8833 should be 7f78a1f812be8acbe1920bee53b21df0 ``` - 使用systemd 脚本启动 [建议] ``` [Unit] Description=Confd Server After=network.target After=network-online.target Wants=network-online.target [Service] Type=simple User=root ExecStart=/usr/bin/confd -config-file /etc/confd/confd.toml Restart=on-failure [Install] WantedBy=multi-user.target ``` - 查看生成的配置文件 ``` # cat /etc/nginx/conf.d/app.conf upstream www_dev.vqiu.cn { ip_hash; server 172.16.4.150:80; } server { listen 172.16.4.151:1989; server_name dev.vqiu.cn; location / { proxy_pass http://www_dev.vqiu.cn; proxy_http_version 1.1; proxy_read_timeout 86400s; proxy_send_timeout 86400s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404; } access_log /data/log/access/dev.vqiu.cn-access.log; error_log /data/log/error/dev.vqiu.cn-error.log; } ``` - 简单测试: ``` # etcdctl set /dev/http/port 80 # etcdctl set /dev/http/upstream/server1 172.16.4.149:80 正常情况,每更新 value 都会触发文件更新的,并且是实时的。 ``` > http://blog.liuts.com/post/242/ > http://www.cnblogs.com/Anker/p/6112022.html > https://gist.github.com/rosskukulinski/a8b76fa8269a7a85467e > https://github.com/kelseyhightower/confd/tree/master/docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment