consul & registrator & consul-template 使用

consul & registrator & consul-template 使用

參考這裏的文章:
https://www.jianshu.com/p/a4c04a3eeb57html

docker-compose.ymlnginx

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 upgit

docker-compose up --scale web=3
執行效果github

停掉其中一個container ,看看發生什麼事情致使可以檢測到節點掛掉,並不轉發流量?
關鍵處:訂閱了狀態的結果後,動態改變了nginx的配置。效果:web

template

單機的架構、原理以下:
Alt text
or 參考:
Alt textdocker

consul 只是其中一種註冊中心的實現,registrator support zk&consule/etcd/skydns2bootstrap

registrator 主動檢查

https://github.com/gliderlabs/registrator/blob/master/registrator.go
根據tick 的到來,registrator.go 作幾件事情:segmentfault

registrator.go

  • resync interval 到達,掃描, 同步容器狀態 refresh :
    • alive 的container,
    • delete 到consul
  • refresh interval 到達,sync :
    • list 當前的container
    • 移除不健康的container
    • deregister 不健康的container

看到這裏,這種基於registrator的主動檢測,解耦服務於註冊中心的代碼websocket

ref架構

分佈式的配置

每一個宿主機都要配置 registrator ,由於只能同一臺宿主機去掃描container 的狀態,注意紅框處:

Alt text

集羣的架構

architecture

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
http://www.javashuo.com/article/p-gtwwndim-ey.html
http://www.javashuo.com/article/p-unnqpyye-bn.html
http://www.javashuo.com/article/p-cdfmxbry-hy.html
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
http://www.javashuo.com/article/p-qqnpmfjo-gw.html

相關文章
相關標籤/搜索