Consul-template配置文件

文件结构:

 

 nginx.conf.ctmpl2

{{range services}}  {{$name := .Name}} {{$service := service .Name}}

upstream {{$name}} {
  zone upstream-{{$name}} 64k;
  {{range $service}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1;
  {{else}}server 127.0.0.1:65535 down; # force a 502{{end}}
} 
{{end}}

server {
  listen 80 default_server;
  server_name 127.0.0.1;

{{range services}} {{$name := .Name}} {{$namelower := .Name|toLower}}
  location /{{$name | replaceAll "_" "/" }}/ {
    proxy_pass http://{{$name}}/;
  }
{{if ne $name $namelower}}
location /{{$name | toLower | replaceAll "_" "/" }}/ {
    proxy_pass http://{{$name}}/;
  }
{{end}}
{{end}}
}

nginx.hc1

consul {
  address = "127.0.0.1:8500"
}

template {
  source = "nginx.conf.ctmpl2"
  destination = "/etc/nginx/conf.d/consul-template.conf"
  command = "./start_nginx.sh"
}

start_nginx.sh

#!/bin/sh
ps -ef |grep nginx |grep -v grep |grep -v .sh
if [ $? -ne 0 ]; then
 nginx;
 echo "nginx start"
else
 nginx -s reload;
 echo "nginx reload"
fi

 参考文档: 通过 Consul-Template 实现动态配置Nginx负载服务-阿里云开发者社区 (aliyun.com)

posted @ 2022-02-28 22:30  唐磊(Jason)  阅读(128)  评论(0)    收藏  举报