本文參考:http://www.lijiaocn.com/%E9%A1%B9%E7%9B%AE/2018/04/26/hyperledger-fabric-deploy.html 學習。
1、准備工作:
准備了兩台阿里雲ecs A,B兩台機器。:centOs7.2系統。
規划: 打算部署一個order節點,兩個peer節點。peer分屬兩個不同的組織。
A機IP orderer.example.com 7050
A機IP peer0.org1.example.com
B機IP peer0.org2.example.com
1)首先將AB兩台機器的hosts文件添加上面的內容。
2)兩台機器安裝docker:
yum install -y docker
systemctl start docker (啟動docker)
3) A機器安裝go
4)啟動peer 需要准備鏡像,AB機器上分別執行
docker pull hyperledger/fabric-javaenv:x86_64-1.1.0
docker pull hyperledger/fabric-ccenv:x86_64-1.1.0
docker pull hyperledger/fabric-baseos:x86_64-0.4.6\
2:下載相關源碼
1.創建文件夾 fabric-deploy 存放源碼。
2.cd fabric-deploy
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.1.0/hyperledger-fabric-linux-amd64-1.1.0.tar.gz
解壓 tar -xvf hyperledger-fabric-linux-amd64-1.1.0.tar.gz
解壓完有 bin 和config兩個文件夾
$ ls bin/
configtxgen configtxlator cryptogen get-byfn.sh get-docker-images.sh orderer peer
$ ls config/
configtx.yaml core.yaml orderer.yaml
3.准備證書
3.准備證書
證書的作用是確保每個節點,成員都是受認可的。證書的生成方式有兩種:一種用cryptogen
命令生成,一種是通過fabric-ca服務
生成。因為源碼自帶的cyptogen,且這種方法比較簡單,這里介紹cyptogen方式生成證書。
1.首先創建創建一個證書的配置文件 crypto-config.yaml
,這里配置了兩個組織,org1的Count是2,表示兩個peer:
OrdererOrgs:
- Name: Orderer
Domain: example.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.example.com 組織名稱
Template:
Count: 1 peer節點數量
Users:
Count: 1 用戶數量
- Name: Org2
Domain: org2.example.com
Template:
Count: 1
Users:
Count: 1
執行cyptogen 生成對應數量的證書:
./bin/cryptogen generate --config=crypto-config.yaml --output ./certs
cert目錄項生成兩個證書
$ ls ./certs/ ordererOrganizations peerOrganizations
證書文件說明:
這里目錄中的內容是用於orderer.example.com的,里面有兩個子目錄tls
和msp
:
$ tree certs/ordererOrganizations/example.com/orderers/orderer.example.com/ certs/ordererOrganizations/example.com/orderers/orderer.example.com/ |-- msp | |-- admincerts | | -- Admin@example.com-cert.pem | |-- cacerts | | -- ca.example.com-cert.pem | |-- keystore | | -- 16da15d400d4ca4b53d369b6d6e50a084d4354998c3b4d7a0934635d3907f90f_sk | |-- signcerts | | -- orderer.example.com-cert.pem | -- tlscacerts | -- tlsca.example.com-cert.pem -- tls |-- ca.crt |-- server.crt -- server.key
tls目錄中的內容很好理解,它是order對外服務時使用的私鑰(server.key)和證書(server.crt),ca.crt是簽注這個證書的CA,需要提供給發起請求的一端。
msp中有五個目錄,對區塊鏈進行操作時需要使用這些文件。
msp/admincerts
中存放的是用戶證書,使用該證書的用戶對orderer.example.com具有管理權限。
msp/cacerts
是簽署msp/signcerts
中用戶證書的ca,可以用來校驗用戶的證書:
$ cd ./certs/ordererOrganizations/example.com/orderers/orderer.example.com/msp/ $ openssl verify -CAfile ./cacerts/ca.example.com-cert.pem admincerts/Admin\@example.com-cert.pem admincerts/Admin@example.com-cert.pem: OK
msp/keystore
是orderer.example.com操作區塊,進行簽署時使用的的私鑰。
msp/signcerts
是orderer.example.com提供其它組件用來核實它的簽署的公鑰。
msp/tlscacerts
文件與tls/ca.crt
相同。
這里需要特別提到的是msp/admincerts
,在使用fabric時,你可能會發現有些操作需要admin權限,例如在某個peer上安裝合約。
那么管理員是如何認定的?就是看當前用戶的證書是不是在msp/admincerts
目錄中。
這個目錄中的內容目前是(版本1.1.x)啟動時加載的,因此如果在里面添加或刪除文件后,需要重啟使用到它的組件。
在ordererOrganizations/example.com/
還有其它幾個目錄:
ca msp orderers tlsca users
orderers中存放就簽名分析的每個orderer組件的證書文件。
users中存放的用戶的證書文件,與orderer.example.com
中內容基本相同,不過tls目錄中文件名變成了client.X
:
$ tree ordererOrganizations/example.com/users ordererOrganizations/example.com/users -- Admin@example.com |-- msp | |-- admincerts | | -- Admin@example.com-cert.pem | |-- cacerts | | -- ca.example.com-cert.pem | |-- keystore | | -- 1ac3b40c9ddda7e7a0f724b18faa0ce6fdf3f9e9ff5eac59e1e3f9739499ac2d_sk | |-- signcerts | | -- Admin@example.com-cert.pem | -- tlscacerts | -- tlsca.example.com-cert.pem -- tls |-- ca.crt |-- client.crt -- client.key
certs/peerOrganizations
中的內容與certs/ordererOrganizations
中也基本相同,只不過它里面存放的是peer要使用的證書文件。
certs目錄中的文件留着備用。
2.部署文件准備
每一個peer節點都有對應的文件,為了方便,這里先在fabric-deploy下面創建一個order.example.com 的文件夾
orderer.example.com
mkdir orderer.example.com
然后把所需要的文件放進去,后面在放到對應的服務器中啟動節點。另外兩個peer節點做同樣的操作。
1、先將order需要的證書放進去:cp bin/orderer orderer.example.com/
2.在創建order的配置文件:orderer.example.com/orderer.yaml
:
General:
LedgerType: file
ListenAddress: 0.0.0.0
ListenPort: 7050 order對應的端口號
TLS:
Enabled: true
PrivateKey: ./tls/server.key
Certificate: ./tls/server.crt
RootCAs:
- ./tls/ca.crt
# ClientAuthEnabled: false
# ClientRootCAs:
LogLevel: debug
LogFormat: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
# GenesisMethod: provisional
GenesisMethod: file
GenesisProfile: SampleInsecureSolo
GenesisFile: ./genesisblock
LocalMSPDir: ./msp
LocalMSPID: OrdererMSP
Profile:
Enabled: false
Address: 0.0.0.0:6060
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
KeyStore:
FileLedger:
Location: /opt/app/fabric/orderer/data order數據文件安裝的位置
Prefix: hyperledger-fabric-ordererledger
RAMLedger:
HistorySize: 1000
Kafka:
Retry:
ShortInterval: 5s
ShortTotal: 10m
LongInterval: 5m
LongTotal: 12h
NetworkTimeouts:
DialTimeout: 10s
ReadTimeout: 10s
WriteTimeout: 10s
Metadata:
RetryBackoff: 250ms
RetryMax: 3
Producer:
RetryBackoff: 100ms
RetryMax: 3
Consumer:
RetryBackoff: 2s
Verbose: false
TLS:
Enabled: false
PrivateKey:
#File: path/to/PrivateKey
Certificate:
#File: path/to/Certificate
RootCAs:
#File: path/to/RootCAs
Version:
注意,orderer將被部署在目標機器的/opt/app/fabric/orderer
目錄中,如果要部署在其它目錄中,需要修改配置文件中路徑。
這里需要用到一個data目錄,存放orderer的數據:
mkdir orderer.example.com/data
peer0.org1.example.com
peer 配置跟order相似:
mkdir peer0.org1.example.com
1.復制證書文件:
先將bin/peer以及證書復制到peer0.org1.example.com目錄中。
cp bin/peer peer0.org1.example.com/
cp -rf certs/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/* peer0.org1.example.com/
2.添加配置文件 core.yaml
logging:
peer: debug
cauthdsl: warning
gossip: warning
ledger: info
msp: warning
policies: warning
grpc: error
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
peer:
id: peer0.org1.example.com
networkId: dev
listenAddress: 0.0.0.0:7051
address: 0.0.0.0:7051
addressAutoDetect: false
gomaxprocs: -1
gossip:
bootstrap: 127.0.0.1:7051
bootstrap: peer0.org1.example.com:7051
useLeaderElection: true
orgLeader: false
endpoint:
maxBlockCountToStore: 100
maxPropagationBurstLatency: 10ms
maxPropagationBurstSize: 10
propagateIterations: 1
propagatePeerNum: 3
pullInterval: 4s
pullPeerNum: 3
requestStateInfoInterval: 4s
publishStateInfoInterval: 4s
stateInfoRetentionInterval:
publishCertPeriod: 10s
skipBlockVerification: false
dialTimeout: 3s
connTimeout: 2s
recvBuffSize: 20
sendBuffSize: 200
digestWaitTime: 1s
requestWaitTime: 1s
responseWaitTime: 2s
aliveTimeInterval: 5s
aliveExpirationTimeout: 25s
reconnectInterval: 25s
externalEndpoint: peer0.org1.example.com:7051
election:
startupGracePeriod: 15s
membershipSampleInterval: 1s
leaderAliveThreshold: 10s
leaderElectionDuration: 5s
events:
address: 0.0.0.0:7053
buffersize: 100
timeout: 10ms
tls:
enabled: true
cert:
file: ./tls/server.crt
key:
file: ./tls/server.key
rootcert:
file: ./tls/ca.crt
serverhostoverride:
fileSystemPath: /opt/app/fabric/peer/data
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
KeyStore:
mspConfigPath: msp
localMspId: Org1MSP
profile:
enabled: true
listenAddress: 0.0.0.0:6060
vm:
endpoint: unix:///var/run/docker.sock
docker:
tls:
enabled: false
ca:
file: docker/ca.crt
cert:
file: docker/tls.crt
key:
file: docker/tls.key
attachStdout: false
hostConfig:
NetworkMode: host
Dns:
# - 192.168.0.1
LogConfig:
Type: json-file
Config:
max-size: "50m"
max-file: "5"
Memory: 2147483648
chaincode:
peerAddress:
id:
path:
name:
builder: $(DOCKER_NS)/fabric-ccenv:$(ARCH)-$(PROJECT_VERSION)
golang:
runtime: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-$(BASE_VERSION)
car:
runtime: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-$(BASE_VERSION)
java:
Dockerfile: |
from $(DOCKER_NS)/fabric-javaenv:$(ARCH)-$(PROJECT_VERSION)
startuptimeout: 300s
executetimeout: 30s
mode: net
keepalive: 0
system:
cscc: enable
lscc: enable
escc: enable
vscc: enable
qscc: enable
logging:
level: info
shim: warning
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
ledger:
blockchain:
state:
stateDatabase: goleveldb
couchDBConfig:
couchDBAddress: 127.0.0.1:5984
username:
password:
maxRetries: 3
maxRetriesOnStartup: 10
requestTimeout: 35s
queryLimit: 10000
history:
enableHistoryDatabase: true
注意,peer將被部署在目標機器的/opt/apt/fabric/peer
目錄中,如果要部署在其它目錄中,需要修改配置文件中路徑。
這里需要用到一個data目錄,存放peer的數據:
mkdir peer0.org1.example.com/data
peer0.org2.example.com
跟peer0.org1.example.com 相同。、
4.開始部署
拷貝 orderer.example.com/到A機的
/opt/app/fabric/orderer/
拷貝 peer0.org1.example.com/到A機的
/opt/app/fabric/peer/
拷貝 peer0.org2.example.com/到B機的
/opt/app/fabric/peer/
到這里 啟動需要的文件基本准備完畢。查看 orderer.yaml 可以看到:
GenesisMethod: file
GenesisFile: ./genesisblock
GenesisProfile: SampleInsecureSolo 這幾行代碼。前兩行配置了創世區塊的獲取方式,第一個區塊的獲取方式有多種,這里采用最簡單的一種做法,用configtxgen生成。
創建一個 生成創世區塊的需要的配置文件configtx.yaml:
Profiles: TwoOrgsOrdererGenesis: Orderer: <<: *OrdererDefaults Organizations: - *OrdererOrg Consortiums: SampleConsortium: Organizations: - *Org1 - *Org2 TwoOrgsChannel: Consortium: SampleConsortium Application: <<: *ApplicationDefaults Organizations: - *Org1 - *Org2 Organizations: - &OrdererOrg Name: OrdererOrg ID: OrdererMSP MSPDir: ./certs/ordererOrganizations/example.com/msp - &Org1 Name: Org1MSP ID: Org1MSP MSPDir: ./certs/peerOrganizations/org1.example.com/msp AnchorPeers: - Host: peer0.org1.example.com Port: 7051 - &Org2 Name: Org2MSP ID: Org2MSP MSPDir: ./certs/peerOrganizations/org2.example.com/msp AnchorPeers: - Host: peer0.org2.example.com Port: 7051 Orderer: &OrdererDefaults OrdererType: solo Addresses: - orderer.example.com:7050 BatchTimeout: 2s BatchSize: MaxMessageCount: 10 AbsoluteMaxBytes: 99 MB PreferredMaxBytes: 512 KB Kafka: Brokers: - 127.0.0.1:9092 Organizations: Application: &ApplicationDefaults Organizations:
使用:
./bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./genesisblock 生成創世區塊。 將生成的
genesisblock文件放到 A機器order節點下。