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

4建立Fabric多節點集羣

4.1配置說明

首先能夠根據官方Fabric自帶的e2e_cli列子中的集羣方案來生成咱們本身的集羣,與案例不一樣的是咱們須要把容器都分配到不一樣的服務器上,彼此之間經過網絡來進行通訊,網絡構建完成後則進行相關的channel和chaincode操做。html

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

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

名稱docker

ip安全

節點標識bash

節點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/Admin@org1.example.com/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/Admin@org1.example.com/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的多機部署,在這位老師的博客裏寫的很是清楚,並且我也是參考這篇博客來完成多機多節點自動部署方案的,只是手動調配及後續操做在網上基本上沒有可供參考的中文內容,須要查看官網文檔來逐步實現。

相關文章
相關標籤/搜索