(五)、Fabric多机多节点部署

Fabric1.0多机部署

前面我们使用单机部署的方式将fabric1.0的基本流程全部调通了。这次我们玩一次分布式多机部署的游戏。将Fabric-E2E=1orderer+4peer部署到五台不同的主机上。并实现智能合约的操作。


  • 环境准备
  • 单机运行4+1 Fabric实例,确保脚本和镜像正常
  • 生成公私钥、证书、创世区块
  • 设置docker-compose文件
  • 分发配置文件
  • 启动Fabric
  • 总结

1. 环境准备

主机名 ip 角色
orderer.example.com 39.108.219.98 orderer排序服务器
peer0.org1.example.com 120.78.167.221 peer1节点
peer1.org1.example.com 120.79.169.213 peer2节点
peer0.org2.example.com 47.106.127.24 peer3节点
peer1.org2.example.com 47.98.143.199 peer4节点

2. 单机运行4+1 Fabric实例,确保脚本和镜像正常

cd $GOPATH/src/github.com/hyperledger/fabric/examples/e2e_cli/
./network_setup.sh down(已经启动的话、或者不知道有没有启动)
./network_setup.sh up

- 生成公私钥、证书、创世区块

公私钥和证书是用于Server和Server之间的安全通信,另外要创建Channel并让其他节点加入Channel就需要创世区块,这些必备文件都可以一个命令生成,官方已经给出了脚本

./generateArtifacts.sh mychannel

运行这个命令后,系统会创建channel-artifacts文件夹,里面包含了mychannel这个通道相关的文件,另外还有一个crypto-config文件夹,里面包含了各个节点的公私钥和证书的信息。

- 设置docker-compose文件

  • 设置peer节点的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配置如下:

version: '2'

services:

  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:39.108.219.98"

同样,cli也需要能够和各个节点通讯,所以cli下面也需要添加extra_hosts设置,去掉无效的依赖,并且去掉command这一行,因为我们是每个peer都会有个对应的客户端,也就是cli,所以我只需要去手动执行一次命令,而不是自动运行。修改后的cli配置如下:

  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=false
      - 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
      - GODEBUG=netdns=go
    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:39.108.219.98"
     - "peer0.org1.example.com:120.78.167.221"
     - "peer1.org1.example.com:120.79.169.213"
     - "peer0.org2.example.com:47.106.127.24"
     - "peer1.org2.example.com:47.98.143.199"

在单击模式下,4个peer会映射主机不同的端口,但是我们在多机部署的时候是不需要映射不同端口的,所以需要修改base/docker-compose-base.yaml文件,将所有peer的端口映射都改为相同的:

ports: 
  - 7051:7051 
  - 7052:7052 
  - 7053:7053
  • 设置orderer排序服务器的的docker-compose
    与创建peer的配置文件类似,我们也复制一个yaml文件出来进行修改:
cp docker-compose-cli.yaml 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

- 分发配置文件

一次性都丢到orderer的E2E目录下去

scp /Users/lantian/Downloads/fabric4+1/docker-compose-peer1.yaml root@39.108.219.98:/root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

scp /Users/lantian/Downloads/fabric4+1/docker-compose-peer2.yaml  root@39.108.219.98:/root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

scp /Users/lantian/Downloads/fabric4+1/docker-compose-peer3.yaml  root@39.108.219.98:/root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

scp /Users/lantian/Downloads/fabric4+1/docker-compose-peer4.yaml  root@39.108.219.98:/root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

base下的yaml
scp /Users/lantian/Downloads/fabric4+1/base/docker-compose-base.yaml root@39.108.219.98:/root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/base

scp /Users/lantian/Downloads/fabric4+1/base/peer-base.yaml root@39.108.219.98:/root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/base

先删掉peer每台机子原本的E2E

rm -r /root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

然后从orderer上下载E2E

scp -r root@39.108.219.98:/root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/ /root/go/src/github.com/hyperledger/fabric/examples

- 启动Fabric

开启orderer

docker-compose -f docker-compose-orderer.yaml up -d

依次开启其余四台peer

docker-compose -f docker-compose-peer1.yaml up -d

docker-compose -f docker-compose-peer2.yaml up -d

docker-compose -f docker-compose-peer3.yaml up -d

docker-compose -f docker-compose-peer4.yaml up -d

- 总结

新手第一次部署的难度会比较大,务必耐心。一定可以部署的成功。

猜你喜欢

转载自blog.csdn.net/qq_37989365/article/details/81410162