Docker swarm集群管理工具

1. 简介

Docker Swarm 是Docker的集群管理工具,简单方便、易于上手。


  • Mananger:负责整个集群的管理工作包括集群配置、服务管理等所有跟集群有关的工作
  • Worker:主要负责运行相应的服务来执行任务(task)

2. 部署


  • 初始化Manager
# docker swarm init --advertise-addr
Swarm initialized: current node (zm7q1dewfj6tqo4way3f497ky) is now a manager.

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

    docker swarm join --token SWMTKN-1-0671cw61lnf0xuf8gftx7pnapirf8pk20dv9rr3k9fo7kuti76-8micra51k1rolbjcx47i2vd4v

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
  • 加入Worker
# docker swarm join --token SWMTKN-1-0671cw61lnf0xuf8gftx7pnapirf8pk20dv9rr3k9fo7kuti76-8micra51k1rolbjcx47i2vd4v
This node joined a swarm as a worker.

3. 管理

可以利用docker node实现对swarm节点的管理

  • 查看帮助
# docker node --help

Usage:  docker node COMMAND

Manage Swarm nodes

  demote      Demote one or more nodes from manager in the swarm
  inspect     Display detailed information on one or more nodes
  ls          List nodes in the swarm
  promote     Promote one or more nodes to manager in the swarm
  ps          List tasks running on one or more nodes, defaults to current node
  rm          Remove one or more nodes from the swarm
  update      Update a node
  • Manager上查看node信息
# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
zm7q1dewfj6tqo4way3f497ky *   master              Ready               Active              Leader              19.03.12
eg2en2llac0g4cd17xqoweufz     node1               Ready               Active                                  18.06.3-ce

4. 服务

可以利用docker service实现对swarm服务的管理

  • 查看帮助
# docker service --help

Usage:  docker service COMMAND

Manage services

  create      Create a new service
  inspect     Display detailed information on one or more services
  logs        Fetch the logs of a service or task
  ls          List services
  ps          List the tasks of one or more services
  rm          Remove one or more services
  rollback    Revert changes to a service's configuration
  scale       Scale one or multiple replicated services
  update      Update a service
  • 创建服务
# docker service create --replicas 1 -p80:80 --name=myngx nginx
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
  • 列出服务
# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
n3q3hjn69isu        myngx               replicated          1/1                 nginx:latest        *:80->80/tcp
  • 删除服务
docker service rm myngx

5. 编排

可以利用docker stack将应用部署到swarm集群上

  • 查看帮助
# docker stack --help

Usage:  docker stack [OPTIONS] COMMAND

Manage Docker stacks

      --orchestrator string   Orchestrator to use (swarm|kubernetes|all)

  deploy      Deploy a new stack or update an existing stack
  ls          List stacks
  ps          List the tasks in the stack
  rm          Remove one or more stacks
  services    List the services in the stack
  • docker-compose.yml示例
# cat docker-compose.yml
version: '3'
    image: "redis:alpine"
  • 部署
# docker stack deploy -c docker-compose.yml redis
Creating network redis_default
Creating service redis_redis
  • 查看
# docker stack ls
NAME                SERVICES            ORCHESTRATOR
redis               1                   Swarm

# docker stack ps redis
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                    ERROR               PORTS
s6oqyfb0tgf4        redis_redis.1       redis:alpine        node1               Running             Running less than a second ago

# docker stack services redis
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
5x3zx5lkf20u        redis_redis         replicated          1/1                 redis:alpine
  • 删除
# docker stack rm redis
Removing service redis_redis
Removing network redis_default
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页