Hyperledger Fabric 实战(5)—— 基于Virtualbox+Vagrant搭建示例环境


本文转载自微信公众号:    超级账本开发    , 并已经取得转载授权.

Virtualbox是Oracle公司出品的开源\免费的虚拟机管理工具,类似于Windows下面的VMWare Workstation, 但更节约系统资源.  Vagrant是一个虚拟机管理工具,可以使用一个配置文件轻松地创建一个虚拟机集群. 使用Virtual + Vagrant可以非常方便地搭建基于VM的开发测试环境集群,在使用完毕后可以一键清理,不会对主机造成任何影响.


Vagrant常用的命令:

vagrant  init ubuntu/trusty64  ---- 初始化一个Ubuntu14.04的64位虚拟机配置(生成Vagrantfile文件)

vagrant up  ---- 启动虚拟机

vagrant destroy ----销毁虚拟机


本文介绍基于Virtualbox + Vagrant搭建Hyperledger Fabric的示例环境,并执行测试程序. 使用的主机操作系统是Ubuntu16.04 x64 桌面版.  注意,为保证Virtualbox正常运行, 建议使用桌面版的Ubuntu 系统.


1. 安装Virtualbox

可以通过apt-get  直接安装.

sudo apt-get install -y virtualbox


2. 安装Vagrant

使用apt-get  安装的可能是老的版本, 本次环境需要2.0以上版本,所有直接从官网下载最新安装包安装:

wget https://releases.hashicorp.com/vagrant/2.1.1/vagrant_2.1.1_x86_64.deb

  sudo dpkg -i vagrant_2.1.1_x86_64.deb

如果安装过程中报错提示依赖不存在,则再运行  sudo apt-get install -f 修复即可.


3. 新建一个目录 fabric-net,  在该目录下创建Vagrantfile文件,  内容如下:

Vagrant.configure("2") do |config|

 # The most common configuration options are documented and commented below.

 # For a complete reference, please see the online documentation at

 # https://docs.vagrantup.com.

 # Every Vagrant development environment requires a box. You can search for

 # boxes at https://atlas.hashicorp.com/search.

 config.vm.box = "ubuntu/trusty64"

 # Disable automatic box update checking. If you disable this, then

 # boxes will only be checked for updates when the user runs

 # `vagrant box outdated`. This is not recommended.

 # config.vm.box_check_update = false

 # Create a forwarded port mapping which allows access to a specific port

 # within the machine from a port on the host machine. In the example below,

 # accessing "localhost:8080" will access port 80 on the guest machine.

 # config.vm.network "forwarded_port", guest: 80, host: 8080

 # Create a private network, which allows host-only access to the machine

 # using a specific IP.

 # config.vm.network "private_network", ip: "192.168.33.10"

 # Create a public network, which generally matched to bridged network.

 # Bridged networks make the machine appear as another physical device on

 # your network.

 # config.vm.network "public_network"

 # Share an additional folder to the guest VM. The first argument is

 # the path on the host to the actual folder. The second argument is

 # the path on the guest to mount the folder. And the optional third

 # argument is a set of non-required options.

 # config.vm.synced_folder "./data", "/vagrant_data"

 # Provider-specific configuration so you can fine-tune various

 # backing providers for Vagrant. These expose provider-specific options.

 # Example for VirtualBox:

 #

 #config.vm.provider "virtualbox" do |vb|

 #   # Display the VirtualBox GUI when booting the machine

 #   vb.gui = true

 #

 #   # Customize the amount of memory on the VM:

 #   vb.memory = "1024"

 # end

 # change vm memory from default(512) to 1024

 config.vm.provider "virtualbox" do |vb|

    vb.memory = "4096"

 end

 #

 # View the documentation for the provider you are using for more

 # information on available options.

 # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies

 # such as FTP and Heroku are also available. See the documentation at

 # https://docs.vagrantup.com/v2/push/atlas.html for more information.

 # config.push.define "atlas" do |push|

 #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"

 # end

 # Enable provisioning with a shell script. Additional provisioners such as

 # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the

 # documentation for more information about their specific syntax and use.

 # config.vm.provision "shell", inline: <<-SHELL

 #   apt-get update

 #   apt-get install -y apache2

 # SHELL

 # create 1 vm

   config.vm.define "hyperledger" do |node|

     node.vm.hostname = "hperledger"

     node.vm.network "private_network", ip: "192.168.12.11"

     node.vm.provision "shell", path: "init.sh"

   end

end


这里指定了启动一个Ubuntu/trusty64的虚拟机, 指定了IP地址为 192.168.12.11, 启动时将执行init.sh初始化脚本.


4. 在 fabric-net 目录下,创建 init.sh 文件, 内容如下:

#!/bin/bash

set -e

#### 1 Install docker and docker-compose ####

