【IPFS】IPFS部署分布式项目到应用服务器的实践

前言

上一篇文章主要做了本地开发机的环境安装和demo运行,算是对IPFS有了一些认识,具体查看http://blog.csdn.net/diandianxiyu_geek/article/details/77171820,这次部署在对外网开放的服务器上,解决遇到的小问题和体会分布式部署到节点。

服务器环境


//查看nginx版本
[root@xxxxxxxxxxxx ~]# nginx -v
nginx version: nginx/1.9.7

//查看php版本
[root@xxxxxxxxxxxxxxxxxx ~]# php -v
PHP 7.0.0 (cli) (built: Dec 10 2015 10:56:28) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

//查看mysql版本
[root@xxxxxxxxx ~]# mysql --help | grep Distrib 
mysql  Ver 14.14 Distrib 5.6.28, for Linux (x86_64) using  EditLine wrapper

实践

为了保证安装过程中失误造成不必要的损失,最好备份下服务器磁盘快照~

[root@xxxxxxxxx ipfs_zip]# wget https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
--2017-08-15 10:09:35--  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Resolving dist.ipfs.io (dist.ipfs.io)... 147.135.130.181, 217.182.195.23
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8580949 (8.2M) [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz0% [                                       ] 32,223      --.-K/s   in 0s      

2017-08-15 10:09:37 (132 MB/s) - Connection closed at byte 32223. Retrying.

--2017-08-15 10:09:38--  (try: 2)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8548726 (8.2M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz0% [                                       ] 64,389      --.-K/s   in 0s      

2017-08-15 10:09:39 (122 MB/s) - Connection closed at byte 64389. Retrying.

--2017-08-15 10:09:41--  (try: 3)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8516560 (8.1M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz1% [                                       ] 96,555      --.-K/s   in 0s      

2017-08-15 10:09:43 (119 MB/s) - Connection closed at byte 96555. Retrying.

--2017-08-15 10:09:46--  (try: 4)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8484394 (8.1M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz1% [                                       ] 128,721     --.-K/s   in 0s      

2017-08-15 10:09:47 (108 MB/s) - Connection closed at byte 128721. Retrying.

--2017-08-15 10:09:51--  (try: 5)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8452228 (8.1M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz1% [                                       ] 160,886     --.-K/s   in 0s      

2017-08-15 10:09:53 (111 MB/s) - Connection closed at byte 160886. Retrying.

--2017-08-15 10:09:58--  (try: 6)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8420063 (8.0M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz2% [                                       ] 193,051     --.-K/s   in 0s      

2017-08-15 10:09:59 (111 MB/s) - Connection closed at byte 193051. Retrying.

--2017-08-15 10:10:05--  (try: 7)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8387898 (8.0M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz2% [>                                      ] 225,216     --.-K/s   in 0s      

2017-08-15 10:10:07 (129 MB/s) - Connection closed at byte 225216. Retrying.

--2017-08-15 10:10:14--  (try: 8)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8355733 (8.0M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz2% [+                                      ] 257,381     --.-K/s   in 0s      

2017-08-15 10:10:15 (118 MB/s) - Connection closed at byte 257381. Retrying.

--2017-08-15 10:10:23--  (try: 9)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8323568 (7.9M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz3% [+                                      ] 289,546     --.-K/s   in 0s      

2017-08-15 10:10:25 (108 MB/s) - Connection closed at byte 289546. Retrying.

--2017-08-15 10:10:34--  (try:10)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8291403 (7.9M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz3% [+                                      ] 321,711     --.-K/s   in 0s      

2017-08-15 10:10:35 (115 MB/s) - Connection closed at byte 321711. Retrying.

--2017-08-15 10:10:45--  (try:11)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8259238 (7.9M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz4% [+                                      ] 353,876     --.-K/s   in 0s      

2017-08-15 10:10:46 (121 MB/s) - Connection closed at byte 353876. Retrying.

--2017-08-15 10:10:56--  (try:12)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8227073 (7.8M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz4% [+                                      ] 386,041     --.-K/s   in 0s      

2017-08-15 10:10:58 (131 MB/s) - Connection closed at byte 386041. Retrying.

--2017-08-15 10:11:08--  (try:13)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8194908 (7.8M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz4% [+                                      ] 418,206     --.-K/s   in 0s      

2017-08-15 10:11:09 (112 MB/s) - Connection closed at byte 418206. Retrying.

--2017-08-15 10:11:19--  (try:14)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8162743 (7.8M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz5% [+>                                     ] 450,371     --.-K/s   in 0s      

2017-08-15 10:11:21 (121 MB/s) - Connection closed at byte 450371. Retrying.

--2017-08-15 10:11:31--  (try:15)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8130578 (7.8M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz5% [++                                     ] 482,536     --.-K/s   in 0s      

2017-08-15 10:11:32 (125 MB/s) - Connection closed at byte 482536. Retrying.

--2017-08-15 10:11:42--  (try:16)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8098413 (7.7M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz5% [++                                     ] 514,701     --.-K/s   in 0s      

2017-08-15 10:11:44 (120 MB/s) - Connection closed at byte 514701. Retrying.

--2017-08-15 10:11:54--  (try:17)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8066248 (7.7M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz6% [++                                     ] 546,866     --.-K/s   in 0s      

2017-08-15 10:11:56 (124 MB/s) - Connection closed at byte 546866. Retrying.

--2017-08-15 10:12:06--  (try:18)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8034083 (7.7M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz6% [++                                     ] 579,031     --.-K/s   in 0.004s  

2017-08-15 10:12:07 (8.42 MB/s) - Connection closed at byte 579031. Retrying.

--2017-08-15 10:12:17--  (try:19)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 8001918 (7.6M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz7% [++                                     ] 611,196     --.-K/s   in 0s      

2017-08-15 10:12:19 (114 MB/s) - Connection closed at byte 611196. Retrying.

--2017-08-15 10:12:29--  (try:20)  https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
Connecting to dist.ipfs.io (dist.ipfs.io)|147.135.130.181|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 8580949 (8.2M), 7969753 (7.6M) remaining [application/x-gzip]
Saving to: ‘go-ipfs_v0.4.10_linux-amd64.tar.gz7% [++                                     ] 643,361     --.-K/s   in 0s      

2017-08-15 10:12:30 (128 MB/s) - Connection closed at byte 643361. Giving up.

Giving up了,官方安装包下载不下来,所以先下载在电脑上,再上传到服务器上。。。。。。

需要下载的文件

[root@xxxxxxxxxxxxxxxx ipfs_zip]# tar xvfz go-ipfs_v0.4.10_linux-amd64.tar.gz   //解压
go-ipfs/build-log
go-ipfs/install.sh
go-ipfs/ipfs
go-ipfs/LICENSE
go-ipfs/README.md
[root@xxxxxxx ipfs_zip]# mv go-ipfs/ipfs /usr/local/bin/ipfs   //移动执行文件
[root@xxxxxxxxx ipfs_zip]# ipfs help //执行命令,说明安装成功
USAGE
  ipfs - Global p2p merkle-dag filesystem.

  ipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...

SUBCOMMANDS
  BASIC COMMANDS
    init          Initialize ipfs local configuration
    add <path>    Add a file to IPFS
    cat <ref>     Show IPFS object data
    get <ref>     Download IPFS objects
    ls <ref>      List links from an object
    refs <ref>    List hashes of links from an object

  DATA STRUCTURE COMMANDS
    block         Interact with raw blocks in the datastore
    object        Interact with raw dag nodes
    files         Interact with objects as if they were a unix filesystem
    dag           Interact with IPLD documents (experimental)

  ADVANCED COMMANDS
    daemon        Start a long-running daemon process
    mount         Mount an IPFS read-only mountpoint
    resolve       Resolve any type of name
    name          Publish and resolve IPNS names
    key           Create and list IPNS name keypairs
    dns           Resolve DNS links
    pin           Pin objects to local storage
    repo          Manipulate the IPFS repository
    stats         Various operational stats
    p2p           Libp2p stream mounting
    filestore     Manage the filestore (experimental)

  NETWORK COMMANDS
    id            Show info about IPFS peers
    bootstrap     Add or remove bootstrap peers
    swarm         Manage connections to the p2p network
    dht           Query the DHT for values or peers
    ping          Measure the latency of a connection
    diag          Print diagnostics

  TOOL COMMANDS
    config        Manage configuration
    version       Show ipfs version information
    update        Download and apply go-ipfs updates
    commands      List all available commands

  Use 'ipfs <command> --help' to learn more about each command.

  ipfs uses a repository in the local file system. By default, the repo is located
  at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable:

    export IPFS_PATH=/path/to/ipfsrepo

  EXIT STATUS

  The CLI will exit with one of the following values:

  0     Successful execution.
  1     Failed executions.


至此,安装完成。


[root@xxxxxxxxxx ipfs_zip]# ipfs init
initializing IPFS node at /root/.ipfs
generating 2048-bit RSA keypair...done
peer identity: QmdSkEjtdHsifEySmGZ9EGG3vpmSa4zNWG5oavuTvuyvPN
to get started, enter:

    ipfs cat /ipfs/QmVLDAhCY3X9P2uRudKAryuQFPM5zqA3Yij1dY8FpGbL7T/readme


[root@xxxxxxxx ipfs_zip]# ipfs cat /ipfs/QmVLDAhCY3X9P2uRudKAryuQFPM5zqA3Yij1dY8FpGbL7T/readme
Hello and Welcome to IPFS!

██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗  ███████╗
██║██╔═══╝ ██╔══╝  ╚════██║
██║██║     ██║     ███████║
╚═╝╚═╝     ╚═╝     ╚══════╝

If you're seeing this, you have successfully installed
IPFS and are now interfacing with the ipfs merkledag!

 -------------------------------------------------------
| Warning:                                              |
|   This is alpha software. Use at your own discretion! |
|   Much is missing or lacking polish. There are bugs.  |
|   Not yet secure. Read the security notes for more.   |
 -------------------------------------------------------

Check out some of the other files in this directory:

  ./about
  ./help
  ./quick-start     <-- usage examples
  ./readme          <-- this file
  ./security-notes

[root@xxxxxxxxxx ipfs_zip]# ipfs cat  /ipfs/QmVLDAhCY3X9P2uRudKAryuQFPM5zqA3Yij1dY8FpGbL7T/quick-start
# 0.1 - Quick Start

This is a set of short examples with minimal explanation. It is meant as
a "quick start". Soon, we'll write a longer tour :-)


Add a file to ipfs:

  echo "hello world" >hello
  ipfs add hello


View it:

  ipfs cat <the-hash-you-got-here>


Try a directory:

  mkdir foo
  mkdir foo/bar
  echo "baz" > foo/baz
  echo "baz" > foo/bar/baz
  ipfs add -r foo


View things:

  ipfs ls <the-hash-here>
  ipfs ls <the-hash-here>/bar
  ipfs cat <the-hash-here>/baz
  ipfs cat <the-hash-here>/bar/baz
  ipfs cat <the-hash-here>/bar
  ipfs ls <the-hash-here>/baz


References:

  ipfs refs <the-hash-here>
  ipfs refs -r <the-hash-here>
  ipfs refs --help


Get:

  ipfs get <the-hash-here> -o foo2
  diff foo foo2


Objects:

  ipfs object get <the-hash-here>
  ipfs object get <the-hash-here>/foo2
  ipfs object --help


Pin + GC:

  ipfs pin add <the-hash-here>
  ipfs repo gc
  ipfs ls <the-hash-here>
  ipfs pin rm <the-hash-here>
  ipfs repo gc


Daemon:

  ipfs daemon  (in another terminal)
  ipfs id


Network:

  (must be online)
  ipfs swarm peers
  ipfs id
  ipfs cat <hash-of-remote-object>


Mount:

  (warning: fuse is finicky!)
  ipfs mount
  cd /ipfs/<the-hash-here>
  ls


Tool:

  ipfs version
  ipfs update
  ipfs commands
  ipfs config --help
  open http://localhost:5001/webui


Browse:

  webui:

    http://localhost:5001/webui

  video:

    http://localhost:8080/ipfs/QmVc6zuAneKJzicnJpfrqCH9gSy6bz54JhcypfJYhGUFQu/play#/ipfs/QmTKZgRNwDNZwHtJSjCp6r5FYefzpULfy37JvMt9DwvXse

  images:

    http://localhost:8080/ipfs/QmZpc3HvfjEXvLWGQPWbHk3AjD5j8NEN4gmFN8Jmrd5g83/cs

  markdown renderer app:

    http://localhost:8080/ipfs/QmX7M9CiYXjVeFnkfVGf3y5ixTZ2ACeSGyL1vBJY1HvQPp/mdown


[root@xxxxx ipfs_zip]# ipfs daemon //启动
Initializing daemon...
Swarm listening on /ip4/10.144.176.230/tcp/4001
Swarm listening on /ip4/115.28.190.53/tcp/4001
Swarm listening on /ip4/115.28.190.53/tcp/4001
Swarm listening on /ip4/127.0.0.1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Error: serveHTTPGateway: manet.Listen(/ip4/127.0.0.1/tcp/8080) failed: listen tcp4 127.0.0.1:8080: bind: address already in use //端口占用,解决:修改配置文件,更改端口

[root@iZ2851te7e5Z ipfs_zip]# ipfs config //查看配置文件的命令
Error: Argument 'key' is required //报错暂时不管

USAGE
  ipfs config <key> [<value>] - Get and set ipfs config values.

  ipfs config [--bool] [--json] [--] <key> [<value>]

  'ipfs config' controls configuration variables. It works like 'git config'.
  The configuration values are stored in a config file inside your ipfs
  repository.

SUBCOMMANDS
  ipfs config edit           - Open the config file for editing in $EDITOR.
  ipfs config replace <file> - Replace the config with <file>.
  ipfs config show           - Output config file contents.

Use 'ipfs config --help' for more information about this command.

[root@xxxxxxxxx ipfs_zip]# ipfs config edit //修改配置文件
Error: ENV variable $EDITOR not set //没有设置默认编辑器
[root@xxxxxxxxxx ipfs_zip]# export EDITOR=vi  //修改默认编辑器
[root@xxxxxxxxxx ipfs_zip]# ipfs config edit //再次修改配置文件

修改配置文件中的AddressesGateway中的端口,这里改成了8081.

[root@iZ2851te7e5Z ipfs_zip]# ipfs daemon
Initializing daemon...
Swarm listening on /ip4/10.144.176.230/tcp/4001
Swarm listening on /ip4/115.28.190.53/tcp/4001
Swarm listening on /ip4/115.28.190.53/tcp/4001
Swarm listening on /ip4/127.0.0.1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8081
Daemon is ready

//运行成功

把上面的提示的地址换成自己服务器的IP,还是不能访问,原因在于配置问题。

[root@xxxxxxxxxx ipfs_zip]# ipfs config edit //再次修改配置文件

修改配置文件中的AddressesGateway`中的127.0.0.1改成0.0.0.0`.

还会出现的问题,dial tcp xxxxxx:8081: i/o timeout

问题总结

目前在服务器上出现以下问题:

  • 官网的安装包下载失败,一直在超时
  • 服务器运行ipfs后,没有打开过页面,一直出现IO的超时

可能造成这样的原因

  • IPFS官网以及官方提供的节点被阿里云墙了

解决思路:如果需要使用IPFS的服务,可以采用海外服务器作为服务的一个节点,构建分布式文件系统

上一篇:Sandworm团队与乌克兰电力部门的攻击事件有关


下一篇:深入Log4J源码之LoggerRepository和Configurator