基于ethereum的五种链环境搭建方式 第11章 方式四、使用docker-compose进行多节点部署

  1. 创建数据目录及多节点目录
     > mkdir -p ~/workmeta/EduEthereumServerDeploy/deploy_2/{node1,node2}
     > cd ~/workmeta/EduEthereumServerDeploy/deploy_2
    
  2. 预先创建挖矿帐户及预置资金帐户
     # 1. 创建密码文件
     > echo "111111" > .passwd
    
     # 2. 为node1创建帐户
     > for ((n=0;n<2;n++)); do geth account new --password .passwd --datadir ./node1; done
     INFO [10-30|11:43:51] Maximum peer count                       ETH=25 LES=0 total=25
     Address: {232327606a63b5e53516dd849c45c6afce4ed717}
     INFO [10-30|11:43:53] Maximum peer count                       ETH=25 LES=0 total=25
     Address: {b3ab8ddb59ea9692586fa6d1348bc9f5333659ff}
    
     # 3. 为node2创建帐户
     > for ((n=0;n<2;n++)); do geth account new --password .passwd --datadir ./node2; done
     INFO [10-30|11:44:31] Maximum peer count                       ETH=25 LES=0 total=25
     Address: {bb7d18dc172aa7e54601222d0a4df6ecddf9c77f}
     INFO [10-30|11:44:32] Maximum peer count                       ETH=25 LES=0 total=25
     Address: {8efd41ebca913fa45c2d32f95ccdb903e0809d53}
    
