您的位置:首页 > 运维架构 > Nginx

Etcd+Confd实现Nginx配置文件自动管理

2017-03-24 19:17 477 查看
一、需求

我们使用Nginx做七层负载均衡,后端是Tomcat。项目采用灰度发布方式,每次项目升级,都要手动先从Nginx下摘掉一组,然后再升级这组,当项目快速迭代时,手动做这些操作显然会增加部署时间,于是就想通过脚本实现自动化管理Nginx配置文件。
当时考虑自己写Shell脚本对Nginx配置文件操作,需要用到sed流编辑器,sed本身没有条件判断语句,并不能灵活判断配置文件中要添加/删除位置,因此会增加配置错误风险。
在查资料无意间发现confd能自动管理配置文件,通过模板渲染生成配置文件,避免了配置错误风险,觉得挺好,就实验了下,于是就有了本章博文。
二、架构图



三、涉及软件
etcd:分布式KV存储系统,一般用于共享配置和服务注册与发现。是CoreOS公司发起的一个开源项目。 ETCD存储格式类似于文件系统,以根"/"开始下面一级级目录,最后一个是Key,一个key对应一个Value。
etcd集群:使用Raft协议保证每个节点数据一致,由多个节点对外提供服务。这里只用单台。
confd:管理本地应用配置文件,使用etcd或consul存储的数据渲染模板,还支持redis、zookeeper等。
confd有一个watch功能,通过HTTP API定期监测对应的etcd中目录变化,获取最新的Value,然后渲染模板,更新配置文件。
四、部署
环境说明:CentOS7,IP 192.168.1.99 # 为了试验服务都安装这台上。
4.1 下载软件包
https://github.com/coreos/etcd/releases/download/v3.1.4/etcd-v3.1.4-linux-amd64.tar.gz
https://github.com/kelseyhightower/confd/releases/download/v0.11.0/confd-0.11.0-linux-amd64
如果没翻墙可能下载不了,这个是我下载好的:http://pan.baidu.com/s/1c1M9kBm
4.2 安装Nginx并启动
# yum install -y gcc gcc-c++ make openssl-devel pcre-devel
# useradd nginx -s/sbin/nologin
# wget http://nginx.org/download/nginx-1.10.3.tar.gz # tar zxvf nginx-1.10.3.tar.gz
# cd nginx-1.10.3
# ./configure--prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module--with-http_gzip_static_module --with-http_realip_module--with-http_stub_status_module
# make && make install
独立出来虚拟主机:
# mkdir /usr/local/nginx/conf/vhost
# vi /usr/local/nginx/conf/nginx.conf # 在http{}段中末尾添加包含虚拟主机配置文件
http {
includevhost/*.conf;
}
# /usr/local/nginx/sbin/nginx  # 启动Nginx
4.3 安装etcd并启动
# tar zxvf etcd-v3.1.4-linux-amd64.tar.gz
# mv etcd etcdctl /usr/bin/  # etcd提供封装好的包,直接使用即可
# nohup etcd--data-dir /var/lib/data.etcd --listen-client-urls http://192.168.1.99:2379--advertise-client-urls http://192.168.1.99:2379 &>/var/log/etcd.log &
4.4 安装confd

confd也是一个封装好的包,直接使用即可。
# mv confd-0.11.0-linux-amd64 /usr/bin/confd
key规划:

keyvalue
/nginx/www/server_name
域名
/nginx/www/upstream/server01
节点01
/nginx/www/upstream/server02
节点02
/nginx/www/upstream/server03
节点03
创建配置目录
# mkdir -p /etc/confd/{conf.d,templates}
conf.d    # 资源模板,下面文件必须以toml后缀
templates # 配置文件模板,下面文件必须以tmpl后缀
创建资源模板
# vi /etc/confd/conf.d/app01.conf.toml
[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"   # 最后执行的命令
创建Nginx配置文件模板
# vi /etc/confd/templates/app01.conf.tmpl
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;
}
}

博客地址:http://lizhenliang.blog.51cto.comQQ群:323779636(Shell/Python运维开发群)
五、测试
使用etcdctl客户端操作etcd,常用的几个选项如下:
USAGE:
etcdctl [global options] command [command options] [arguments...]
COMMANDS:
ls          retrieve a directory
get         retrieve the value of a key
set         set the value of a key
rm          remove a key or a directory
GLOBAL OPTIONS:
--peers, -C     a comma-delimited list of machine addresses in the cluster (default: "http://127.0.0.1:2379,http://127.0.0.1:2379")
5.1 向etcd添加k/v
# etcdctl -C http://192.168.1.99:2379 set domain.com
domain.com
# etcdctl -C http://192.168.1.99:2379 set /nginx/www/upstream/server01 "192.168.1.10:80"
192.168.1.10:80
# etcdctl -C http://192.168.1.99:2379 set /nginx/www/upstream/server02 "192.168.1.11:80"
192.168.1.11:80
5.2 启动confd监测etcd中的keys



当你启动后,confd就会从etcd获取key的值并填充到Nginx配置文件模板,并更新到/usr/local/nginx/conf/vhost/app01.conf,并nginx reload。



5.3 近一步测试
向etcd中/nginx/www/upstream/再添加一个节点:



OK!这样就实现了自动管理Nginx配置文件,无感知加入后端节点。
六、etcd Rest API使用
curl -X PUT http://192.168.1.99:2379/v2/keys/test/a_key -d value="789"  # 增改key
curl -X DELETE http://192.168.1.99:2379/v2/keys/test/a_key     # 删除key
curl http://192.168.1.99:2379/v2/keys/test/a_key                 # 查询key的值
curl -X PUT http://192.168.1.99:2379/v2/keys/dir -d dir=true   # 创建目录
curl http://192.168.1.99:2379/v2/keys             # 查看所有keys
curl -X PUT http://192.168.1.99:2379/v2/keys/ttlvar -d value="ttl_value" -d ttl=10 # 创建过期时间的key,单位秒
curl http://192.168.1.99:2379/version             # 查看etcd版本
curl http://192.168.1.99:2379/v2/members            # 列出所有集群成员
curl http://192.168.1.99:2379/v2/stats/leader         # 查看leader
curl http://192.168.1.99:2379/v2/stats/self           # 节点自身信息
curl http://192.168.1.99:2379/v2/stats/store          # 查看集群运行状态
七、总结总体来说,etcd+confd要比自己写脚本管理Nginx配置文件更方便!当然,增加一套组件也会增加一点运维成本。当初始化一台Web节点,可以增加一步操作去把自己信息注册到etcd,从而实现自动加入节点。如果应用在生产环境,有些功能需要更加完善些,比如向etcd添加数据用Rest API或者用Python封装接口,confd写一个后台进程脚本运行等。如果线上已经有redis或者zookeeper的话,那么就可以直接作为k/v存储信息,而不需要再搭建一套etcd集群!由于keys的每次更新confd都会Nginx reload,在高并发访问量会有一点影响,比较好的解决方案就是写lua去动态加载Nginx参数。但应付中小规模还是可以的!由此看来,confd不但局限管理Nginx配置文件,对于其他应用软件也是可以的,比如Haproxy,LVS等。
confd使用文档:https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md资源模板其他参数:https://github.com/kelseyhightower/confd/blob/master/docs/template-resources.md
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx etcd confd
相关文章推荐