创建 Docker Swarm 集群

初始化集群(在第一个管理节点操作)

docker swarm init --advertise-addr <first_manager_node_ip>

部分输出信息:

Swarm initialized: current node (mo2p7vnjhzg68g6wzejozdomo) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-260gjixktxw5jnso7osrpuhsvqi33m2ksro78doxlqanug9kix-141p1sm3ugjsw4qh6o0ydp2ho <first_manager_node_ip>:2377    ## 此命令添加worker节点

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.    ## 此方法可获取添加备用manager节点的命令,生产环境必须至少三个manager节点

后面可以使用以下命令获取加入其他节点的命令:

docker swarm join-token manager
docker swarm join-token worker

集群节点之间打开协议和端口说明:

TCP port 2377 for cluster management communications
TCP and UDP port 7946 for communication among nodes
UDP port 4789 for overlay network traffic

查看集群状态:

docker info
docker node ls

部署一个服务(必须在管理节点操作)

部署命令:

# docker service create --replicas 1 --name helloworld alpine ping docker.com

命令说明:

The docker service create command creates the service.
The --name flag names the service helloworld.
The --replicas flag specifies the desired state of 1 running instance.
The arguments alpine ping docker.com define the service as an Alpine Linux container that executes the command ping docker.com.

查看服务:

# docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
9wquw4g6bt29        helloworld          replicated          1/1                 alpine:latest

# docker service inspect --pretty helloworld

ID:             9wquw4g6bt29yl213t4pg9spq
Name:           helloworld
Service Mode:   Replicated
 Replicas:      1
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:         alpine:latest@sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0
 Args:          ping docker.com 
Resources:
Endpoint Mode:  vip

# docker service ps helloworld

ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
f5tbcczzpy40        helloworld.1        alpine:latest       mysqlmaster         Running             Running 34 minutes ago

扩容服务:

# docker service scale helloworld=3

helloworld scaled to 3
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged

# docker service ps helloworld

ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
f5tbcczzpy40        helloworld.1        alpine:latest       mysqlmaster         Running             Running 40 minutes ago                       
pbx26pn67shp        helloworld.2        alpine:latest       mysqlmaster         Running             Running 20 seconds ago                       
d7k1txanj0d2        helloworld.3        alpine:latest       mysqlslave          Running             Running 20 seconds ago

删除服务:

# docker service rm helloworld
posted @ 2021-08-20 13:56  Varden  阅读(95)  评论(0编辑  收藏  举报