Docker+Consul+Registrator實現服務註冊與發現html
邏輯圖 實現nginx節點自動化加入容器IP代理node
一、三臺Consul agent server做爲高可用經過Consul Template實時監測Leader的變化。linux
二、Leader若是變化就觸發渲染模板動做,執行相應命令。nginx
三、如圖 若是Node01 docker建立了一個nginx web容器隨機暴露了一個24123端口,Registrator會監聽到這個事件以後,會將宿主級IP與暴露的端口註冊到本地的Consul agent client。web
四、Consul agent Clinet 再將這個請求轉發到 Consul agent service中。算法
環境說明docker
一、管理節點安裝 Consul 服務端bootstrap
1.一、下載解壓安裝包Consul瀏覽器
# 解壓出一個二進制包 unzip unzip consul_1.4.0_linux_amd64.zip # 移動到bin目錄下 mv consul /usr/bin
1.二、server模式初始化集羣session
consul agent \ -server \ -bootstrap \ -ui \ -data-dir=/var/lib/consul-data \ -bind=192.168.1.79 \ -client=0.0.0.0 \ -node=test
# 使用命令 consul agent \ # 設置server模式 -server \ # 自選舉爲本身Leader -bootstrap \ # 啓用內置web頁面 -ui \ # 集羣數據保存位置 -data-dir=/var/lib/consul-data \ # 集羣之間通信綁定地址 -bind=192.168.1.79 \ # 客戶端訪問地址 -client=0.0.0.0 \ # 指定管理節點名稱默認主機名,必須是惟一的 -node=test
==> Consul agent running! # 軟件版本1.4 Version: 'v1.4.0' Node ID: '0f09693a-f66e-08f2-6199-0523c27d010d' # node節點爲test Node name: 'test' # 數據中心默認名稱dc1 Datacenter: 'dc1' (Segment: '<all>') Server: true (Bootstrap: true) # client可訪問IP地址 Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, gRPC: -1, DNS: 8600) # 集羣IP地址,設置爲管理節點地址 Cluster Addr: 192.168.1.79 (LAN: 8301, WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
1.三、查看管理節點Consul啓動信息
命令:netstat -antp | grep consul # 集羣節點 tcp 0 0 192.168.1.79:8300 0.0.0.0:* LISTEN 68220/consul # 集羣內部訪問 tcp 0 0 192.168.1.79:8301 0.0.0.0:* LISTEN 68220/consul # 誇數據中心訪問 tcp 0 0 192.168.1.79:8302 0.0.0.0:* LISTEN 68220/consul tcp 0 1 192.168.1.79:52742 192.168.1.78:8301 SYN_SENT 68220/consul tcp 0 0 192.168.1.79:8300 192.168.1.79:55923 ESTABLISHED 68220/consul tcp 0 0 192.168.1.79:55923 192.168.1.79:8300 ESTABLISHED 68220/consul tcp 0 0 192.168.1.79:8300 192.168.1.77:44698 ESTABLISHED 68220/consul # HTTP tcp6 0 0 :::8500 :::* LISTEN 68220/consul # DNS tcp6 0 0 :::8600 :::* LISTEN 68220/consul
命令:consul info agent: check_monitors = 0 check_ttls = 0 checks = 0 services = 0 build: prerelease = revision = 0bddfa23 version = 1.4.0 consul: acl = disabled bootstrap = true known_datacenters = 1 leader = true # leader地址 leader_addr = 192.168.1.79:8300 server = true # 保持選舉一致性算法 raft: applied_index = 22 commit_index = 22 fsm_pending = 0 last_contact = 0 last_log_index = 22 last_log_term = 2 last_snapshot_index = 0 last_snapshot_term = 0 latest_configuration = [{Suffrage:Voter ID:0f09693a-f66e-08f2-6199-0523c27d010d Address:192.168.1.79:8300}] latest_configuration_index = 1 num_peers = 0 protocol_version = 3 protocol_version_max = 3 protocol_version_min = 0 snapshot_version_max = 1 snapshot_version_min = 0 state = Leader term = 2 runtime: arch = amd64 cpu_count = 2 goroutines = 81 max_procs = 2 os = linux version = go1.11.1 serf_lan: coordinate_resets = 0 encrypted = false event_queue = 1 event_time = 2 failed = 0 health_score = 0 intent_queue = 0 left = 0 member_time = 1 members = 1 query_queue = 0 query_time = 1 serf_wan: coordinate_resets = 0 encrypted = false event_queue = 0 event_time = 1 failed = 0 health_score = 0 intent_queue = 0 left = 0 member_time = 1 members = 1 query_queue = 0 query_time = 1
命令:consul members
Node Address Status Type Build Protocol DC Segment
test 192.168.1.79:8301 alive server 1.4.0 2 dc1 <all>
consul catalog services
curl 127.0.0.1:8500/v1/status/peers # 集羣server成員 curl 127.0.0.1:8500/v1/status/leader # 集羣Raft leader curl 127.0.0.1:8500/v1/catalog/services # 註冊的全部服務 curl 127.0.0.1:8500/v1/catalog/services/nginx # 服務信息 curl 127.0.0.1:8500/v1/catalog/nodes # 集羣節點詳細信息
類型 描述
kv 鍵值存儲
agent agent管理
catalog nodes和services管理
health 健康檢查
session session管理
acl ACL管理
event 用戶事件
status Consul系統狀態
二、工做節點安裝Consul 客戶端
2.一、下載解壓安裝包到本地
# 解壓出一個二進制包 unzip unzip consul_1.4.0_linux_amd64.zip # 移動到bin目錄下 mv consul /usr/bin
2.二、client模式加入集羣
工做節點1
docker run -d \ --name=consul \ -p 8301:8301 \ -p 8301:8301/udp \ -p 8500:8500 \ -p 8600:8600 \ -p 8600:8600/udp \ --restart=always \ progrium/consul \ -join 192.168.1.79 -advertise 192.168.1.77 -client 0.0.0.0 -node=node01
工做節點2
docker run -d \ --name=consul \ -p 8301:8301 \ -p 8301:8301/udp \ -p 8500:8500 \ -p 8600:8600 \ -p 8600:8600/udp \ --restart=always \ progrium/consul \ -join 192.168.1.79 -advertise 192.168.1.78 -client 0.0.0.0 -node=node02
# 運行 docker run -d \ # 指定名稱 --name=consul \ # 指定暴露端口 -p 8301:8301 \ -p 8301:8301/udp \ # 提供HTTP服務端口 -p 8500:8500 \ # 提供DNS服務端口 -p 8600:8600 \ -p 8600:8600/udp \ # 設置重啓策略 --restart=always \ # 指定鏡像 progrium/consul \ # 加入現有集羣IP -join 192.168.1.79 # 通告地址,本機地址 -advertise 192.168.1.77 # 經過那裏鏈接本地 -client 0.0.0.0 # 指定管理節點名稱默認主機名,必須是惟一的 -node=node01
2.三、查看工做節點consul啓動狀況
命令:docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7c82880b3685 progrium/consul "/bin/start -join 19…" 3 minutes ago Up 9 seconds 53/tcp, 8300/tcp, 53/udp, 8302/tcp, 0.0.0.0:8301->8301/tcp, 0.0.0.0:8500->8500/tcp, 0.0.0.0:8301->8301/udp, 8400/tcp, 8302/udp, 0.0.0.0:8600->8600/tcp, 0.0.0.0:8600->8600/udp consul
命令:consul members Node Address Status Type Build Protocol DC Segment test 192.168.1.79:8301 alive server 1.4.0 2 dc1 <all> node01 192.168.1.77:8301 alive client 0.5.2 2 dc1 <default> node02 192.168.1.78:8301 failed client 0.5.2 2 dc1 <default>
注:關閉防火牆攔截。
2.四、管理節點:web頁面測試查看
http://192.168.1.79:8500/ui/dc1/services
查看節點
三、管理節點:註冊使用
3.一、註冊服務
# 案例: curl -X PUT -d \ '{"id": "jetty","name": "service_name","address": "192.168.1.77","port": 8080,"tags": ["test"],"checks": [{"http": "http://192.168.1.77:8080/","interval": "5s"}]}' \ http://127.0.0.1:8500/v1/agent/service/register
3.二、查看註冊服務
五、工做節點:安裝Registrator與template
5.一、Registrator簡介
consul-template:一個守護程序,用於實時查詢consul集羣數據,並更新文件系統上的任意數量的指定模板,生成配置文件,更新完成後能夠選擇運行任何命令。
gliderlabs/registrator:檢查容器運行狀態自動註冊到服務中心。registrator支持服務註冊有consul、etcd、SkyDNS2。
5.二、工做節點1節點2:下載解壓部署
# 解壓文件 unzip consul-template_0.19.3_linux_amd64.zip # 移動文件到bin目錄下 mv consul-template /usr/bin/
5.三、工做節點1節點2:建立registrator項目
docker run -d \ --name=registrator \ -v /var/run/docker.sock:/tmp/docker.sock \ --restart=always \ gliderlabs/registrator:latest \ consul://192.168.1.77:8500
# docker run -d \ # 指定容器名稱 --name=registrator \ # 給容器掛載存儲卷,掛載到容器的某個目錄 -v /var/run/docker.sock:/tmp/docker.sock \ # 添加劇啓策略 --restart=always \ # 指定鏡像 gliderlabs/registrator:latest \ # 指定docker本地IP,鏈接註冊到consul client。 consul://192.168.1.77:8500 注:它能夠將以前建立的容器註冊。
5.四、瀏覽器查看註冊狀況
六、實現Nginx集羣節點自動加入
6.一、工做節點1節點2:啓動nginx容器
docker run -d -P nginx
6.二、工做節點1節點2:瀏覽器查看服務
前提:管理節點(推薦)環境安裝好nginx服務設置代理
6.三、管理節點:建立nginx代理模板
vi nginx.ctmpl
upstream http_backend { ip_hash; # nginx 爲服務名稱 {{range service "nginx"}} # 服務名 服務IP:端口號 server {{ .Address }}:{{ .Port }}; # 結束 {{ end }} } server { listen 80; server_name localhost; location / { proxy_pass http://http_backend; } }
6.四、管理節點:配置template自動部署
consul-template \ -consul-addr 192.168.1.79:8500 \ -template "./nginx.ctmpl:/etc/nginx/conf.d/default2.conf:/usr/sbin/nginx -s reload" \ -log-level=info
# 使用template命令 consul-template \ # 指定consul server地址 -consul-addr 192.168.1.79:8500 \ # 指定 新配置文件:生成配置文件:重載配置文件 -template "./nginx.ctmpl:/etc/nginx/conf.d/default2.conf:/usr/sbin/nginx -s reload" \ # 指定日誌級別 -log-level=info
6.五、管理節點:測試查看生成結果
命令: cat default2.conf upstream http_backend { ip_hash; server 192.168.1.77:32768; server 192.168.1.77:32769; server 192.168.1.78:32788; } server { listen 80; server_name localhost; location / { proxy_pass http://http_backend; } }
6.六、測試添加了多臺Nginx容器後輸出,驗證自動發現
查看Nginx配置文件
cat default2.conf upstream http_backend { ip_hash; server 192.168.1.77:32772; server 192.168.1.77:32768; server 192.168.1.77:32771; server 192.168.1.77:32773; server 192.168.1.77:32770; server 192.168.1.77:32769; server 192.168.1.78:32789; server 192.168.1.78:32791; server 192.168.1.78:32788; server 192.168.1.78:32790; } server { listen 80; server_name localhost; location / { proxy_pass http://http_backend; } } 注:刪除添加docker容器都執行動做。