# 1.1 Update the apt package index:

echo "1.1 Update the apt package index"

apt-get update

# 1.2 Install packages to allow apt to use a repository over HTTPS:

echo "1.2 Install packages to allow apt to use a repository over HTTPS"

apt-get install -y \

   apt-transport-https \

   ca-certificates \

   curl \

   software-properties-common

# 1.3 Add Docker’s official GPG key:

echo "1.3 Add Docker’s official GPG key"

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

# 1.4 Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

echo "1.4 Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint"

apt-key fingerprint 0EBFCD88

# 1.5 Use the following command to set up the stable repository.

echo "1.5 Set up the stable repository"

add-apt-repository \

  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \

  $(lsb_release -cs) \

  stable"

# 1.6 Install docker ce

echo "1.6 Install docker ce"

apt-get update

apt-get install -y docker-ce

# 1.7 Install docker-compose

echo "1.7 Install docker-compose"

curl -L https://github.com/docker/compose/releases/download/1.20.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

# 1.8 Install docker-compose command auto-complete

#curl -L https://raw.githubusercontent.com/docker/compose/1.20.1/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

#### 2 Install golang ####

# 2.1 Download and extract

echo "2 Install golang"

wget https://dl.google.com/go/go1.10.linux-amd64.tar.gz

tar -C /usr/local -xzf go1.10.linux-amd64.tar.gz

export PATH=$PATH:/usr/local/go/bin

# 2.2 Set GOPATH

mkdir $HOME/go

export GOPATH=$HOME/go

export PATH=$PATH:$GOPATH/bin

# 2.3 Set PATH ENV to .bashrc

echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc

echo 'export GOPATH=$HOME/go' >> ~/.bashrc

echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc

#### 3 Run Fabric sample

# 3.1 Install git

echo "3.1 Install git"

apt-get install -y git

# 3.2 Download fabric code

echo "3.2 Download fabric code"

mkdir $HOME/fabric ; cd $HOME/fabric

git clone -b master https://github.com/hyperledger/fabric-samples.git

cd  fabric-samples

git checkout v1.1.0

# 3.3 Set docker registry mirror

echo "3.3 Set docker registry mirror"

echo '{ "registry-mirrors": ["https://registry.docker-cn.com"]  }' > /etc/docker/daemon.json

service docker restart

# 3.4 Init env

echo "3.4 Init env"

curl -L https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.1.0

# 3.5 Set env var

echo "3.5 Set env var"

export PATH=$HOME/fabric/fabric-samples/bin:$PATH

echo "export PATH=$HOME/fabric/fabric-samples/bin:$PATH" >> ./bashrc

# 3.6 Setup First Network

echo "3.6 Setup First Network"

cd fabric-samples/first-network

echo "3.6.1 Run ./byfn.sh -m generate"

./byfn.sh -m generate

echo "3.6.2 Run ./byfn.sh -m up -l node"

./byfn.sh -m up -l node

echo "3.6.4 Run ./byfn.sh -m down"

./byfn.sh -m down


该初始化脚本分为3个部分,第一部分安装docker及docker-compose环境;第二部分安装go语言环境; 第三部分安装fabric示例并执行示例程序.


5. 启动虚拟机机环境

在 fabric-net 目录中,启动命令行窗口, 运行命令:

vagrant up

即可演示整个过程.

在第一次运行时, vagrant会自动从网上下载ubuntu/trusty64的虚拟机模板 (box),然后启动虚拟机, 并在虚拟机启动后执行init脚本.

根据网络环境, 整个过程可能会花费10~60分钟的时间.

