consul & registrator & consul-template 使用
consul & registrator & consul-template 使用
參考這裏的文章:
https://www.jianshu.com/p/a4c04a3eeb57
docker-compose.yml
version: '3' services: web: image: liberalman/helloworld:latest environment: SERVICE_80_NAME: my-web-server SERVICE_TAGS: backend-1 MY_HOST: host-1 ports: - "80" lb: image: liberalman/nginx-consul-template:latest hostname: lb links: - consulserver:consul ports: - "80:80" consulserver: image: progrium/consul:latest environment: SERVICE_TAGS: consul servers hostname: consulserver ports: - "8300" - "8400" - "8500:8500" - "53" command: -server -ui-dir /ui -data-dir /tmp/consul -bootstrap-expect 1 registrator: image: gliderlabs/registrator:master hostname: registrator links: - consulserver:consul volumes: - "/var/run/docker.sock:/tmp/docker.sock" command: -internal consul://consul:8500
docker-compose up
docker-compose up --scale web=3
停掉其中一個container ,看看發生什麽事情導致能夠檢測到節點掛掉,並不轉發流量?
關鍵處:訂閱了狀態的結果後,動態改變了nginx的配置。效果:
單機的架構、原理如下:
or 參考:
consul 只是其中一種註冊中心的實現,registrator support zk&consule/etcd/skydns2
registrator 主動檢查
https://github.com/gliderlabs/registrator/blob/master/registrator.go
根據tick 的到來,registrator.go 做幾件事情:
- resync interval 到達,掃描, 同步容器狀態 refresh :
- alive 的container,
- delete 到consul
- refresh interval 到達,sync :
- list 當前的container
- 移除不健康的container
- deregister 不健康的container
看到這裏,這種基於registrator的主動檢測,解耦服務於註冊中心的代碼
ref
分布式的配置
每個宿主機都要配置 registrator ,因為只能同一臺宿主機去掃描container 的狀態,註意紅框處:
集群的架構
consul vs zk , etcd
https://www.consul.io/intro/vs/zookeeper.html
vs traditional: nginx + lua + jenkins
傳統的發布模式:
- 服務需要配置健康檢查
- 添加新的服務節點要運維人為操作下到nginx 分發
- lua 要配置好藍綠發布狀態的控制
- 如果傳統的配置中心:
- 服務要配置註冊中心 register deregister,耦合
而現在,只需要簡單的配置增加服務節點,就好了
websocket/https
ref :
https://www.jianshu.com/p/a4c04a3eeb57
https://segmentfault.com/a/1190000005026022
https://segmentfault.com/a/1190000013720661
https://segmentfault.com/a/1190000005040914
https://learn.hashicorp.com/consul/getting-started/checks.html
https://www.consul.io/docs/agent/checks.html
https://my.oschina.net/guol/blog/353101
https://www.jianshu.com/p/f8746b81d65d
https://medium.com/eleven-labs/consul-service-discovery-and-failure-detection-64b06a5cbce6
https://www.cnblogs.com/zhangdk/p/Registrator_reference.html
https://kevinguo.me/2017/09/01/docker-consul-consul-template-registrator-nginx/#nginx-with-consul-template
https://juejin.im/post/5b59d64b6fb9a04fd1602017
consul & registrator & consul-template 使用