Hyperledger Fabric节点的动态添加和删除

前言

Hyperledger Fabric组织的动态添加和删除中,我们已经完成了在运行着的网络中动态添加和删除组织。本文将在其基础上,详细介绍了如何在 soft 组织上添加新的 peer2 节点,并在简要概述了删除节点的方法,本实验必要的准备工作和 DNS 配置请参考 准备工作

背景介绍

本文工作

向 Hyperledger Fabric 网络中的 soft 组织动态添加一个节点 peer2 ,网络结构为(实验代码已上传至:https://github.com/wefantasy/FabricLearn2_FabricNetworkUpdate/7_AddPeer.sh 下)1

运行端口 说明
council.ifantasy.net 7050 council 组织的 CA 服务, 为联盟链网络提供 TLS-CA 服务
orderer.ifantasy.net 7150 orderer 组织的 CA 服务, 为联盟链网络提供排序服务
orderer1.orderer.ifantasy.net 7151 orderer 组织的 orderer1 成员节点
soft.ifantasy.net 7250 soft 组织的 CA 服务, 包含成员: peer1 、 admin1
peer1.soft.ifantasy.net 7251 soft 组织的 peer1 成员节点
peer2.soft.ifantasy.net 7252 soft 组织的 peer2 成员节点
web.ifantasy.net 7350 web 组织的 CA 服务, 包含成员: peer1 、 admin1
peer1.web.ifantasy.net 7351 web 组织的 peer1 成员节点

实验准备

本文网络结构直接使用 Hyperledger Fabric组织的添加和删除 中创建的2_FabricNetworkUpdate (建议直接将本案例仓库 FabricLearn 下的 2_FabricNetworkUpdate 目录拷贝到本地运行),文中大部分命令在 Hyperledger Fabric定制联盟链网络工程实践 中已有介绍因此不会详细说明。默认情况下,所有命令皆在 2_FabricNetworkUpdate 根目录下执行,在开始后面的实验前按照以下命令启动基础实验网络:

  1. 设置DNS(如果未设置): ./setDNS.sh
  2. 设置环境变量: source envpeer1soft
  3. 启动CA网络: ./0_Restart.sh
  4. 注册用户: ./1_RegisterUser.sh
  5. 构造证书: ./2_EnrollUser.sh
  6. 配置通道: ./3_Configtxgen.sh
  7. 安装测试链码: ./4_TestChaincode.sh

本实验初始 docker 网络为:

https://cdn.jsdelivr.net/gh/wefantasy/FileCloud/img/hyperledger_fabric_3_update_peer-2022-04-07-15-59-23.png
初始 docker 网络

本实验初始区块高度为6:

https://cdn.jsdelivr.net/gh/wefantasy/FileCloud/img/hyperledger_fabric_3_update_peer-2022-04-07-16-00-18.png
验初始区块高度

添加新节点

生成peer2的组织证书

由于 peer2 属于 soft 组织,所以其证书直接使用已有的 CA 服务器即可生成。

  1. 生成 TLS-CA 证书:
1
2
3
export FABRIC_CA_CLIENT_TLS_CERTFILES=$LOCAL_CA_PATH/council.ifantasy.net/ca/crypto/ca-cert.pem
export FABRIC_CA_CLIENT_HOME=$LOCAL_CA_PATH/council.ifantasy.net/ca/admin
fabric-ca-client register -d --id.name peer2soft --id.secret peer2soft --id.type peer -u https://council.ifantasy.net:7050
  1. 生成 CA 证书:
1
2
3
export FABRIC_CA_CLIENT_TLS_CERTFILES=$LOCAL_CA_PATH/soft.ifantasy.net/ca/crypto/ca-cert.pem
export FABRIC_CA_CLIENT_HOME=$LOCAL_CA_PATH/soft.ifantasy.net/ca/admin
fabric-ca-client register -d --id.name peer2 --id.secret peer2 --id.type peer -u https://soft.ifantasy.net:7250
  1. 构造证书目录:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
echo "Enroll Peer2"
export FABRIC_CA_CLIENT_HOME=$LOCAL_CA_PATH/soft.ifantasy.net/registers/peer2
export FABRIC_CA_CLIENT_TLS_CERTFILES=$LOCAL_CA_PATH/soft.ifantasy.net/assets/ca-cert.pem
export FABRIC_CA_CLIENT_MSPDIR=msp
fabric-ca-client enroll -d -u https://peer2:[email protected]:7250
# for TLS
export FABRIC_CA_CLIENT_MSPDIR=tls-msp
export FABRIC_CA_CLIENT_TLS_CERTFILES=$LOCAL_CA_PATH/soft.ifantasy.net/assets/tls-ca-cert.pem
fabric-ca-client enroll -d -u https://peer2soft:[email protected]:7050 --enrollment.profile tls --csr.hosts peer2.soft.ifantasy.net
cp $LOCAL_CA_PATH/soft.ifantasy.net/registers/peer2/tls-msp/keystore/*_sk $LOCAL_CA_PATH/soft.ifantasy.net/registers/peer2/tls-msp/keystore/key.pem
mkdir -p $LOCAL_CA_PATH/soft.ifantasy.net/registers/peer2/msp/admincerts
cp $LOCAL_CA_PATH/soft.ifantasy.net/registers/admin1/msp/signcerts/cert.pem $LOCAL_CA_PATH/soft.ifantasy.net/registers/peer2/msp/admincerts/cert.pem

配置peer2的容器及环境变量

  1. 在 compose 目录下新建 update-peer.yaml 文件,内容如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
version: '2'

networks:
  network:

services:
  peer2.soft.ifantasy.net:
    container_name: peer2.soft.ifantasy.net
    extends:
      file: docker-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer2.soft.ifantasy.net
      - CORE_PEER_ADDRESS=peer2.soft.ifantasy.net:7051
      - CORE_PEER_LOCALMSPID=softMSP
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer2.soft.ifantasy.net:7051
    volumes:
      - ${LOCAL_CA_PATH}/soft.ifantasy.net/registers/peer2:${DOCKER_CA_PATH}/peer
    ports:
      - 7252:7051
  1. 启动 peer2 容器:
1
docker-compose -f $LOCAL_ROOT_PATH/compose/update-peer.yaml up -d peer2.soft.ifantasy.net

此时可以使用 docker ps 命令看到 peer2 容器成功运行:

https://cdn.jsdelivr.net/gh/wefantasy/FileCloud/img/hyperledger_fabric_3_update_peer-2022-04-07-16-01-33.png
启动 peer2 容器
3. 添加 peer2 的 DNS 解析记录到本机:

1
echo "127.0.0.1       peer2.soft.ifantasy.net" >> /etc/hosts
  1. 将 peer1 的环境变量文件 envpeer1soft 复制一份到 envpeer2soft ,其内容为:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
export LOCAL_ROOT_PATH=$PWD
export LOCAL_CA_PATH=$LOCAL_ROOT_PATH/orgs
export DOCKER_CA_PATH=/tmp
export COMPOSE_PROJECT_NAME=fabriclearn
export DOCKER_NETWORKS=network
export FABRIC_BASE_VERSION=2.4
export FABRIC_CA_VERSION=1.5
echo "init terminal soft"
export FABRIC_CFG_PATH=$LOCAL_ROOT_PATH/config
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="softMSP"
export CORE_PEER_ADDRESS=peer2.soft.ifantasy.net:7252
export CORE_PEER_TLS_ROOTCERT_FILE=$LOCAL_CA_PATH/soft.ifantasy.net/assets/tls-ca-cert.pem
export CORE_PEER_MSPCONFIGPATH=$LOCAL_CA_PATH/soft.ifantasy.net/registers/admin1/msp
export ORDERER_CA=$LOCAL_CA_PATH/orderer.ifantasy.net/registers/orderer1/tls-msp/tlscacerts/tls-council-ifantasy-net-7050.pem

peer2加入通道

  1. 拉取通道创世区块:
1
peer channel fetch 0 mychannel.block -o orderer1.orderer.ifantasy.net:7151 -c mychannel --tls --cafile $ORDERER_CA

由于 peer2 还没有 mychannel 通道的访问权限,所以目前为止我们都是使用 peer1 的环境变量进行操作,后面加入通道后可以使用 peer2 的环境变量。

  1. peer2 加入通道:
1
2
source envpeer2soft
peer channel fetch 0 mychannel.block -o orderer1.orderer.ifantasy.net:7151 -c mychannel --tls --cafile $ORDERER_CA

此时 peer2 已经加入通道,但是其区块高度仍为0:

https://cdn.jsdelivr.net/gh/wefantasy/FileCloud/img/hyperledger_fabric_3_update_peer-2022-04-07-16-02-27.png
peer2 区块高度

  1. peer2 安装链码:
1
peer lifecycle chaincode install basic.tar.gz

现在 peer2 的区块高度已更新到最新的6:

https://cdn.jsdelivr.net/gh/wefantasy/FileCloud/img/hyperledger_fabric_3_update_peer-2022-04-07-16-04-03.png
peer2 区块高度2

删除旧节点

或许是删除旧节点不符合区块链的设计思想,因此官方并没有提供方法来移除已经加入通道的 peer 节点,但是在实际使用中,我们可以直接通过停用 peer 容器来移除 peer 节点2

参考


  1. zcc0721. Fabric向现有组织中添加新节点. 2021-01-14. [CSDN] ↩︎

  2. Alessandro Sorniotti. How to remove peer from a channel. 2018-11-14. [CSDN] ↩︎