- Published on
docker swarm使用
- Authors
- Name
- JiGu
- @crypto20x
注意事项
- service 名字不能存在 "."等符号
- volume 的目录需要每个节点上都存在
- compose文件只支持v3版本
- 同一个swarm网络中的端口不能冲突,各个副本暴露的端口各个节点都是对外暴露的(nginx 在 节点1,2,3开放80端口,那么节点1,2,3的80端口都指向nginx这个服务)
1. 创建docker网络 用于swarm
#在主机上创建叠加网络
docker network create --attachable --driver overlay first-network
2. 创建swarm集群
- 在 node1 的机器上执行
# 初始化swarm 将 node1 作为master
docker swarm init --advertise-addr <当前机器ip地址>
# 获取其他节点加入的token
docker swarm join-token manager
终端会打印加入该swarm网络的命令,包含了token信息,复制在其他节点上执行
3. 部署服务
cli直接部署示例
# 创建副本数为3 端口为80的 nginx服务
docker service create --replicas 3 -p 80:80 --name nginx nginx:1.13.7-alpine
docker stack部署 示例
yaml 文件
version: '3'
networks:
byfn:
external:
name: first-network
services:
orderer1:
hostname: orderer1.3hmis.com
image: hyperledger/fabric-orderer:$IMAGE_TAG
environment:
- FABRIC_LOGGING_SPEC=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
- GODEBUG=netdns=go
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
# - ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1
# - ORDERER_KAFKA_VERBOSE=true
- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
deploy:
replicas: 1
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
placement:
constraints: [node.role == manager]
volumes:
- /root/fabric_base_new/channel-artifacts/orderer.genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- /root/fabric_base_new/crypto-config/ordererOrganizations/3hmis.com/orderers/orderer1.3hmis.com/msp:/var/hyperledger/orderer/msp
- /root/fabric_base_new/crypto-config/ordererOrganizations/3hmis.com/orderers/orderer1.3hmis.com/tls/:/var/hyperledger/orderer/tls
# - /udata/fabricdata/orderer1:/var/hyperledger/production/orderer
# - orderer1.3hmis.com:/var/hyperledger/production/orderer
ports:
- 7050:7050
command: orderer
container_name: orderer1.3hmis.com
networks:
byfn:
aliases:
- orderer1.3hmis.com
orderer2:
hostname: orderer2.3hmis.com
image: hyperledger/fabric-orderer:$IMAGE_TAG
environment:
- FABRIC_LOGGING_SPEC=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
- GODEBUG=netdns=go
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
# - ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1
# - ORDERER_KAFKA_VERBOSE=true
- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
deploy:
replicas: 1
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
placement:
constraints: [node.role == manager]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
volumes:
- /root/fabric_base_new/channel-artifacts/orderer.genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- /root/fabric_base_new/crypto-config/ordererOrganizations/3hmis.com/orderers/orderer2.3hmis.com/msp:/var/hyperledger/orderer/msp
- /root/fabric_base_new/crypto-config/ordererOrganizations/3hmis.com/orderers/orderer2.3hmis.com/tls/:/var/hyperledger/orderer/tls
# - /udata/fabricdata/orderer2:/var/hyperledger/production/orderer
# - orderer2.3hmis.com:/var/hyperledger/production/orderer
ports:
- 8050:7050
command: orderer
container_name: orderer2.3hmis.com
networks:
byfn:
aliases:
- orderer2.3hmis.com
orderer3:
hostname: orderer3.3hmis.com
image: hyperledger/fabric-orderer:$IMAGE_TAG
environment:
- FABRIC_LOGGING_SPEC=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
- GODEBUG=netdns=go
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
# - ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1
# - ORDERER_KAFKA_VERBOSE=true
- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
deploy:
replicas: 1
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
placement:
constraints: [node.role == manager]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
volumes:
- /root/fabric_base_new/channel-artifacts/orderer.genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- /root/fabric_base_new/crypto-config/ordererOrganizations/3hmis.com/orderers/orderer3.3hmis.com/msp:/var/hyperledger/orderer/msp
- /root/fabric_base_new/crypto-config/ordererOrganizations/3hmis.com/orderers/orderer3.3hmis.com/tls/:/var/hyperledger/orderer/tls
# - /udata/fabricdata/orderer3:/var/hyperledger/production/orderer
# - orderer3.3hmis.com:/var/hyperledger/production/orderer
ports:
- 9050:7050
command: orderer
container_name: orderer3.3hmis.com
networks:
byfn:
aliases:
- orderer3.3hmis.com
部署命令
# cmd
docker stack deploy -c swarm-compose/orderers-compose.yaml orderers