如何在Ubuntu 16中无Docker安装Hyperledger Fabric并配置服务与多主机网络
Alright, let's break down your questions step by step—no Docker, setting up Fabric as system services, and multi-host networks on Ubuntu 16.04. I've walked through this process multiple times, so here's a practical, hands-on guide:
First, we need to get all dependencies sorted and compile Fabric from source, since we're skipping Docker.
Install base dependencies:
Update your packages and grab the tools we need for compilation:sudo apt-get update && sudo apt-get install -y git make gcc build-essentialSet up Go environment (Fabric 2.x requires Go 1.14+, which is compatible with Ubuntu 16.04):
- Download Go 1.14.15 (a stable long-term release):
wget https://dl.google.com/go/go1.14.15.linux-amd64.tar.gz - Extract it to
/usr/local:sudo tar -C /usr/local -xzf go1.14.15.linux-amd64.tar.gz - Configure environment variables by editing
~/.bashrc:
Add these lines at the end:export PATH=$PATH:/usr/local/go/bin export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin - Reload the shell and verify:
source ~/.bashrc go version # Should output go1.14.15 linux/amd64
- Download Go 1.14.15 (a stable long-term release):
Download and compile Fabric core components:
- Create the required directory structure and clone the repo:
mkdir -p $GOPATH/src/github.com/hyperledger cd $GOPATH/src/github.com/hyperledger git clone https://github.com/hyperledger/fabric.git cd fabric git checkout v2.2.10 # LTS version, stable for production - Compile all Fabric binaries (peer, orderer, configtxgen, etc.):
make all - Add the compiled binaries to your PATH (add this to
~/.bashrctoo for persistence):export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/build/bin - Verify installation:
peer version orderer version
- Create the required directory structure and clone the repo:
Optional: Install Fabric CA (for certificate management):
cd $GOPATH/src/github.com/hyperledger git clone https://github.com/hyperledger/fabric-ca.git cd fabric-ca git checkout v1.5.7 make all export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric-ca/build/bin
Ubuntu 16.04 uses systemd, so we'll create service files to run orderer and peer as background services that start on boot.
First, organize your Fabric data and configs in a central directory:
sudo mkdir -p /opt/hyperledger/{orderer,peer,config,certs}
- Place your genesis block, channel transaction files in
/opt/hyperledger/config - Place all MSP/TLS certificates in
/opt/hyperledger/certs - Update
core.yaml(peer config) andconfig.yaml(orderer config) to point to these paths (adjust paths likepeer.fileSystemPathororderer.fileSystemPathaccordingly).
Set up Orderer Service
Create a systemd service file:
sudo nano /etc/systemd/system/hyperledger-orderer.service
Paste this content (adjust User, Group, and paths to match your setup):
[Unit] Description=Hyperledger Fabric Orderer Service After=network.target [Service] User=ubuntu Group=ubuntu ExecStart=/home/ubuntu/go/src/github.com/hyperledger/fabric/build/bin/orderer start --configPath /opt/hyperledger/orderer/config.yaml Restart=always RestartSec=5 Environment=ORDERER_GENERAL_LOGLEVEL=info [Install] WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable hyperledger-orderer sudo systemctl start hyperledger-orderer # Check status sudo systemctl status hyperledger-orderer
Set up Peer Service
Create the peer service file:
sudo nano /etc/systemd/system/hyperledger-peer.service
Paste this content:
[Unit] Description=Hyperledger Fabric Peer Service After=network.target hyperledger-orderer.service [Service] User=ubuntu Group=ubuntu ExecStart=/home/ubuntu/go/src/github.com/hyperledger/fabric/build/bin/peer node start --configPath /opt/hyperledger/peer/core.yaml Restart=always RestartSec=5 Environment=CORE_LOGGING_LEVEL=info [Install] WantedBy=multi-user.target
Enable and start the peer service:
sudo systemctl daemon-reload sudo systemctl enable hyperledger-peer sudo systemctl start hyperledger-peer
The key here is shared certificates, proper network connectivity, and correct endpoint configuration across all hosts.
Step 1: Prepare Shared Assets on the Orderer Host
- Use
cryptogento generate MSP/TLS certificates for all orderers, peers, and organizations (create acrypto-config.yamlfirst to define your network topology). - Use
configtxgento generate the genesis block (genesis.block) and channel transaction file (channel.tx). - Copy these certificates and config files to all peer hosts using
scporrsync:scp -r /opt/hyperledger/certs user@peer-host-ip:/opt/hyperledger/ scp /opt/hyperledger/config/{genesis.block,channel.tx} user@peer-host-ip:/opt/hyperledger/config/
Step 2: Ensure Network Connectivity
- Open required ports on all hosts (use
ufwor your firewall):# On orderer host sudo ufw allow 7050/tcp # Orderer gRPC port # On peer hosts sudo ufw allow 7051/tcp # Peer gRPC port sudo ufw allow 7053/tcp # Peer event port - Update each peer's
core.yamlto set the external endpoint (so other peers can reach it):peer: gossip: externalendpoint: peer-host-ip:7051 - Update the orderer address in each peer's
core.yamland the orderer'sconfig.yamlto use the orderer's public IP/hostname.
Step 3: Start Services Across All Hosts
- Start the orderer service on the orderer host (as set up in section 2).
- Start the peer service on each peer host.
Step 4: Create and Join the Channel
- On one peer host, create the channel:
peer channel create -o orderer-ip:7050 -c mychannel -f /opt/hyperledger/config/channel.tx --tls --cafile /opt/hyperledger/certs/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem - Copy the generated
mychannel.blockto all other peer hosts, then have each peer join the channel:peer channel join -b mychannel.block
Step 5: Verify the Network
Install, approve, and commit a test chaincode, then submit transactions across peers to confirm cross-host communication works:
# Install chaincode on a peer peer lifecycle chaincode install mycc.tar.gz # Approve and commit it (follow Fabric 2.x chaincode lifecycle steps) # Invoke a transaction peer chaincode invoke -o orderer-ip:7050 -c mychannel -n mycc -c '{"Args":["init","a","100","b","200"]}' --tls --cafile /opt/hyperledger/certs/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
内容的提问来源于stack exchange,提问作者user2571216

