iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 16
0

環境

環境採用三臺實體機器,也可以嘗試使用OpenStack 三個 Instance進行建置:

主機名稱 IP 角色
orderer.example.com 10.0.0.78 orderer
peer0.org1.example.com 10.0.0.79 peer
peer0.org2.example.com 10.0.0.201 peer

peer 表示一個區塊鏈網路中的節點,負責儲存交易帳本以及背書其他節點的交易,orderer 表示排序者,負責處理各個節點傳送過來的交易,依據傳送時間點進行排序,將其結果打包並生成一個Block。

建置 Hyperledger Fabric 步驟

  • 安裝Go
wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.8.3.linux-amd64.tar.gz

編輯環境變量

vim ~/.profile

添加以下內容

export PATH=$PATH:/usr/local/go/bin 
export GOROOT=/usr/local/go 
export GOPATH=$HOME/go 
export PATH=$PATH:$HOME/go/bin

載入修改的環境變量

source ~/.profile

因為將go的目錄GOPATH設置在$HOME/go,所以需要在$HOME目錄底下建置go目錄

mkdir ~/go
  • 安裝Docker
sudo apt-get install docker.io
  • 安裝Docker-Compose
    需要先安裝python-pip
sudo apt-get install python-pip

curl -L https://get.daocloud.io/docker/compose/releases/download/1.10.1/docker-compose-`uname -s`-`uname -m` > ~/docker-compose 

sudo mv ~/docker-compose /usr/local/bin/docker-compose 

chmod +x /usr/local/bin/docker-compose
  • 下載Fabric
go get github.com/hyperledger/fabric

下載成功後會在~/go/src/目錄下看到已經下載好的Fabric

  • Fabric Docker images下載
    官方提供了完整著images下載腳本,直接執行該腳本即可下載所有Fabric環境所需的images
cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/
source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0

以上即完成整個 Hyperledger Fabric 所有環境的建置,接下來只需要透過修改配置文件來自訂所需的環境

  • 手動生成公私鑰、證書、創世區塊
    本文環境中尚未加入CA因此需要透過cryptogen來生成公私鑰、證書、創世區塊,以下會透過官方提供的腳本生成
./generateArtifacts.sh mychannel

系统會創建 channel-artifacts 以及 crypto-config 兩個資料夾,分別存放通道所需的相關文件以及各個節點的公私鑰與證書

  • 修改docker-compose-base.yaml
ports: 
  - 7051:7051 
  - 7052:7052 
  - 7053:7053

該配置文件原先為提供同一台主機中設置多節點網路,因此會透過修改 port 號來區別這個節點,不過我們這邊為多機器部署,因此必須將port皆設置為原先模樣

  • 設置orderer節點
    我們基於e2e_cli目錄底下提供的docker-compose-cli.yaml進行修改
cp docker-compose-cli.yaml docker-compose-orderer.yaml
vim 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
  • 設置peer0.org1節點
    同樣使用e2e_cli目錄底下提供的docker-compose-cli.yaml進行修改
cp docker-compose-cli.yaml docker-compose-peer.yaml
vim docker-compose-peer.yaml

接著將其修改為以下樣子

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

  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:10.0.0.78"
      - "peer0.org1.example.com:10.0.0.79"
      - "peer0.org2.example.com:10.0.0.201"

與 orderer 不同 peer 需要與 orderer 溝通所以需要配置 extra_hosts ,而 cli 需要與所有節點溝通,因此需要配置與所有節點溝通的 extra_hosts 配置

  • 設置peer0.org2節點
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:10.0.0.78"
     - "peer0.org1.example.com:10.0.0.79"
  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=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.org2.example.com
    extra_hosts:
      - "orderer.example.com:10.0.0.78"
      - "peer0.org1.example.com:10.0.0.79"
      - "peer0.org2.example.com:10.0.0.201"

與 peer0.org1 不同在於 extra_hosts 部分需要加入 "peer0.org1.example.com:10.0.0.79" 配置
那麼以上完成配置文件後,即可在各自的主機運行剛剛修改的配置文件

  • 啟動orderer
    在 orderer 主機運行以下指令
docker-compose -f docker-compose-orderer.yaml up –d

可以透過 docker ps 看到已有一個名為 orderer.example.com 的節點運行中

CONTAINER ID        IMAGE                        COMMAND             CREATED             STATUS              PORTS                    NAMES
9e6d7c58ab1f        hyperledger/fabric-orderer   "orderer"           11 days ago         Up 11 days          0.0.0.0:7050->7050/tcp   orderer.example.com
  • 啟動peer
    在 peer 主機運行以下指令
docker-compose -f docker-compose-orderer.yaml up –d

可以透過 docker ps 看到 peer 與 cli 已啟動

CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                              NAMES
21d1df7f3a91        hyperledger/fabric-tools              "/bin/bash"              11 days ago         Up 11 days                                             cli
4ac3288023f8        hyperledger/fabric-peer               "peer node start"        11 days ago         Up 11 days          0.0.0.0:7051-7053->7051-7053/tcp   peer0.org1.example.com

peer0.org2也是使用相同啟動步驟
那個以上環境皆啟動完畢,接下來需要透過創建Channel與測試chaincode來確定環境設定為正確的

  • 修改 e2e 腳本
    官方原先提供的腳本為 4peer + 1orderer 的架構,因此我們這邊需要先先改為 2peer + 1orderer 的測試腳本

首先於 peer0.org1 的主機上進入 cli container

docker exec -ti cli bash

接著會在 /opt/gopath/src/github.com/hyperledger/fabric/peer/scripts 底下看到 script.sh 腳本,將其複製來進行修改

cp ./script/script.sh ./script/test.sh

於腳本 function 運行區塊部分,修改為以下樣子

## Create channel
echo "Creating channel..."
createChannel

## Join all the peers to the channel
echo "Having all peers join the channel..."
joinChannel

## Set the anchor peers for each org in the channel
echo "Updating anchor peers for org1..."
updateAnchorPeers 0
echo "Updating anchor peers for org2..."
updateAnchorPeers 2

## Install chaincode on Peer0/Org1 and Peer2/Org2
echo "Installing chaincode on org1/peer0..."
installChaincode 0
echo "Install chaincode on org2/peer2..."
installChaincode 2

#Instantiate chaincode on Peer2/Org2
echo "Instantiating chaincode on org2/peer2..."
instantiateChaincode 2

#Query on chaincode on Peer0/Org1
echo "Querying chaincode on org1/peer0..."
chaincodeQuery 0 100

#Invoke on chaincode on Peer0/Org1
echo "Sending invoke transaction on org1/peer0..."
chaincodeInvoke 0

以上修改方式為僅保留 Peer0/Org1 與 Peer2/Org2

  • 接著在 cli 容器下運行腳本
./scripts/script.sh mychannel

運行完成後看到以下結果,表示整個 Hyperledger Fabric 多節點的部署已經成功建置

===================== All GOOD, End-2-End execution completed =====================

上一篇
Hyperledger Fabric 基礎介紹
下一篇
軟體定義網路介紹(SDN)
系列文
基於雲端Iaas基礎平台OpenStack結合Kubernetes,BlockChain,Spark,SDN24
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言