consul-template and registrator

https://www.cnblogs.com/zhangdk/p/Registrator_reference.html#registrator%E9%80%89%E9%A1%B9

https://www.hi-linux.com/posts/36431.html    consul-template

https://book-consul-guide.vnzmi.com/11_consul_template.html

{{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; # force a 502{{end}}
} {{end}}

server {
  listen 80 default_server;

  location / {
    root /usr/share/nginx/html/;
    index index.html;
  }

  location /stub_status {
    stub_status;
  }

{{range services}} {{$name := .Name}}
  location /{{$name}} {
    proxy_pass http://{{$name}};
  }
{{end}}
}

consule-nginx

部署:

consule集群    start.sh

nginx与服务发现 registrator   start_nginx.sh

服务注册 consule命令

cat start.sh
docker stop consul1 consul2 consul3
docker rm consul1 consul2 consul3
docker run -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600 \
-v /opt/AI/MICROSERVICES/consul_data:/consul/data \
-e CONSUL_BIND_INTERFACE=eth0 \
–name=consul1 \
consul:1.4.4 \
agent -server -bootstrap-expect=3 -ui -client=0.0.0.0 -bind=0.0.0.0 -node=consul1

consulip=`docker inspect –format ‘{{ .NetworkSettings.Networks.bridge.IPAddress }}’ consul1`

echo $consulip
docker run -d -p 8501:8500 –name=consul2 \
-e CONSUL_BIND_INTERFACE=eth0 \
consul:1.4.4 \
agent -server -ui -bootstrap-expect=3 -client=0.0.0.0 -bind=0.0.0.0 -node=consul2 -join $consulip

docker run -d -p 8502:8500 –name=consul3 \
-e CONSUL_BIND_INTERFACE=eth0 \
consul:1.4.4 \
agent -server -ui -bootstrap-expect=3 -client=0.0.0.0 -bind=0.0.0.0 -node=consul3 -join $consulip

 

cat start_nginx.sh
consulip=`docker inspect –format ‘{{ .NetworkSettings.Networks.bridge.IPAddress }}’ consul1`

echo $consulip

docker stop registrator consultemplate
docker rm registrator consultemplate

docker run -d –name=registrator \
–privileged \
–volume=/var/run/docker.sock:/tmp/docker.sock \
gliderlabs/registrator:latest \
consul://$consulip:8500
docker run –add-host consul:$consulip \
-d –name consultemplate \
-v /opt/AI/MICROSERVICES/nginx.conf.ctmpl:/etc/consul-templates/nginx.conf.ctmpl \
-p 8800:80 liberalman/nginx-consul-template:latest

 

 

cat register_service.sh
curl –request PUT –data @service.json http://localhost:8500/v1/agent/service/register
cat service.json
{
“ID”: “my-web-server1”,
“Name”: “my-web-server”,
“Tags”: [
“primary”,
“v1”
],
“Address”: “10.10.10.253”,
“Port”: 30088,
“EnableTagOverride”: false,
“Check”: {
“DeregisterCriticalServiceAfter”: “90m”,
“HTTP”: “http://10.10.10.253:30088/static/dist/index.html”,
“Interval”: “10s”
}
}