节点 挖矿帐户 资金帐户
node1 232327606a63b5e53516dd849c45c6afce4ed717 b3ab8ddb59ea9692586fa6d1348bc9f5333659ff
node2 bb7d18dc172aa7e54601222d0a4df6ecddf9c77f 8efd41ebca913fa45c2d32f95ccdb903e0809d53
  1. 生成创始区块配置
    操作步骤可参考[五、配置创世区块]

    注: 因大家在本地生成的帐户与教程中的帐户不同,所以需要根据自己生成帐户进行创世区块配置。

  2. 初始化链数据
     # 初始化node1链数据
     > geth init --datadir ./node1 pubchain.json 
     # 初始化node2链数据
     > geth init --datadir ./node2 pubchain.json
    

    查看当前工程目录

     .
     ├── node1
     │   ├── geth
     │   │   ├── chaindata
     │   │   │   ├── 000001.log
     │   │   │   ├── CURRENT
     │   │   │   ├── LOCK
     │   │   │   ├── LOG
     │   │   │   └── MANIFEST-000000
     │   │   └── lightchaindata
     │   │       ├── 000001.log
     │   │       ├── CURRENT
     │   │       ├── LOCK
     │   │       ├── LOG
     │   │       └── MANIFEST-000000
     │   └── keystore
     │       ├── UTC--2018-10-30T03-43-51.917428870Z--232327606a63b5e53516dd849c45c6afce4ed717
     │       └── UTC--2018-10-30T03-43-53.047177001Z--b3ab8ddb59ea9692586fa6d1348bc9f5333659ff
     ├── node2
     │   ├── geth
     │   │   ├── chaindata
     │   │   │   ├── 000001.log
     │   │   │   ├── CURRENT
     │   │   │   ├── LOCK
     │   │   │   ├── LOG
     │   │   │   └── MANIFEST-000000
     │   │   └── lightchaindata
     │   │       ├── 000001.log
     │   │       ├── CURRENT
     │   │       ├── LOCK
     │   │       ├── LOG
     │   │       └── MANIFEST-000000
     │   └── keystore
     │       ├── UTC--2018-10-30T03-44-31.107508622Z--bb7d18dc172aa7e54601222d0a4df6ecddf9c77f
     │       └── UTC--2018-10-30T03-44-32.272686672Z--8efd41ebca913fa45c2d32f95ccdb903e0809d53
     └── pubchain.json
    
     10 directories, 25 files
    
     # 工程可参考: https://github.com/ChainDesk/EduEthereumServerDeploy/tree/master/deploy_4
    
  1. 配置Dockerfile
     > touch Dockerfile
     > vim Dockerfile
     #在Dockerfile文件中加入以下内容
     FROM golang:1.10-alpine as builder
    
     MAINTAINER zhangliang <mail@zhangliang@cldy.org>
    
     RUN apk add --no-cache make gcc musl-dev linux-headers git
     RUN git clone --depth 1 --branch release/1.8 https://github.com/ethereum/go-ethereum /go-ethereum
     RUN cd /go-ethereum && make all
    
     FROM alpine:latest
     RUN apk add --no-cache ca-certificates
     COPY --from=builder /go-ethereum/build/bin/* /usr/local/bin/
    
     WORKDIR "/opt"
    
     ENV coinbase=""
     #ENV bootnode_hashkey=""
     #ENV bootnode_ipaddr=""
     #--bootnodes "enode://$bootnode_hashkey@$bootnode_ipaddr:30301"
     ENV datadir=""
    
     CMD exec geth --datadir ./$datadir  --verbosity=4 --rpc --rpcapi "eth,web3,personal,net,miner,admin,debug,db" --rpcaddr "0.0.0.0" --rpccorsdomain "*" --syncmode=full --mine --unlock $coinbase --password .passwd
    
     EXPOSE 8545
     EXPOSE 30303
    
  2. 配置docker-compose
     > touch docker-compose.yaml
     > vim docker-compose.yaml
     #在Dockerfile文件中加入以下内容
     version: "3"
    
     services:
       sealnode-1:
         container_name: sealnode-1
         hostname: sealnode-1
         environment:
           coinbase: 232327606a63b5e53516dd849c45c6afce4ed717
           datadir: node1
         build:
           context: .
         ports:
           - 8545:8545
           - 30303:30303
         volumes:
           - .:/opt
         networks:
           - mynet
    
       sealnode-2:
         container_name: sealnode-2
         hostname: sealnode-2
         environment:
           coinbase: bb7d18dc172aa7e54601222d0a4df6ecddf9c77f
           datadir: node2
         build:
           context: .
         ports:
           - 8546:8545
           - 30304:30303
         volumes:
           - .:/opt
         networks:
           - mynet
    
     networks:
       default:
         driver: bridge
       mynet:
         driver: bridge
    
  3. 启动多节点
     > docker-compose up -d
     > docker-compose ps
     Name                 Command               State                        Ports
     ------------------------------------------------------------------------------------------------------
     sealnode-1   /bin/sh -c exec geth --dat ...   Up      0.0.0.0:30303->30303/tcp, 0.0.0.0:8545->8545/tcp
     sealnode-2   /bin/sh -c exec geth --dat ...   Up      0.0.0.0:30304->30303/tcp, 0.0.0.0:8546->8545/tcp
    
  4. 节点间发现
     # 1. 查看node1节点enode
     > geth attach http://localhost:8545
     > admin.nodeInfo.enode
     "enode://57395a4d08ef22f2e2b48e86bb792ec43055233b72cd059b1e58ea608b572ad08ba259296b27d3b0ed190d5d2e10b62020e5b3e45cacd9ed3a68061bb09b865e@[::]:30303"
     > ctrl+d退出
    
     #2. 查看node1节点ip
     > docker exec -it sealnode-1 /bin/sh
     /opt # ifconfig 
     eth0      Link encap:Ethernet  HWaddr 02:42:AC:14:00:02
               inet addr:172.20.0.2  Bcast:172.20.255.255  Mask:255.255.0.0
               UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
               RX packets:23698 errors:0 dropped:0 overruns:0 frame:0
               TX packets:28399 errors:0 dropped:0 overruns:0 carrier:0
               collisions:0 txqueuelen:0
               RX bytes:3117030 (2.9 MiB)  TX bytes:2805356 (2.6 MiB)
    
     3. 在node2中添加node1的enode
     # node1's enode: "enode://57395a4d08ef22f2e2b48e86bb792ec43055233b72cd059b1e58ea608b572ad08ba259296b27d3b0ed190d5d2e10b62020e5b3e45cacd9ed3a68061bb09b865e@172.20.0.2:30303"
     > geth attach http://localhost:8546
     > admin.addPeer("enode://57395a4d08ef22f2e2b48e86bb792ec43055233b72cd059b1e58ea608b572ad08ba259296b27d3b0ed190d5d2e10b62020e5b3e45cacd9ed3a68061bb09b865e@172.20.0.2:30303")
    
     4. 检查是否添加节点成功
     > admin.peers
     [{
         caps: ["eth/62", "eth/63"],
         enode: "enode://57395a4d08ef22f2e2b48e86bb792ec43055233b72cd059b1e58ea608b572ad08ba259296b27d3b0ed190d5d2e10b62020e5b3e45cacd9ed3a68061bb09b865e@172.20.0.2:30303",
         id: "624e846604becf64ba489f3a6ecb227102d3143414ad45e19954bf71998823b2",
         name: "Geth/v1.8.17-stable-8bbe7207/linux-amd64/go1.10.4",
         network: {
           inbound: false,
           localAddress: "172.20.0.3:42362",
           remoteAddress: "172.20.0.2:30303",
           static: true,
           trusted: false
         },
         protocols: {
           eth: {
             difficulty: 2,
             head: "0x4c2d6795e503ffc19528fa3b935b40fae0cca2591fa71bb200a3a7452db98505",
             version: 63
           }
         }
     }]
    
  5. 节点服务验证
         > geth attach http://localhost:8546 
    
         # -------------转帐--BEFORE-------------------
         // 查询node1挖矿帐户余额(232327606a63b5e53516dd849c45c6afce4ed717)
         > web3.eth.getBalance("232327606a63b5e53516dd849c45c6afce4ed717")
         0
         // 查询node2资金帐户余额(8efd41ebca913fa45c2d32f95ccdb903e0809d53)
         > web3.eth.getBalance("8efd41ebca913fa45c2d32f95ccdb903e0809d53")
         9.04625697166532776746648320380374280103671755200316906558262375061821325312e+74
    
         # -------------转帐--ING---------------------
         #  node2资金帐户(8efd41ebca913fa45c2d32f95ccdb903e0809d53)向node1挖矿帐户(232327606a63b5e53516dd849c45c6afce4ed717)转帐10eth
         # 1. 解锁帐户
         > web3.personal.unlockAccount("0x8efd41ebca913fa45c2d32f95ccdb903e0809d53")
         Unlock account 0x8efd41ebca913fa45c2d32f95ccdb903e0809d53
         Passphrase:
         true
         # 2. 转帐
         > web3.eth.sendTransaction({from: "0x8efd41ebca913fa45c2d32f95ccdb903e0809d53", to: "0x232327606a63b5e53516dd849c45c6afce4ed717", value: web3.toWei(10, "ether")})
    
         # -------------转帐--AFTER--------------------
         // 1. 查询node1帐户余额(0x232327606a63b5e53516dd849c45c6afce4ed717)
         > web3.fromWei(eth.getBalance("0x232327606a63b5e53516dd849c45c6afce4ed717"),"ether")
         10
         // 2. 查询node2帐户余额(0x8efd41ebca913fa45c2d32f95ccdb903e0809d53)
         > web3.fromWei(eth.getBalance("0x8efd41ebca913fa45c2d32f95ccdb903e0809d53"),"ether") 
         9.04625697166532776746648320380374280103671755200316906548261997061821325312e+56
    

未经授权禁止转载、改编,转载请注明出处!

本文由 区块链技术网 作者:区块链 发表,其版权均为 区块链技术网 所有,文章内容系作者个人观点,不代表 区块链技术网 对观点赞同或支持。如需转载,请注明文章来源。

发表评论