Confd实现Nginx配置文件自动管理
admin
2023-02-27 19:41:42
0

Confd是一个轻量级的配置管理工具。通过查询Etcd,结合配置模板引擎,保持本地配置最新,同时具备定期探测机制,配置变更自动reload。其后端支持的数据类型有:etcd、consul、vault、environment variables、redis、zookeeper、dynamodb、stackengine、rancher。不过一般使用Confd和etcd的配合使用比较多。
Confd实现Nginx配置文件自动管理
前端服务器:
服务器IP 主机名 安装组件 备注
192.168.27.211 Client1 etcd+confd+nginx+keepalived 192.168.27.110(Vip)
(http://nginx.jerry.com)
192.168.27.212 Client2 etcd+confd+nginx+keepalived
192.168.27.213 Client3 etcd+confd+nginx+keepalived
192.168.27.210 master ansible 堡垒机

后端服务器(web站):
服务器IP 功能
192.168.26.210 web1
192.168.26.211 web2
192.168.26.212 web3

安装etcd集群确保正常略(ansible k8s -m shell -a'etcdctl endpoint health')。
Confd实现Nginx配置文件自动管理

后端web服务器安装配置略(注意VIP域名映射关系):
简略介绍安装keepalived安装配置:
[root@client1 ~]# yum install keepalived –y
Confd实现Nginx配置文件自动管理
Confd实现Nginx配置文件自动管理
192.168.27.211:
[root@client1 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
router_id nginx1
}
vrrp_script chk_http_port {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2

}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 20
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass jerry520
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.27.110/22
}
}

192.168.27.212:
! Configuration File for keepalived

global_defs {
router_id nginx2
}
vrrp_script chk_http_port {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2

}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 20
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass jerry520
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.27.110/22
}
}
192.168.27.213:
! Configuration File for keepalived

global_defs {
router_id nginx3
}
vrrp_script chk_http_port {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2

}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 20
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass jerry520
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.27.110/22
}
}
Nginx检测脚本:三台服务器上都需要配置(一样的)vim /etc/keepalived/chk_nginx.sh
[root@client1 keepalived]# cat chk_nginx.sh
#!/bin/bash
A=ps -C nginx --no-header |wc -l
if [ $A -eq 0 ];then
echo 'nginx server is died'
/etc/init.d/keepalived stop
fi

