1. 程式人生 > >Hyperledger Fabric 1.0 從零開始(六)——創建Fabric多節點集群

Hyperledger Fabric 1.0 從零開始(六)——創建Fabric多節點集群

_id 測試 es2017 xtra 去掉 compose 多個 服務 執行命令

4創建Fabric多節點集群

4.1配置說明

首先可以根據官方Fabric自帶的e2e_cli列子中的集群方案來生成我們自己的集群,與案例不同的是我們需要把容器都分配到不同的服務器上,彼此之間通過網絡來進行通信,網絡構建完成後則進行相關的channel和chaincode操作。

筆者目前申請了五臺服務器,所有的服務器均是按照上述e2e_cli環境構建與測試步驟配置。計劃其中四臺服務器運行peer節點,另外一臺服務器運行orderer節點,為其它四個節點提供order服務。

虛擬機具體參數如下表所示:

名稱

ip

節點標識

節點Hostname

Organization(組織機構)

Server1

10.130.116.8

orderer

orderer.example.cn

Orderer

Server2

10.130.116.9

sp0

peer0.org1.example.cn

Org1

Server3

10.130.116.10

sp1

peer1.org1.example.cn

Org1

Server4

10.130.116.25

sp2

peer0.org2.example.cn

Org2

Server5

10.130.116.27

sp3

Peer1.org2.example.cn

Org2

4.2、生成公私鑰、證書、創世區塊等

公私鑰和證書是用於Server與Server之間的安全通信,另外要創建channel並讓其它節點加入channel就需要創世區塊,這些必備文件都可以通過一個命令生成,並且官方已經給出了腳本,在如下目錄中的文件:

/opt/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli/ generateArtifacts.sh

使用generateArtifacts.sh生成證書和config.tx,具體執行命令如下:

bash generateArtifacts.sh mychannel

這裏創建了5臺服務器,在任意一臺服務器的該目錄下執行此項命令即可,將會生成兩個目錄,它們分別為channel-artifacts和crypto-config,兩個目錄的結果和含義如下視圖:

技術分享

在上述目錄裏的文件用於orderer創建channel,它們根據configex.yaml的配置生成。

技術分享

在上述目錄裏有orderer和peer的證書、私鑰和用於通信加密的tls證書等文件,它通過configex.yaml配置文件生成。

4.3、配置多服務器

根據4.2的方案,以及之前所述的gopath目錄等配置方案,我們假定所有的服務器都按照該文檔的配置來操作,所有服務器都有Fabric源碼且目錄為

/opt/gopath/src/github.com/hyperledger/fabric

我們在Server1上執行如下命令生成構建Fabric網絡所需的成員證書等必要材料:

bash generateArtifacts.sh mychannel

如4.2中所述,該命令只需在其中某一臺服務器上運行一次即可,其它服務器無需再次運行。

在運行該命令的服務器/opt/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli目錄下會生成channel-artifacts和crypto-config目錄,需要把它們拷貝到其它服務器相同的e2e_cli目錄下,如果在其它服務器中已經存在該目錄,則先把此目錄刪除。

當所有服務器都有同一個channel-artifacts和crypto-config目錄後,接下來就開始配置compose文件。

4.4設置peer0.org1.example.com節點的docker-compose文件

e2e_cli中提供了多個yaml文件,我們可以基於docker-compose-cli.yaml文件創建,具體可執行如下命令:

cp docker-compose-cli.yaml docker-compose-peer.yaml

然後修改docker-compose-peer.yaml,去掉orderer的配置,只保留一個peer和cli,因為我們要多級部署,節點與節點之前又是通過主機名通訊,所以需要修改容器中的host文件,也就是extra_hosts設置,修改後的peer配置如下:

1 peer0.org1.example.com:
2     container_name: peer0.org1.example.com
3     extends:
4       file:  base/docker-compose-base.yaml
5       service: peer0.org1.example.com
6     extra_hosts:
7      - "orderer.example.com:10.130.116.8"

