自动搭建多机器多节点hyperledger fabric (使用ansilbe工具,搭建环境超级简单!)

本文主要参考了网易云课堂“IT技术快速入门学院”的第二门课《HyperLedger Fabric进阶实战课》第三章节使用的素材。
但是在实践过程中还是遇到不少问题,所以相应做了一些改动,并且把测试部分改为更加自动化一点。
如果搭建在搭建过程中遇到什么问题,可以在下面评论,我会及时回复。希望我们都能跳出搭建hyperledger fabric环境的坑,尽快进入到这个框架更深层次的学习中。

配置情况如下:
2个组织,3个节点
10.61.0.91 peer0.org1.example.com
10.61.0.92 peer1.org1.example.com
10.61.0.93 peer0.org2.example.com
10.61.0.92 orderer.exmaple.com

1. 安装ansilbe工具

yum install ansible -y

2. 下载搭建的素材

git clone https://github.com/YuaJuan/hyperledger-fabric-ansible.git

3. 配置hosts

vim /etc/hosts
vim inventories/example.com/hosts
vim inventories/example.com/etc_hosts

根据自己的Ip地址对应修改

4.如果域名没有绑定IP,修改每台机器的/etc/hosts,(会替换整个文件):

ansible -i inventories/example.com/hosts -u root  all  -m copy -a "src=./inventories/example.com/etc_hosts dest=/etc/hosts"

5.执行自动化安装

./run.sh

执行完没有任何错误,那么我们的环境就安装好了。在每个机器的opt/app/fabric/cli/user/orgX.example.com/Admin-peerX.orgX.example.com/下都有对应的创建channel,加入channel,安装链码等相关的脚本,可以自行在每个机器下执行来测试。

说明

  1. 配置文件在inventories/example.com下,需要根据自己的需要做相应修改
  2. 如果修改了crypto-config.yaml文件,则需要修改对应的/etc/hosts文件

使用进程管理工具supervisor

在这个自动化安装中,使用了supervisor来管理进程,所以简单介绍下相关配置

在Linux服务器中,有时候我们需要一个进程需要可靠的在后台运行,并且能够监控进程状态,在意外结束时能够自动重启等。此时就可以使用supervisor。

supervisor 是使用Python开发的一套通用的进程管理程序,能够将一个普通的命令行进程变成后台的守护进程,并且监控进程的状态,异常退出时能够自动重启。

其他工具

1 启动链:

ansible-playbook -i inventories/example.com/hosts -u root playbooks/manage_start.yml

2 停止链:

ansible-playbook -i inventories/example.com/hosts -u root playbooks/manage_stop.yml

3 清空链上所有数据:

ansible-playbook -i inventories/example.com/hosts -u root playbooks/manage_rebuild.yml

4 销毁链:

ansible-playbook -i inventories/example.com/hosts -u root playbooks/manage_destroy.yml
  1. 安装supervisor
  2. 为每个进程配置文件
cd /etc/supervisord.d/
[program: orderer]
command=/opt/app/fabric/orderer/bin/orderer

environment=FABRIC_CFG_PATH=/opt/app/fabric/orderer

numprocs=1
autostart=true
startretries=3
autorestart=unexpected
exitcodes=0,2

stdout_logfile=/opt/app/fabric/orderer/log/orderer0.member1.example.com.stdout
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10

stderr_logfile=/opt/app/fabric/orderer/log/orderer0.member1.example.com.stderr
stderr_logfile_maxbytes=50MB
stderr_logfile_backups=10
[program: peer]
command=/opt/app/fabric/peer/bin/peer node start
environment=FABRIC_CFG_PATH=/opt/app/fabric/peer

numprocs=1
autostart=true
startretries=3
autorestart=unexpected
exitcodes=0,2

stdout_logfile=/opt/app/fabric/peer/log/peer1.member1.example.com.stdout
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10

stderr_logfile=/opt/app/fabric/peer/log/peer1.member1.example.com.stderr
stderr_logfile_maxbytes=50MB
stderr_logfile_backups=10

可能遇到的错误情况

failed: [10.61.0.93] (item=ntpd) => {“ansible_loop_var”: “item”, “changed”: false, “item”: “ntpd”, “msg”: “Could not find the requested service ntpd: host”}

安装ntp即可解决

yum install ntp
ntp service start

Got status: &{FORBIDDEN}
Error: can’t read the block: &{FORBIDDEN}

你可能感兴趣的:(区块链)