nginx安装:
yum install nginx -y
nginx.conf配置文件:三台服务器保持一样 vim /etc/nginx/nginx.conf
user nginx ;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream nginx.jerry.com {
server 192.168.26.210:80;
server 192.168.26.211:80;
server 192.168.26.212:80;

}
server {
listen 80;
server_name nginx.jerry.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://nginx.jerry.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
分别安装配置好KEEPALIved和nginx(转发器)并启动运行观察效果:

Confd安装配置:

[root@master etc]# ansible k8s -m copy -a"src=/etc/confd dest=/etc/"

[root@master bin]# ansible k8s -m copy -a"src=/usr/bin/confd dest=/usr/bin/confd"
[root@master bin]# ansible k8s -m shell -a"cd /usr/bin;chmod +x confd "
[root@master conf.d]# ansible k8s -m shell -a'ls /usr/bin/confd -l'
Confd实现Nginx配置文件自动管理
Confd实现Nginx配置文件自动管理
Confd实现Nginx配置文件自动管理
创建配置目录
mkdir -p /etc/confd/{conf.d,templates}
conf.d # 资源模板,下面文件必须以toml后缀
templates # 配置文件模板,下面文件必须以tmpl后缀
Confd实现Nginx配置文件自动管理
创建confd配置文件:
[root@client1 confd]# cat conf.d/sync_nginx.toml
[template]
prefix = "/nginx/www"
src = "nginx.conf.tmpl"
dest = "/etc/nginx/conf.d/mynginx.conf"
owner = "nginx"
mode = "0644"
keys = [
"/server_name",
"/upstream",
]
reload_cmd = "/usr/sbin/nginx -s reload"
创建模板文件:
upstream {{getv "/server_name"}}.jerry.com {
{{ range getvs "/upstream/*"}}
server {{.}};
{{end}}
}
server {
listen 80;
server_name {{getv "/server_name"}}.jerry.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://{{getv "/server_name"}}.jerry.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

Confd实现Nginx配置文件自动管理
[root@client1 templates]# confd -watch -backend="etcdv3" -node http://192.168.27.211:2379
Confd实现Nginx配置文件自动管理
[root@client1 conf.d]# etcdctl put /nginx/www/upstream/serverweb1 "192.168.26.210"
[root@client1 conf.d]# etcdctl put /nginx/www/upstream/serverweb2 "192.168.26.211"
[root@client1 conf.d]# etcdctl put /nginx/www/upstream/serverweb3 "192.168.26.212"
[root@client1 conf.d]# etcdctl put /nginx/www/server_name "nginx"
观察集群中每一个结点NGINX反向代理配置文件变化:
27.212:
Confd实现Nginx配置文件自动管理
27.213:
Confd实现Nginx配置文件自动管理
27.211:

Confd实现Nginx配置文件自动管理
我们再来一次观察下变化,这次通过27.212更换键值/nginx/www/server_name 我们把值改为httpd(原来为nginx)观察27.211上NGINX配置文件是否更改变化。
[root@client2 conf.d]# etcdctl put /nginx/www/server_name "httpd"
Confd实现Nginx配置文件自动管理
配置文件瞬间更改:
Confd实现Nginx配置文件自动管理
测试:假如要增删改后端服务器(把后端WEB服务192.168.26.210进行删除操作).
[root@client2 conf.d]# etcdctl del /nginx/www/upstream/serverweb1
Confd实现Nginx配置文件自动管理
配置文件中已经将后端服务器192.168.26.210无感知地移出并重新加载配置
Confd实现Nginx配置文件自动管理
访问也变了
Confd实现Nginx配置文件自动管理
用公网IP:192.168.27.100(vip)访问(负载均衡采用轮询):
Confd实现Nginx配置文件自动管理
Confd实现Nginx配置文件自动管理
Confd实现Nginx配置文件自动管理
通过域名(http://nginx.jerry.com)访问后端站点(负载均衡采用轮询):
记得做公网域名解析或更换本地HOST文件:
Confd实现Nginx配置文件自动管理
Confd实现Nginx配置文件自动管理
Confd实现Nginx配置文件自动管理
Confd实现Nginx配置文件自动管理

参考文献:https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md
https://github.com/kelseyhightower/confd/blob/master/docs/template-resources.md
https://github.com/kelseyhightower/confd/blob/master/docs/templates.md

相关内容

热门资讯

水龙头出水小怎么解决 水龙头出水小可能是由多种原因引起的,以下是一些常见的解决方法:1. 清洁水龙头:水龙头使用时间长了,...
小神童洗衣机启动后不出水直接洗 有可能是洗衣机的进水阀被烧坏了,需要换一个新的进水阀安装上即可。另外不建议自己擅自拆卸洗衣机内部进行...
祝贺景德镇! 海报制作:苗夏阳正在韩国釜山举行的联合国教科文组织第48届世界遗产大会25日通过决议将“景德镇手工瓷...
中央空调不制冷怎么办-中央空调... 中央空调,是电器股份有限公司主要产品之一,但是由于中央空调使用环境和使用方式的不同,在使用过程中就会...
空调不出水不制冷怎么回事 内罩长期不清洗,当然房间密封性差的话也会导致这种现象;其次就是外机问题,如果外机积满灰尘,会成空调制...
空调不出水,不制冷 原因有多种,可能是电源插头没有接牢、制冷剂不足、空调短时间休息间隙、空调滤网积灰等等。很多情况可以自...
中方反击了,欧盟回应 据凤凰卫视报道,中国商务部7月24日宣布对包括德国军工巨头莱茵金属公司在内的14家欧洲企业实施出口管...
这个“中国首次”,很有看点 刚刚,韩国釜山,第48届世界遗产大会传来好消息:“景德镇手工瓷业遗存”成功列入《世界遗产名录》!这是...
85岁剑桥教授,为何成了中国年... ·2026年6月,艾伦·麦克法兰在英国剑桥大学国王学院。(Muye.G/摄)(本文图片均由剑桥康河出...
西安造12英寸硅片月产销突破1... 12英寸电子级硅片是当前全球半导体产业链的紧缺关键材料。近日,记者从位于西安高新区的西安奕斯伟材料科...