Docker+Consul+Registrator 實現服務注冊與發現


Docker+Consul+Registrator實現服務注冊與發現

邏輯圖 實現nginx節點自動化加入容器IP代理

1、三台Consul agent server作為高可用通過Consul Template實時監測Leader的變化。

2、Leader如果變化就觸發渲染模板動作,執行相應命令。

3、如圖 如果Node01 docker創建了一個nginx web容器隨機暴露了一個24123端口,Registrator會監聽到這個事件之后,會將宿主級IP與暴露的端口注冊到本地的Consul agent client。

4、Consul agent Clinet 再將這個請求轉發到 Consul agent service中。


環境說明

  • Linux系統版本:Linux Centos 7.4 x64
  • Consul應用版本:consul_1.4.0
  • Template應用版本:consul-template_0.19.3
  • Docker應用版本:18.09.0
  • Consul管理節點:192.168.1.79
  • Docker工作節點:192.168.1.78
  • Docker工作節點:192.168.1.77

1、管理節點安裝 Consul 服務端

1.1、下載解壓安裝包Consul

  • Consul下載地址:https://pan.baidu.com/s/13fFhJwj6TxXkAB0tOLo8Sg
  • 密碼:hjub
# 解壓出一個二進制包
unzip unzip consul_1.4.0_linux_amd64.zip
# 移動到bin目錄下
mv consul /usr/bin

1.2、server模式初始化集群

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.3、查看管理節點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信息
命令:consul members

Node  Address            Status  Type    Build  Protocol  DC   Segment
test  192.168.1.79:8301  alive   server  1.4.0  2         dc1  <all>
查看集群成員
查看leaderIP地址
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 # 集群節點詳細信息
通過HTTP API獲取集群信息
類型        描述
kv         鍵值存儲
agent     agent管理
catalog     nodes和services管理
health     健康檢查
session     session管理
acl         ACL管理
event     用戶事件
status     Consul系統狀態
Consul API管理  

 

2、工作節點安裝Consul 客戶端

2.1、下載解壓安裝包到本地

# 解壓出一個二進制包
unzip unzip consul_1.4.0_linux_amd64.zip
# 移動到bin目錄下
mv consul /usr/bin

2.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.3、查看工作節點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.4、管理節點:web頁面測試查看

http://192.168.1.79:8500/ui/dc1/services

查看節點

 

3、管理節點:注冊使用

3.1、注冊服務

# 案例:
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.2、查看注冊服務

 

4、管理節點工作節點安裝Dokcer

 

5、工作節點:安裝Registrator與template

5.1、Registrator簡介

consul-template:一個守護程序,用於實時查詢consul集群數據,並更新文件系統上的任意數量的指定模板,生成配置文件,更新完成后可以選擇運行任何命令。

gliderlabs/registrator:檢查容器運行狀態自動注冊到服務中心。registrator支持服務注冊有consul、etcd、SkyDNS2。

5.2、工作節點1節點2:下載解壓部署

  • Template下載地址:https://pan.baidu.com/s/1N9U89ClxY-fpTfxiP8l3Tw
  • 密碼:wso1
# 解壓文件
unzip consul-template_0.19.3_linux_amd64.zip
# 移動文件到bin目錄下
mv consul-template /usr/bin/

5.3、工作節點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.4、瀏覽器查看注冊情況

 

 

 

6、實現Nginx集群節點自動加入

6.1、工作節點1節點2:啟動nginx容器

docker run -d -P nginx

6.2、工作節點1節點2:瀏覽器查看服務

前提:管理節點(推薦)環境安裝好nginx服務設置代理

6.3、管理節點:創建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.4、管理節點:配置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.5、管理節點:測試查看生成結果

命令: 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.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容器都執行動作。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM