当前位置: 首页 > 工具软件 > Confd > 使用案例 >

confd_confd管理nginx配置

隗轶
2023-12-01

1.环境准备

confd需要和nginx安装在同一台服务器上

主机名

IP地址

CPU

内存

硬盘

gztxy-prd-nginx01

192.168.1.21

4

8

100GB

gztxy-prd-nginx01

192.168.1.31

4

8

100GB

2.安装并配置

安装

#下载confd

wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64

mv confd-0.16.0-linux-amd64 /usr/bin/confd

chmod +x /usr/bin/confd

#检测是否安装成功

confd --version

confd 0.16.0 (Git SHA: 7217b0ca, Go Version: go1.10.2)

#confd的配置文件,主要包含配置的生成逻辑,例如模板源,后端存储对应的keys,命令执行等。

#templates:配置模板Template,即基于不同组件的配置,修改为go语言的模板文件

mkdir -p /etc/confd/{conf.d,templates}

#nginx需配置check_status后端检测(模块的编译以及配置已经在nginx初始化脚本里,按道理是应该是妥妥的)

#获取方式为http://IP/view/?format=json

location /view {

check_status;

access_log off;

}

#启动confd,每隔5秒轮询一次,并根据CMDB的etcd传入的json数组动态生成配置文件

nohup confd -interval=5 -backend etcd -node http://192.168.1.11:2379 -node http://192.168.1.12:2379 -node http://192.168.1.13:2379 >> /data/confd.log &

配置

在Nginx网关里,配置Confd的2个配置文件

新增 Keys,不能重复,增加注意文件格式

cat > /etc/confd/conf.d/behavior-nginx.toml <

[template]

src = "nginx.tmpl"

dest = "/usr/local/nginx/conf/vhost/up.conf"

owner = "root"

keys = [

"/behavior",

"/kuasheng-gzzs"

]

reload_cmd = "/usr/local/nginx/sbin/nginx -s reload"

EOF

#cat /etc/confd/templates/nginx.tmpl

#增加完Key后,修改UPstream的配置模板,复制存在的UPstream,修改对应的UPstream名字即可,其它保持一样

cat > /etc/confd/templates/nginx.tmpl <

upstream behavior{

{{range jsonArray (getv "/behavior")}}

server {{ .}} max_fails=2 fail_timeout=40s weight=10;

{{end}}

check_http_send "HEAD /info HTTP/1.0\r\n\r\n";

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

}

upstream kuasheng-gzzs{

{{range jsonArray (getv "/kuasheng-gzzs")}}

server {{ .}} max_fails=2 fail_timeout=40s weight=10;

{{end}}

check_http_send "HEAD /interiorappapi/actuator/info HTTP/1.0\r\n\r\n";

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

}

EOF

配置nginx

cat > /usr/local/nginx/conf/vhost/up.conf <

server {

listen 80;

server_name localhost;

location /view {

check_status;

access_log off;

}

location /api/kuasheng/behavior {

proxy_pass http://behavior/;

}

location /interiorappapi/ {

proxy_pass http://kuasheng-gzzs;

}

error_page 502 /502.json;

location /502.json {

}

error_page 404 /404.json;

location /404.json {

}

}

EOF

3.测试

#添加behavior和kuasheng-gzzs值

etcdctl -C http://192.168.1.11:2379 set /behavior "[\"192.168.1.11:10900\", \"192.168.1.12:10900\"]"

etcdctl -C http://192.168.1.11:2379 set /kuasheng-gzzs "[\"192.168.1.11:80\", \"192.168.1.12:80\"]"

#自动生成 /usr/local/nginx/conf/vhost/up.conf文件

upstream behavior{

server 192.168.1.11:10900 max_fails=2 fail_timeout=40s weight=10;

server 192.168.1.20:10900 max_fails=2 fail_timeout=40s weight=10;

check_http_send "HEAD /info HTTP/1.0\r\n\r\n";

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

}

upstream kuasheng-gzzs{

server 192.168.1.11:10900 max_fails=2 fail_timeout=40s weight=10;

server 192.168.1.20:10900 max_fails=2 fail_timeout=40s weight=10;

check_http_send "HEAD /interiorappapi/actuator/info HTTP/1.0\r\n\r\n";

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

}

 类似资料: