Hyperledger Fabric V1.0 在Ubuntu16.04+中的环境搭建

一、准备

所需工具:Ubuntu、git、pip、go、docker、docker-compose
windows用户可以下载一个VirtualBox,在VirtualBox中装Ubuntu虚拟机。需注意网络设置时NAT模式和桥接模式的不同。
(建议启用root用户来安装后续工具软件。)

二、安装git

后续需要用git从GitHub中下载hyperledger的源码。

$ apt-get update                                                            
$ apt-get install git   
$ git clone https://github.com/hyperledger/fabric.git
$ git checkout v1.0.0

三、安装go

Go也被称为Golang,它是由谷歌创建的一种开源、编译和静态类型的编程语言,是hyperledger的编程语言。下载解压后需要配置环境变量,具体过程如下。

$ wget https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz    
$ tar -C /usr/local -xzf go1.8.linux-amd64.tar.gz                        
$ vi /etc/profile                                                        
追加内容                                                                    
export GOROOT=/usr/local/go                                                
export PATH=$PATH:$GOROOT/bin 
export GOPATH=/opt/gopath                                           
$ source /etc/profile 

可以通过go version命令查看当前go语言的版本。

四、安装pip

$ apt-get update
$ apt-get install python-pip
$ pip install --upgrade pip

五、安装docker

docker用来支持容器环境。
从不同的地方看到很多方法,以下列出两种,目前尚不清楚不同方法的区别是什么。
1. 可以通过Docker官方提供的脚本进行安装:

$ curl -sSL https://get.docker.com | sh

或者

$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun

2. 使用APT安装

$ apt-get install \                                                        
    apt-transport-https \                                                
    ca-certificates \                                                    
    curl \                                                                
    software-properties-common                                            
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg \                    
    | sudo apt-key add -                                                    
$ add-apt-repository \                                                    
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \            
   $(lsb_release -cs) \                                                    
   stable"                                                                
$ apt-get update                                                            
$ apt-get install docker-ce  

或者

$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

# 官方源
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -


$ sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

# 官方源
# $ sudo add-apt-repository \
#    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
#    $(lsb_release -cs) \
#    stable"

鉴于国内网络问题,yeasy强烈建议使用国内源下载docker,具体请参考文后网址2,个人感觉yeasy的方法比较权威。
启动docker CE

$ sudo systemctl enable docker
$ sudo systemctl start docker

建立docker用户组
默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
建立 docker 组(参考3中没有此步):

$ sudo groupadd docker

将当前用户加入 docker 组:

$ sudo usermod -aG docker $USER

退出当前终端并重新登录,进行如下测试。
测试Docker 是否安装正确

$ docker run hello-world

出现Hello from Docker!等信息说明安装正确。
镜像加速
使用docker官方的docker hub速度太慢,可以使用国内的镜像作为加速器。
使用修改配置文件的方法,配置文件如果不存在则新建,对于使用 systemd 的系统,请在 /etc/docker/daemon.json 中写入如下内容:

{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"] 
}

注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。
之后重新启动服务。
重启守护

service docker restart

关闭守护进程可使用下面的命令:

service docker stop

六、安装docker compose

Docker Compose 是Docker 官方编排(Orchestration)项目之一,负责快速的部署分布式应用,是一个用来定义和运行复杂应用的Docker工具。

$ pip install docker-compose>=1.8.0

注意事项:

  1. 新装的Ubuntu系统输su命令会报错Authentication failure,这时需要用sudo passwd root命令更改/设置root账号密码,后续就可以直接su然后输密码切换至root账户了。
  2. Ubuntu16.04是采用的Systemd进行系统和服务管理,而非upstart(Ubuntu14.04等采用),在重启docker服务时命令不同。
    upstart:
$ sudo service docker restart

systemd:

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

3.使用pip安装docker-compose时可能报错说You are using pip version 8.1.1, however version 9.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
按照提示中的命令更新pip即可。
更新pip后如果还是报错”/usr/bin/pip”, line 9, in from pip import main ImportError: cannot import name main,则需要修改/usr/bin/pip,将原来的:

from pip import main
if __name__ == '__main__':
    sys.exit(main())

改成:

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

这种错误是因为将pip更新为10.0.0后库里面的函数有所变动造成的。

参考:

Hyperledger Fabric V1.0 环境搭建
yeasy:Ubuntu 安装 Docker CE
《深度探索区块链——hyperledger技术与应用》

猜你喜欢

转载自blog.csdn.net/vivian_ll/article/details/79754119