Hyperledger Fabric是強調運維的區塊鏈,Fabric自1.4版本開始就包含了用於peer和orderer節點運維的特性。本教程將介紹如何配置Fabric網絡節點的運維管理服務,以及如何使用Prometheus和statsD/Graphite來可視化監控Hyperledger Fabric網絡中各節點的實時運行指標。linux
相關教程: Fabric區塊鏈Java開發詳解 | Fabric區塊鏈Node.JS開發詳解git
Hyperledger Fabric 1.4提供了以下的特性用於peer和orderer節點的運維服務API:github
配置Fabric區塊鏈節點的運維服務雖然不是尖端的火箭科技,可是若是你漏掉了某些細節也會以爲不那麼容易。docker
首先修改core.yaml來配置peer節點的運維服務,主要包括監聽地址的配置和TLS的配置(咱們先暫時禁用這部分)。json
用編輯器打開core.yaml:api
$ vi ~/fabric-samples/config/core.yaml
下圖顯示了peer節點的運維服務監聽地址listenAddress
的默認值:bash
如今讓咱們啓動BYFN網絡,並試着訪問日誌等級api。網絡
$ cd ~/fabric-samples/first-network $ ./byfn.sh -m up $ docker exec -it cli bash # curl peer0.org1.example.com:9443/logspec
不幸的是,curl命令返回以下錯誤:app
curl: (7) Failed to connect to peer0.org1.example.com port 9443: Connection refused
讓咱們看看究竟是什麼緣由。運維
首先檢查咱們的運維服務配置,打開core.yaml文件:
# vi /etc/hyperledger/fabric/core.yaml
可能你還記得,咱們使用127.0.0.1做爲listenAddress的值,這意味着 從外部沒法訪問運維api。
讓咱們在peer容器內進行必要的操做以再次檢查。此次咱們將使用wget代替 curl,由於在容器內沒有安裝curl。
$ docker exec -it peer0.org1.example.com bash # wget peer0.org1.example.com:9443/logspec
和預期的同樣,錯誤信息再次出現:
Connecting to peer0.org1.example.com (peer0.org1.example.com)… failed: Connection refused
可是若是用127.0.0.1來鏈接就會成功:
# wget 127.0.0.1:9443/logspec
結果以下:
這標明咱們須要設置運維的監聽地址。
爲此,修改docker-compose文件,爲每一個peer指定CORE_OPERATIONS_LISTENADDRESS環境變量。
$ vi ~/fabric-samples/first-network/base/docker-compose-base.yaml
參考下圖進行修改:
如今讓咱們再次嘗試訪問/logspec這個API,別忘了從新啓動BYFN網絡。
$ docker exec -it cli bash # curl peer0.org1.example.com:9443/logspec
輸出結果以下:
相似的,咱們能夠檢查節點健康情況:
# curl peer1.org1.example.com:9443/healthz
輸出結果以下:
最後,讓咱們在docker-compose-base.yaml中爲每一個peer設置ORE_METRICS_PROVIDER=prometheus
來啓用prometheus指標,並修改core.yaml來聲明指標提供器爲prometheus:
若是配置正確,在cli容器內執行以下命令後將會輸出一組監控指標的當前值:
首先下載最新版本的Prometheus並解壓:
$ curl -LO https://github.com/prometheus/prometheus/releases/download/v2.7.1/prometheus-2.7.1.linux-amd64.tar.gz \ $ tar -xvzf prometheus-2.7.1.linux-amd64.tar.gz
在prometheus文件夾中能夠找到prometheus.yml文件。咱們須要修改這個文件以便抓取Hyperledger Fabric的運行指標數據:在scrape_configs部分添加job_name和targets。
如今咱們能夠用docker來運行prometheus:
$ sudo docker run -d --name prometheus-server -p 9090:9090 \ --restart always \ -v /home/mccdev/prometheus/prometheus/prometheus.yml:/prometheus.yml \ prom/prometheus \ --config.file=/prometheus.yml
注意因爲docker老是在專用網絡啓動容器,咱們須要將prometheus容器加入fabric網絡。用以下命令查看fabric網絡:
$ docker inspect peer1.org1.example.com
將prometheus容器接入fabric網絡:
$ sudo docker network connect net_byfn 5b8cbf9d8fa6
能夠在以下地址訪問統計信息:http://localhost:9090/
要檢查是否成功抓取了peer的運行指標,輸入scrape_samples_scraped查看結果表,內容應該非空。
如今咱們換StatsD來可視化監控Fabric網絡的運行指標,Prometheus是pull方式,而StatsD則採用push方式。
首先,咱們須要先從這裏獲取Graphite和StatsD的docker鏡像。
而後,啓動graphite容器:
$ docker run -d\ --name graphite\ --restart=always\ -p 80:80\ -p 2003-2004:2003-2004\ -p 2023-2024:2023-2024\ -p 8125:8125/udp\ -p 8126:8126\ graphiteapp/graphite-statsd
如今該修改docker-copose-base.yaml了。每一個peer都應該設置以下的環境變量:
CORE_OPERATIONS_LISTENADDRESS CORE_METRICS_PROVIDER CORE_METRICS_STATSD_ADDRESS CORE_METRICS_STATSD_NETWORK CORE_METRICS_STATSD_PREFIX
咱們也應當確保指定端口8125。peer的配置示例以下:
peer0.org1.example.com: container_name: peer0.org1.example.com extends: file: peer-base.yaml service: peer-base environment: - CORE_PEER_ID=peer0.org1.example.com - CORE_PEER_ADDRESS=peer0.org1.example.com:7051 - CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org1.example.com:7051 - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051 - CORE_PEER_LOCALMSPID=Org1MSP # metrics - CORE_OPERATIONS_LISTENADDRESS=peer0.org1.example.com:8125 - CORE_METRICS_PROVIDER=statsd - CORE_METRICS_STATSD_ADDRESS=graphite:8125 - CORE_METRICS_STATSD_NETWORK=udp - CORE_METRICS_STATSD_PREFIX=PEER_01 volumes: - /var/run/:/host/var/run/ - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls - peer0.org1.example.com:/var/hyperledger/production ports: - 7051:7051 - 7053:7053 - 8125:8125
須要留意使用的端口:對於peer0,端口應當是8125:8125,peer1則應當使用9125:8125,依此類推。
Orderer應當按以下配置:
- ORDERER_OPERATIONS_LISTENADDRESS=orderer.example.com:8125 - ORDERER_METRICS_PROVIDER=statsd - ORDERER_METRICS_STATSD_ADDRESS=graphite:8125 - ORDERER_METRICS_STATSD_NETWORK=udp - ORDERER_METRICS_STATSD_PREFIX=ORDERER ports: - 7125:8125
如今讓咱們啓動BYFN網絡。
記得將graphite容器接入BYFN網絡,不然你會看到以下錯誤:
Error: error getting endorser client for channel: endorser client failed to connect to peer0.org1.example.com:7051: failed to create new connection: context deadline exceeded
peer容器的日誌將顯示在網絡中沒有找到graphite:
Error: failed to initialize operations subystems: dial udp: lookup graphite on 127.0.0.11:53: no such host
使用以下命令將graphite容器接入fabric網絡:
$ sudo docker network connect net_byfn graphite
一切正常的話,你應該能夠在以下地址看到運行指標:
http://localhost/metrics/index.json
結果以下:
訪問以下地址來查看可視化的數據:
http://localhost/
結果頁面以下:
在這個教程中,咱們學習瞭如何配置hyperledger Fabric的節點的運維服務API,以及如何使用Prometheus或StatsD來可視化監控Hyperledger Fabric的運行狀況。若是你但願繼續深刻學習Hyperledger Fabric區塊鏈鏈碼及應用的開發,能夠參考這兩個在線互動教程: