1、需求
linux
咱们使用Nginx作七层负载均衡,后端是Tomcat。项目采用灰度发布方式,每次项目升级,都要手动先从Nginx下摘掉一组,而后再升级这组,当项目快速迭代时,手动作这些操做显然会增长部署时间,因而就想经过脚本实现自动化管理Nginx配置文件。nginx
当时考虑本身写Shell脚本对Nginx配置文件操做,须要用到sed流编辑器,sed自己没有条件判断语句,并不能灵活判断配置文件中要添加/删除位置,所以会增长配置错误风险。c++
在查资料无心间发现confd能自动管理配置文件,经过模板渲染生成配置文件,避免了配置错误风险,以为挺好,就实验了下,因而就有了本章博文。git
2、架构图github
3、涉及软件redis
etcd:分布式KV存储系统,通常用于共享配置和服务注册与发现。是CoreOS公司发起的一个开源项目。 ETCD存储格式相似于文件系统,以根"/"开始下面一级级目录,最后一个是Key,一个key对应一个Value。后端
etcd集群:使用Raft协议保证每一个节点数据一致,由多个节点对外提供服务。这里只用单台。bash
confd:管理本地应用配置文件,使用etcd或consul存储的数据渲染模板,还支持redis、zookeeper等。架构
confd有一个watch功能,经过HTTP API按期监测对应的etcd中目录变化,获取最新的Value,而后渲染模板,更新配置文件。并发
4、部署
环境说明: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规划:
key |
value |
/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.com
QQ群:323779636(Shell/Python运维开发群)
5、测试
使用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配置文件,无感知加入后端节点。
6、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 # 查看集群运行状态
7、总结
整体来讲,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