7:Fabric多節點集群生產啟動
7.1、多節點服務器配置
在生產環境上,我們沿用4.1、配置說明中的服務器各節點配置方案。
我們申請了五台生產服務器,其中四台服務器運行peer節點,另外一台服務器運行orderer節點,為其它四個節點提供排序服務。
虛擬機具體參數如下表所示:
名稱 |
ip |
節點標識 |
節點Hostname |
Organization(組織機構) |
Server1 |
10.130.116.8 |
orderer |
orderer.example.com |
Orderer |
Server2 |
10.130.116.9 |
org0 |
peer0.org.example.com |
org |
Server3 |
10.130.116.10 |
orgs0 |
peer0.orgs.example.com |
orgS |
Server4 |
10.130.116.25 |
orgpay0 |
peer0.orgpay.example.com |
orgPAY |
Server5 |
10.130.116.27 |
orgt0 |
peer0.orgt.example.com |
orgT |
7.2、啟動orderer排序服務節點
第六章中提到的所有配置方案可以在任意一台符合條件的服務器上進行生成配置,配置完成后,我們首先要啟動屬於本組織(org)的排序節點服務,啟動之前需要創建一個docker-compose-orderer.yaml文件,該文件可參考fabric-samples-release/first-network/docker-compose-cli.yaml進行個性化編寫。
具體的編寫步驟和內容介紹可參考4.6、設置order節點的docker-compose文件,根據第六章所配置的聯盟環境及本組織名稱等內容及4.5節中的介紹,我們最終得到屬於本組織的專屬docker-compose-orderer.yaml文件,具體內容如下:
version: '2'
services:
orderer.example.com:
extends:
file: base/docker-compose-base.yaml
service: orderer.example.com
container_name: orderer.example.com
這里有一個協助啟動文件,是位於base目錄下的docker-compose-base.yaml文件,這個文件的參數配置如下:
docker-compose-base.yaml
version: '2'
services:
orderer.example.com:
container_name: orderer.example.com
image: hyperledger/fabric-orderer
environment:
- ORDERER_GENERAL_LOGLEVEL=debug
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
working_dir: /opt/gopath/src/org/peer
command: orderer
volumes:
- ../bin/channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ../bin/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
- ../bin/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
ports:
- 7050:7050
文件及目錄編寫完成后上傳至服務器指定位置,本組織的目錄為org,如下視圖:
隨后在org目錄下執行如下命令:
docker-compose -f docker-compose-orderer.yaml up -d
完成后執行如下命令:
docker ps
至此我們在10.130.116.8這台服務器上啟動了orderer排序服務。
7.3、啟動orgMSP peer節點
peer節點的啟動方式可參考4.4、設置peer0.org1.excmple.com節點的docker-compose文件,這里因為在6.6、生成channel下節點集合認證文件已經生成了orgMSP的認證文件,故此我們不必再去創建和生成,為了不必要的混亂,建議各組織生成統一認證的服務器固定。
我們需要將orderer排序服務器上的org/bin目錄下的文件拷貝至org組織所在節點服務器org0,即10.130.116.9服務器的/opt/gopath/src目錄下。其中crypto-config.yaml和configtx.yaml文件無需拷貝,因為具體的證書等生成都交由orderer排序服務器去執行。
這里的啟動步驟與上一節的內容類似,在啟動orgMSP之前需要創建一個docker-compose-org.yaml文件,該文件可參考fabric-samples-release/first-network/docker-compose-cli.yaml進行個性化編寫。
具體的編寫步驟和內容介紹可參考4.4、設置peer0.org1.excmple.com節點的docker-compose文件,根據第六章所配置的聯盟環境及本組織名稱等內容及4.4節中的介紹,我們最終得到屬於本組織的專屬docker-compose-org.yaml文件,具體內容如下:
version: '2'
services:
peer0.org.example.com:
container_name: peer0.org.example.com
extends:
file: base/docker-compose-base.yaml
service: peer0.org.example.com
extra_hosts:
- "orderer.example.com:10.130.116.8"
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.org.example.com:7051
- CORE_PEER_LOCALMSPID=orgMSP
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/org/peer/crypto/peerOrganizations/org.example.com/peers/peer0.org.example.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/org/peer/crypto/peerOrganizations/org.example.com/peers/peer0.org.example.com/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/org/peer/crypto/peerOrganizations/org.example.com/peers/peer0.org.example.com/tls/ca.crt
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/org/peer/crypto/peerOrganizations/org.example.com/users/Admin@org.example.com/msp
working_dir: /opt/gopath/src/org/peer
volumes:
- /var/run/:/host/var/run/
- ./chaincode/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
- ./bin/crypto-config:/opt/gopath/src/org/peer/crypto/
- ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
- ./bin/channel-artifacts:/opt/gopath/src/org/peer/channel-artifacts
depends_on:
- peer0.org.example.com
extra_hosts:
- "orderer.example.com:10.130.116.8"
- "peer0.org.example.com:10.130.116.9"
這里與orderer不同,有兩個協助啟動文件,分別是位於base目錄下的docker-compose-base.yaml和peer-base.yaml文件,這兩個文件的參數配置分別如下:
docker-compose-base.yaml:
version: '2'
services:
peer0.org.example.com:
container_name: peer0.org.example.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.org.example.com
- CORE_PEER_ADDRESS=peer0.org.example.com:7051
- CORE_PEER_CHAINCODELISTENADDRESS=peer0.org.example.com:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org.example.com:7051
- CORE_PEER_LOCALMSPID=orgMSP
volumes:
- /var/run/:/host/var/run/
- ../bin/crypto-config/peerOrganizations/org.example.com/peers/peer0.org.example.com/msp:/etc/hyperledger/fabric/msp
- ../bin/crypto-config/peerOrganizations/org.example.com/peers/peer0.org.example.com/tls:/etc/hyperledger/fabric/tls
ports:
- 7051:7051
- 7052:7052
- 7053:7053
peer-base.yaml:
version: '2'
services:
peer-base:
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=e2ecli_default
#- CORE_LOGGING_LEVEL=ERROR
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
working_dir: /opt/gopath/src/org/peer
command: peer node start
隨后在org目錄下執行如下命令:
docker-compose -f docker-compose-org.yaml up -d
完成后執行如下命令:
docker ps
即可查看當前啟動鏡像,如下視圖:
繼續執行如下命令來查看我們掛載的peer工作路徑是否正確,是否為/opt/gopath/src/org/peer:
docker exec -it cli bash
該命令執行后會有如下結果視圖:
出現上述目錄,則表示我們的掛載沒有問題。
因為后續的工作都需要在cli容器中執行,而peer節點的操作就掛載在cli容器中的指定目錄。
7.4、創建並加入channel
操作org所在組織的所在節點,即org組織所在節點服務器org0,即10.130.116.9服務器。
如7.3中最后所述,當我們進入peer掛載目錄后,即可對當前peer進行相關操作。
在6.5、生成channel源文件的過程中,我們定義了當前channel的名稱為examplechannel。參照/scripts/ script.sh文件,我們可以看到創建channel的相關命令,也可以參照官網給定的方案。
具體需要執行命令的樣本如下:
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA
上述命令中需要根據生產中實際的需求進行替換,將orderer.example.com組織替換成我們之前定義的orderer.example.com;將$CHANNEL_NAME替換成examplechannel;$CORE_PEER_TLS_ENABLED這個全局變量我們在docker-compose-org.yaml已經定義過了,可以直接引用;$ORDERER_CA是orderer排序服務器TLS證書所在位置,該位置需要通過peer的掛載路徑來定位,因此將$ORDERER_CA替換成/opt/gopath/src/org/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem。
在前一步$ORDERER_CA定位的過程中,我們通過ftp等在peer下是找不到指定目錄及文件的,主要是通過掛載的方式將./bin/crypto-config和/opt/gopath/src/org/peer/crypto/路徑相關聯,參考7.3、啟動orgMSP peer節點對docker-compose-org.yaml文件的配置詳情。
綜上所述,我們最終在peer掛載路徑下需要執行的命令如下:
peer channel create -o orderer.example.com:7050 -c example -f ./channel-artifacts/example_channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
執行該命令會返回了一個genesis block- <channel-ID.block> -我們可以通過這個id進入到channel。它包含channel.tx中指定的配置信息,創建成功后有如下視圖:
接下來,我們要將本組織的當前節點,即org組織所在節點服務器org0,即10.130.116.9服務器節點,加入到channel中,這其中會用到channel-ID.block,而channel-ID.block是上一步所生成的,即example.block。
參照/scripts/ script.sh文件,我們可以看到加入channel的相關命令,也可以參照官網給定的方案。
具體需要執行命令的樣本如下:
peer channel join -b $CHANNEL_NAME.block
將$CHANNEL_NAME修改成example.block,即執行如下命令:
peer channel join -b example.block
加入成功后,會有如下視圖:
7.4.1、注意事項-必讀
通過peer來創建並加入一個channel的時候,如果之前已經創建過同樣的channel,導致系統已經返回過一個[channel-ID].block,此時再執行創建的時候會提示該channel已經創建過,若執行加入又會提示該channel的[channel-ID].block 文件不存在,無法加入。
解決上述問題的方案目前筆者已知的有且僅有一個,即在創建channel的時候,系統會返回一個[channel-ID].block文件,該文件就存儲在cli容器入口目錄里面,當我們創建一個channel成功之后,再次執行如下命令:
docker exec -it cli bash
ls
該命令執行后會有如下視圖:
可以看到我們這里有一個example.block文件,該文件的命名方式以各自聯盟channel需求為主,這個文件會跟隨cli進程的生命周期,即當cli進程被銷毀的時候,該進程下的所有目錄、文件都會被銷毀,比如執行了如下兩種命令來銷毀進程:
docker-compose -f docker-compose.yaml down -d
或
docker stop [CONTAINER ID]
因此,當我們確認channel創建無誤后,需要對該[channel-ID].block文件進行備份,而docker容器內的文件拷貝到實際掛載硬盤中需要docker cp命令的支持,這里給一個簡單的demo,如下命令格式:
docker cp <containerId>:/file/path/within/container /host/path/target
這里實際操作的命令是:
docker cp 56d4d547f93f:/opt/gopath/src/github.com/hyperledger/fabric/peer/example.block /opt/gopath/src/github.com/hyperledger/fabric/example/org/channelbak/
我們在org目錄下新建一個channelbak目錄,專門用來備份各種[channel-ID].block文件。
有了這個備份之后,我們可以停止或刪除之前創建的cli及peer等進程,隨后當我們創建新的cli及peer容器后,將備份的[channel-ID].block文件拷貝回容器中即可,執行如下命令格式:
docker cp /host/path/target <containerId>:/file/path/within/container
這里實際操作的命令是:
docker cp /opt/gopath/src/github.com/hyperledger/fabric/example/org/channelbak/example.block c912fb6c0a0b:/opt/gopath/src/github.com/hyperledger/fabric/peer/
如果之前沒有備份[channel-ID].block文件,那么該服務器可能已經被污染,執行任何刪除或銷毀容器的方法都無法再次成功創建或加入之前已經創建過的channel中,只能暴力解決該問題,即刪除peer、cli等相關鏡像,並重新load進來,再執行后續初始化yaml等方法來一步一步實現,但該方案筆者並未嘗試過,只是猜測,因為channel的創建依賴cli容器。
補充說明一點,根據peer創建channel的命令來看,創建channel需要的文件都是由configtxgen生成的channel.tx及orderer組織下的pem文件,這兩個文件是固定的,且在各不同組織的節點都是相同的,只要其中一個組織生成了[channel-ID].block文件,並將其拷貝給其它組織,其它組織的peer節點無需再次生成,只需要藉此文件執行加入channel的命令就可以了。
即,如果想加入該channel的組織的peer節點服務器因為一些錯誤操作導致丟失[channel-ID].block文件,可以通過從其它組織的peer節點中拷貝的方式來繼續完成加入channel流程。