如果网速过慢,虚拟机模板下载会超时,这时可以用工具直接下载该box(地址:http://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box ), 然后使用vagrant box add  命令导入.


6. 离线环境

在实验过程中,往往会出现修改了一些地方后,出现莫名其妙的问题,对于初学者来说,修复这些问题将会非常痛苦. 这时就可以使用Vagrant的管理功能了, 可以通过 vagrant  destroy -f 销毁虚拟机,然后再运行 vagrant up 重建环境.


然而如果每次vagrant up 都需要初始化fabric的话, 也将会浪费很多时间, 因为示例程序的下载和docker images的下载都会很耗时间.基于此, 我制作了离线的版本, 不需要联网即可把环境搭建起来, 整个过程只需要1~2分钟时间.


制作方法是, 将所有的安装包及docker映像保存到本地; 对于deb包,直接使用dpkg命令安装;对于docker映像, 使用docker load命令直接导入.


退出 fabric -net  目录, 在其同级目录中新建一个  fabric-local  目录, 创建 Vagrantfile 文件, 内容如下:

# -*- mode: ruby -*-

# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure

# configures the configuration version (we support older styles for

# backwards compatibility). Please don't change it unless you know what

# you're doing.

Vagrant.configure("2") do |config|

 # The most common configuration options are documented and commented below.

 # For a complete reference, please see the online documentation at

 # https://docs.vagrantup.com.

 # Every Vagrant development environment requires a box. You can search for

 # boxes at https://atlas.hashicorp.com/search.

 config.vm.box = "ubuntu/trusty64"

 # Disable automatic box update checking. If you disable this, then

 # boxes will only be checked for updates when the user runs

 # `vagrant box outdated`. This is not recommended.

 # config.vm.box_check_update = false

 # Create a forwarded port mapping which allows access to a specific port

 # within the machine from a port on the host machine. In the example below,

 # accessing "localhost:8080" will access port 80 on the guest machine.

 # config.vm.network "forwarded_port", guest: 80, host: 8080

 # Create a private network, which allows host-only access to the machine

 # using a specific IP.

 # config.vm.network "private_network", ip: "192.168.33.10"

 # Create a public network, which generally matched to bridged network.

 # Bridged networks make the machine appear as another physical device on

 # your network.

 # config.vm.network "public_network"

 # Share an additional folder to the guest VM. The first argument is

 # the path on the host to the actual folder. The second argument is

 # the path on the guest to mount the folder. And the optional third

 # argument is a set of non-required options.

 config.vm.synced_folder "./data", "/vagrant_data"

 # Provider-specific configuration so you can fine-tune various

 # backing providers for Vagrant. These expose provider-specific options.

 # Example for VirtualBox:

 #

 #config.vm.provider "virtualbox" do |vb|

 #   # Display the VirtualBox GUI when booting the machine

 #   vb.gui = true

 #

 #   # Customize the amount of memory on the VM:

 #   #vb.memory = "1024"

 #end

 # change vm memory from default(512) to 1024

 config.vm.provider "virtualbox" do |vb|

    vb.memory = "4096"

 end

 #

 # View the documentation for the provider you are using for more

 # information on available options.

 # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies

 # such as FTP and Heroku are also available. See the documentation at

 # https://docs.vagrantup.com/v2/push/atlas.html for more information.

 # config.push.define "atlas" do |push|

 #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"

 # end

 # Enable provisioning with a shell script. Additional provisioners such as

 # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the

 # documentation for more information about their specific syntax and use.

 # config.vm.provision "shell", inline: <<-SHELL

 #   apt-get update

 #   apt-get install -y apache2

 # SHELL

 # create 1 vm

   config.vm.define "hyperledger" do |node|

     node.vm.hostname = "hperledger"

     node.vm.network "private_network", ip: "192.168.12.12"

     node.vm.provision "shell", path: "init_local.sh"

   end

end


与前面的Vagrantfile不同的是,这个文件中通过 config.vm.synced_folder "./data", "/vagrant_data" 指定了同步目录, 将主机当前目录下面的 data 目录与虚拟机共享, 在虚拟机中, 通过  /vagrant_data 目录即可访问


创建  init_local.sh 文件, 内容如下:

#!/bin/bash

set -e

#### Init ENV ####

# 1.1 Install docker all archives

sudo dpkg -i /vagrant_data/archives/*.deb

# 1.2 Install docker-compose

sudo cp /vagrant_data/archives/docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

# 1.3 Install golang

sudo tar -C /usr/local -xzf /vagrant_data/archives/go1.10.linux-amd64.tar.gz

export PATH=$PATH:/usr/local/go/bin

# 1.4 Set PATH ENV

mkdir $HOME/go

export GOPATH=$HOME/go

export PATH=$PATH:$GOPATH/bin

# 1.5 Set PATH ENV to .bashrc

echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc

echo 'export GOPATH=$HOME/go' >> ~/.bashrc

echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc

#### 2 Run Fabric sample

# 2.1 Extract fabric

cd $HOME

tar -xvf /vagrant_data/fabric-1.1.0.tar.gz

# 2.2 Load docker images

docker load -i /vagrant_data/images.tar

# 2.3 Build Your First Network

cd $HOME/fabric/fabric-samples/first-network

echo "2.3.1 Run ./byfn.sh -m generate"

./byfn.sh -m generate

echo "2.3.2 Run ./byfn.sh -m up "

# It will take a long time (servel minutes if set language as node (-l node).

#./byfn.sh -m up -l node  

./byfn.sh -m up

echo "2.3.3 Run ./byfn.sh -m down"

./byfn.sh -m down


7. 资源下载

本文中所有实验资料已经放到了百度云盘中,

在线版本下载:  链接:  https://pan.baidu.com/s/1bSUDTKMefWaDmHn6hgG_1w  密码: xqka

离线版本下载: 链接: https://pan.baidu.com/s/1QY10iAt-4AFQ-7B1LX6Crg  密码: 5grp


==更多原创内容分享,请扫码关注公众号: 超级账本开发 ==


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值