fabric多機(jī)部署——4+1模式5臺(tái)虛擬機(jī)部署
一、首先準(zhǔn)備5臺(tái)虛擬機(jī):
下面的前幾步都是在單機(jī)(peer0.org1.com節(jié)點(diǎn)的服務(wù)器上)上配置文件经宏,配置好后會(huì)將文件依次復(fù)制到其他各自的節(jié)點(diǎn)上耐亏。
在進(jìn)行下面第二步之前,先要執(zhí)行./network_setup.sh? down關(guān)掉之前的單機(jī)部署沪斟。
二广辰、生成公私鑰、證書(shū)、創(chuàng)世區(qū)塊等
公私鑰和證書(shū)是用于Server和Server之間的安全通信择吊,另外要?jiǎng)?chuàng)建Channel并讓其他節(jié)點(diǎn)加入Channel就需要?jiǎng)?chuàng)世區(qū)塊李根,這些必備文件都可以一個(gè)命令生成,官方已經(jīng)給出了腳本:
./generateArtifacts.sh mychannel
運(yùn)行結(jié)果如下:
運(yùn)行這個(gè)命令后干发,系統(tǒng)會(huì)創(chuàng)建channel-artifacts文件夾朱巨,里面包含了mychannel這個(gè)通道相關(guān)的文件,另外還有一個(gè)crypto-config文件夾枉长,里面包含了各個(gè)節(jié)點(diǎn)的公私鑰和證書(shū)的信息冀续。
三、設(shè)置peer節(jié)點(diǎn)的docker-compose文件
e2e_cli中提供了多個(gè)yaml文件必峰,我們可以基于docker-compose-cli.yaml文件創(chuàng)建(注意洪唐,這里的peer節(jié)點(diǎn)文件有4個(gè),分別是peer0.org1, peer1.org1, peer0.org2, peer1.org2吼蚁。后面會(huì)依次表明所有這4個(gè)節(jié)點(diǎn)的peer文件配置內(nèi)容 ):
cp docker-compose-cli.yaml docker-compose-peer.yaml
然后修改docker-compose-peer.yaml凭需,去掉orderer的配置,只保留peer和cli肝匆,因?yàn)槲覀円鄼C(jī)部署粒蜈,節(jié)點(diǎn)與節(jié)點(diǎn)之前又是通過(guò)主機(jī)名通訊,所以需要修改容器中的host文件旗国,也就是extra_hosts設(shè)置枯怖,修改后的peer(準(zhǔn)確的來(lái)說(shuō),這里的peer是peer0.org1節(jié)點(diǎn)能曾,剩余的其他3個(gè)peer節(jié)點(diǎn)會(huì)在這個(gè)peer0.org1節(jié)點(diǎn)的配置文件上再修改)配置如下:
peer0.org1.example.com:
??container_name: peer0.org1.example.com
??extends:
????file:? base/docker-compose-base.yaml
????service: peer0.org1.example.com
??extra_hosts:
???-?"orderer.example.com:192.168.121.141"
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.org1.example.com:7051
??? - CORE_PEER_LOCALMSPID=Org1MSP
??? - CORE_PEER_TLS_ENABLED=true
??? - 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
???? - 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
??? - 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
??? - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
? working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
? volumes:
????? - /var/run/:/host/var/run/
????? - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
????? - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
????? - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
????? - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
? depends_on:
??? - peer0.org1.example.com
? extra_hosts:
?? - "orderer.example.com:192.168.121.141"
?? - "peer0.org1.example.com:192.168.121.142"
?? - "peer1.org1.example.com:192.168.121.143"
?? - "peer0.org2.example.com:192.168.121.144"
?? - "peer1.org2.example.com:192.168.121.145"
在單擊模式下度硝,4個(gè)peer會(huì)映射主機(jī)不同的端口,但是我們?cè)诙鄼C(jī)部署的時(shí)候是不需要映射不同端口的寿冕,所以需要修改base/docker-compose-base.yaml文件蕊程,將所有peer的端口映射都改為相同的:
ports:
??- 7051:7051
??- 7052:7052
??- 7053:7053
四、設(shè)置orderer節(jié)點(diǎn)的docker-compose文件
與創(chuàng)建peer的配置文件類(lèi)似驼唱,我們也復(fù)制一個(gè)yaml文件出來(lái)進(jìn)行修改:
cp docker-compose-cli.yaml docker-compose-orderer.yaml
orderer服務(wù)器上我們只需要保留order設(shè)置藻茂,其他peer和cli設(shè)置都可以刪除。orderer可以不設(shè)置extra_hosts曙蒸。配置文件結(jié)果如下:
五捌治、分發(fā)配置文件
前面4步的操作,我們都是在peer0.org1.com上完成的纽窟,接下來(lái)我們需要將這些文件分發(fā)到另外4臺(tái)服務(wù)器上肖油。Linux之間的文件傳輸,我們可以使用scp命令臂港。
先登錄orderer.example.com森枪,將本地的e2e_cli文件夾刪除:
rm e2e_cli –R
然后再登錄到peer0.org1.com服務(wù)器上视搏,退回到examples文件夾,因?yàn)檫@樣可以方便的把其下的e2e_cli文件夾整個(gè)傳到orderer服務(wù)器上县袱。
scp -r e2e_cli? fabric@10.51.120.220:/home/fabric/go/src/github.com/hyperledger/fabric/examples/
以上對(duì)于orderer.examples.com配置文件復(fù)制完畢浑娜。下面依次再次運(yùn)行scp命令,復(fù)制到peer1.org1.example.com上式散,然后我們需要對(duì)docker-compose-peer.yaml做一個(gè)小小的修改筋遭,將啟動(dòng)的容器改為peer1.org1.example.com,并且添加peer0.org1.example.com的IP映射暴拄,對(duì)應(yīng)的cli中也改成對(duì)peer1.org1.example.com的依賴(lài)漓滔。這是修改后的peer1.org1.example.com上的配置文件:
version: '2'
services:
? peer1.org1.example.com:
??? container_name: peer1.org1.example.com
??? extends:
????? file:? base/docker-compose-base.yaml
????? service: peer1.org1.example.com
??? extra_hosts:
???? - "orderer.example.com:192.168.121.141"
???? - "peer0.org1.example.com:192.168.121.142"
? 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=peer1.org1.example.com:7051
????? - CORE_PEER_LOCALMSPID=Org1MSP
????? - CORE_PEER_TLS_ENABLED=true
????? - 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
????? - 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
????? - 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
????? - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
??? working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
??? volumes:
??????? - /var/run/:/host/var/run/
??????? - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
???????? - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
??????? - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
??????? - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
??? depends_on:
????? - peer1.org1.example.com
??? extra_hosts:
?? - "orderer.example.com:192.168.121.141"
?? - "peer0.org1.example.com:192.168.121.142"
?? - "peer1.org1.example.com:192.168.121.143"
?? - "peer0.org2.example.com:192.168.121.144"
?? - "peer1.org2.example.com:192.168.121.145"
接下來(lái)繼續(xù)使用scp命令將peer0.org1.com上的文件夾傳送給peer0.org2.example.com和peer1.org2.example.com,然后也是修改一下docker-compose-peer.yaml文件乖篷,使得其啟動(dòng)對(duì)應(yīng)的peer節(jié)點(diǎn)响驴。
peer0.org2.example.com的docker-compose-peer.yaml配置文件:
version: '2'
services:
? peer0.org2.example.com:
??? container_name: peer0.org2.example.com
??? extends:
????? file:? base/docker-compose-base.yaml
????? service: peer0.org2.example.com
??? extra_hosts:
???? - "orderer.example.com:192.168.121.141"
???? - "peer0.org1.example.com:192.168.121.142"
? 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.org2.example.com:7051
????? - CORE_PEER_LOCALMSPID=Org2MSP
????? - CORE_PEER_TLS_ENABLED=true
????? - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt
????? - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key
????? - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
????? - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
??? working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
??? volumes:
??????? - /var/run/:/host/var/run/
??????? - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
???????? - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
??????? - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
??????? - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
??? depends_on:
????? - peer0.org2.example.com
??? extra_hosts:
?? - "orderer.example.com:192.168.121.141"
?? - "peer0.org1.example.com:192.168.121.142"
?? - "peer1.org1.example.com:192.168.121.143"
?? - "peer0.org2.example.com:192.168.121.144"
?? - "peer1.org2.example.com:192.168.121.145"
peer0.org2.example.com的docker-compose-peer.yaml配置文件:
version: '2'
services:
? peer1.org2.example.com:
??? container_name: peer1.org2.example.com
??? extends:
????? file:? base/docker-compose-base.yaml
????? service: peer1.org2.example.com
??? extra_hosts:
???? - "orderer.example.com:192.168.121.141"
???? - "peer0.org1.example.com:192.168.121.142"
? 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=peer1.org2.example.com:7051
????? - CORE_PEER_LOCALMSPID=Org2MSP
????? - CORE_PEER_TLS_ENABLED=true
????? - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls/server.crt
????? - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls/server.key
????? - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls/ca.crt
?? ???- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
??? working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
??? volumes:
??????? - /var/run/:/host/var/run/
??????? - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
???????? - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
??????? - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
??????? - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
??? depends_on:
????? - peer1.org2.example.com
??? extra_hosts:
?? - "orderer.example.com:192.168.121.141"
?? - "peer0.org1.example.com:192.168.121.142"
?? - "peer1.org1.example.com:192.168.121.143"
?? - "peer0.org2.example.com:192.168.121.144"
?? - "peer1.org2.example.com:192.168.121.145"
六、啟動(dòng)fabric
現(xiàn)在所有文件都已經(jīng)準(zhǔn)備完畢撕蔼,可以啟動(dòng)Fabric網(wǎng)絡(luò)了豁鲤。
6.1啟動(dòng)orderer
讓我們首先來(lái)啟動(dòng)orderer節(jié)點(diǎn),在orderer服務(wù)器上運(yùn)行:docker-compose -f docker-compose-orderer.yaml up -d
運(yùn)行完畢后我們可以使用docker ps看到運(yùn)行了一個(gè)名字為orderer.example.com的節(jié)點(diǎn)鲸沮。類(lèi)似以下:
6.2啟動(dòng)peer
然后我們切換到peer0.org1.example.com服務(wù)器琳骡,啟動(dòng)本服務(wù)器的peer節(jié)點(diǎn)和cli,命令為:
docker-compose -f docker-compose-peer.yaml up -d
運(yùn)行完畢后我們使用docker ps應(yīng)該可以看到4個(gè)正在運(yùn)行的容器讼溺。
現(xiàn)在我們整個(gè)Fabric4+1服務(wù)器網(wǎng)絡(luò)已經(jīng)成型日熬,接下來(lái)是創(chuàng)建channel和運(yùn)行ChainCode。
6.3創(chuàng)建Channel測(cè)試ChainCode
我們切換到peer0.org1.example.com(錨節(jié)點(diǎn))服務(wù)器上肾胯,使用該服務(wù)器上的cli來(lái)運(yùn)行創(chuàng)建Channel和運(yùn)行ChainCode的操作。首先進(jìn)入cli容器:
docker exec -it cli bash
進(jìn)入容器后我們可以看到命令提示變?yōu)椋?/p>
root@b41e67d40583:/opt/gopath/src/github.com/hyperledger/fabric/peer#
說(shuō)明我們已經(jīng)以root的身份進(jìn)入到cli容器內(nèi)部耘纱。官方已經(jīng)提供了完整的創(chuàng)建Channel和測(cè)試ChainCode的腳本敬肚,并且已經(jīng)映射到cli容器內(nèi)部,所以我們只需要在cli內(nèi)運(yùn)行如下命令:
./scripts/script.sh mychannel
那么該腳本就可以一步一步的完成創(chuàng)建通道束析,將其他節(jié)點(diǎn)加入通道艳馒,更新錨節(jié)點(diǎn)员寇,創(chuàng)建ChainCode弄慰,初始化賬戶,查詢蝶锋,轉(zhuǎn)賬陆爽,再次查詢等鏈上代碼的各個(gè)操作都可以自動(dòng)化實(shí)現(xiàn)。直到最
后,系統(tǒng)提示:
=============== All GOOD, End-2-End execution completed ================
說(shuō)明我們的4+1的Fabric多級(jí)部署成功了。我們現(xiàn)在是? peer0.org1.example.com的cli容器內(nèi)丧失,我們也可以切換到peer0.org2.example.com服務(wù)器豺妓,運(yùn)行docker ps命令,可以看到本來(lái)是2個(gè)容器的布讹,現(xiàn)在已經(jīng)變成了3個(gè)容器琳拭,因?yàn)镃hainCode會(huì)創(chuàng)建一個(gè)容器。
以上為fabric4+1多機(jī)部署所有步驟炒事。
完臀栈。