同樣,cli也需要能夠和各個節點通訊,所以cli下面也需要添加extra_hosts設置,去掉無效的依賴,並且去掉command這一行,因為我們是每個peer都會有個對應的客戶端,也就是cli,所以我只需要去手動執行一次命令,而不是自動運行。修改後的cli配置如下:

 1 cli:
 2     container_name: cli
 3     image: hyperledger/fabric-tools
 4     tty: true
 5     environment:
 6       - GOPATH=/opt/gopath
 7       - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
 8       - CORE_LOGGING_LEVEL=DEBUG
 9       - CORE_PEER_ID=cli
10       - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
11       - CORE_PEER_LOCALMSPID=Org1MSP
12       - CORE_PEER_TLS_ENABLED=true
13       - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
14       - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
15       - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
16       - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
17     working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
18     volumes:
19         - /var/run/:/host/var/run/
20         - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
21         - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
22         - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
23         - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
24     depends_on:
25       - peer0.org1.example.com
26     extra_hosts:
27      - "orderer.example.com:10.130.116.8"
28      - "peer0.org1.example.com:10.130.116.9"
29      - "peer1.org1.example.com:10.130.116.10"
30      - "peer0.org2.example.com:10.130.116.25"
31      - "peer1.org2.example.com:10.130.116.27"

3.2示例單機模式下,4個peer會映射主機不同的端口,但是我們在多機部署的時候是不需要映射不同端口的,所以需要修改base/docker-compose-base.yaml文件,將所有peer的端口映射都改為相同的:

1 ports: 
2   - 7051:7051 
3   - 7052:7052 
4   - 7053:7053

4.5設置peer1.org1.excmple.com節點的docker-compose文件

與peer0.org1.example.com節點compose文件配置一樣,不過需要將啟動的容器改為peer1.org1.example.com,並且添加peer0.org1.example.com的IP映射,對應的cli中也改成對peer1.org1.example.com的依賴。這是修改後的peer1.org1.example.com上的配置示例:

 1 peer1.org1.example.com:
 2     container_name: peer1.org1.example.com
 3     extends:
 4       file:  base/docker-compose-base.yaml
 5       service: peer1.org1.example.com
 6     extra_hosts:
 7      - "orderer.example.com:10.130.116.8"
 8      - "peer0.org1.example.com:10.130.116.9"
 9 
10   cli:
11     container_name: cli
12     image: hyperledger/fabric-tools
13     tty: true
14     environment:
15       - GOPATH=/opt/gopath
16       - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
17       - CORE_LOGGING_LEVEL=DEBUG
18       - CORE_PEER_ID=cli
19       - CORE_PEER_ADDRESS=peer1.org1.example.com:7051
20       - CORE_PEER_LOCALMSPID=Org1MSP
21       - CORE_PEER_TLS_ENABLED=true
22       - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.crt
23       - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.key
24       - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt
25       - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
26     working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
27     volumes:
28         - /var/run/:/host/var/run/
29         - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
30         - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
31         - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
32         - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
33     depends_on:
34       - peer1.org1.example.com
35     extra_hosts:
36      - "orderer.example.com:10.130.116.8"
37      - "peer0.org1.example.com:10.130.116.9"
38      - "peer1.org1.example.com:10.130.116.10"
39      - "peer0.org2.example.com:10.130.116.25"
40      - "peer1.org2.example.com:10.130.116.27"

與上述類似,可以再次設置peer0.org2.example.com及peer1.org2.example.com節點compose配置文件。

4.6設置order節點的docker-compose文件

與創建peer的配置文件類似,我們也復制一個yaml文件出來進行修改:

cp docker-compose-cli.yaml docker-compose-orderer.yaml

orderer服務器上我們只需要保留order設置,其他peer和cli設置都可以刪除。orderer可以不設置extra_hosts。

以下是order compose的配置示例:

1 orderer.example.com:
2     extends:
3       file:   base/docker-compose-base.yaml
4       service: orderer.example.com
5     container_name: orderer.example.com

本章節及後面的一章可以直接參閱:Fabric 1.0的多機部署,在這位老師的博客裏寫的非常清除,而且我也是參考這篇博客來完成多機多節點自動部署方案的,只是手動調配及後續操作在網上基本上沒有可供參考的中文內容,需要查看官網文檔來逐步實現。

Hyperledger Fabric 1.0 從零開始(六)——創建Fabric多節點集群