etcd
etcd 是一个分布式键值对存储,设计用来可靠而快速的保存关键数据并提供访问。通过分布式锁,leader选举和写屏障(write barriers)来实现可靠的分布式协作。etcd集群是为高可用,持久性数据存储和检索而准备。
- 安装:
a.wget https://github.com/coreos/etcd/releases/download/v3.1.4/etcd-v3.1.4-linux-amd64.tar.gz
b. tar zxvf etcd-v3.1.4-linux-amd64.tar.gz
c.mv etcd etcdctl /usr/bin/
d.etcd --listen-client-urls http://10.8.16.221:2379 --advertise-client-urls http://10.8.16.221:2379
confd
管理本地应用配置文件,使用etcd或consul存储的数据渲染模板,还支持redis、zookeeper等。confd有一个watch功能,通过HTTP API定期监测对应的etcd中目录变化,获取最新的Value,然后渲染模板,更新配置文件。
-
安装:
a.wget https://github.com/kelseyhightower/confd/releases/download/v0.11.0/confd-0.11.0-linux-amd64
b.mv confd-0.11.0-linux-amd64 /usr/bin/confd
c.创建配置目录mkdir -p /etc/confd/{conf.d,templates}
conf.d # 资源模板,下面文件必须以toml后缀
templates # 配置文件模板,下面文件必须以tmpl后缀d.创建资源模板
[template]
src = “app01.conf.tmpl” # 默认在/etc/confd/templates目录下
dest = “/usr/local/nginx/conf/vhost/app01.conf” # 要更新的配置文件
keys = [
“/nginx”, # 监测的key
]
reload_cmd ="/usr/local/nginx/sbin/nginx -s reload" # 最后执行的命令e.创建Nginx配置文件模板
upstream www.{{getv “/nginx/www/server_name”}} {
{{range getvs “/nginx/www/upstream/*”}}
server {{.}};
{{end}}
}
server {
server_name www.{{getv “/nginx/www/server_name”}};
location / {
proxy_pass http://www.{{getv “/nginx/www/server_name”}};
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}f.向etcd添加k/v
etcdctl -C http://10.8.16.221:2379 set /nginx/www/server_name “domain.com”
etcdctl -C http://10.8.16.221:2379 set /nginx/www/upstream/server01 “192.168.1.10:80”
etcdctl -C http://10.8.16.221:2379 set /nginx/www/upstream/server02 “192.168.1.11:80”g.启动confd监测etcd中的keys
confd -watch -backend=“etcd” -node=http://10.8.16.221:2379
h.运行confd
[root@bogon vagrant]# /usr/bin/confd -watch -backend=“etcd” -node=http://10.8.16.221:2379
2018-10-11T09:11:33Z bogon /usr/bin/confd[4394]: INFO Backend set to etcd
2018-10-11T09:11:33Z bogon /usr/bin/confd[4394]: INFO Starting confd
2018-10-11T09:11:33Z bogon /usr/bin/confd[4394]: INFO Backend nodes set to http://10.8.16.221:2379
^C2018-10-11T09:12:27Z bogon /usr/bin/confd[4394]: INFO Captured interrupt. Exiting…
[root@bogon vagrant]# /usr/bin/confd -watch -backend=“etcd” -node=http://10.8.16.221:2379
2018-10-11T09:12:27Z bogon /usr/bin/confd[4398]: INFO Backend set to etcd
2018-10-11T09:12:27Z bogon /usr/bin/confd[4398]: INFO Starting confd
2018-10-11T09:12:27Z bogon /usr/bin/confd[4398]: INFO Backend nodes set to http://10.8.16.221:2379
2018-10-11T09:12:27Z bogon /usr/bin/confd[4398]: INFO /usr/local/nginx/conf/vhost/app01.conf has md5sum 6a15d712bfd00eb045f78db9e6e32c7d should be ac90788f22fba60f7bcd614e63a9d162
2018-10-11T09:12:27Z bogon /usr/bin/confd[4398]: INFO Target config /usr/local/nginx/conf/vhost/app01.conf out of sync
2018-10-11T09:12:27Z bogon /usr/bin/confd[4398]: INFO Target config /usr/local/nginx/conf/vhost/app01.conf has been updatedi.查看配置文件
[root@bogon vagrant]# cat /usr/local/nginx/conf/vhost/app01.conf
upstream www.domain.com {
server 192.168.1.10:80;
server 192.168.1.11:80;
}
server {
server_name www.domain.com;
location / {
proxy_pass http://www.domain.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
参考资料:
[1]: https://www.cnblogs.com/breg/p/5756558.html
[2]: http://etcd.doczh.cn/documentation/
[3]: http://blog.51cto.com/lizhenliang/1910150
[4]: https://www.cnblogs.com/Anker/p/6112022.html
[5]: https://github.com/kelseyhightower/confd
[6]:https://blog.csdn.net/ztsinghua/article/details/51643732