美文网首页
Docker Swarm

Docker Swarm

作者: 旭日丶丶 | 来源:发表于2021-02-05 16:22 被阅读0次

    Swarm简介

    简单说 Swarm就是实时管理部署多个docker container(可在不同机器上), 可单独启动和停止某个conntainer, 并可实时重新加载环境变量, docker-compose的配置的变量等

    • 注意:
      • Linux下可以实现管理多节点
      • Windows和Mac OS下只能管理单节点
      • Manager是管理节点, 主节点, node是被管理节点

    Swarm Manager

    • 启动 Manager 节点
      docker swarm init --advertise-addr <MANAGER-IP>
      其中单节点swarm无需输入--advertise-addr
      输出结果:
    $ docker swarm init --advertise-addr 192.168.99.100
    Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.
    
    To add a worker to this swarm, run the following command:
    
        docker swarm join \
        --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
        192.168.99.100:2377
    
    To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
    
    • docker info 显示swarm信息
    $ docker info
    
    Containers: 2
    Running: 0
    Paused: 0
    Stopped: 2
      ...snip...
    Swarm: active
      NodeID: dxn1zf6l61qsb1josjja83ngz
      Is Manager: true
      Managers: 1
      Nodes: 1
      ...snip...
    
    • docker node ls 显示节点信息:
    $ docker node ls
    
    ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
    dxn1zf6l61qsb1josjja83ngz *  manager1  Ready   Active        Leader
    
    

    在另一台机器上加入node节点

    • docker swarm join-token worker 获取加入节点命令
    $ docker swarm join-token worker
    
    To add a worker to this swarm, run the following command:
    
        docker swarm join \
        --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
        192.168.99.100:2377
    
    • docker swarm join 新增node节点
    $ docker swarm join \
      --token  SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
      192.168.99.100:2377
    
    This node joined a swarm as a worker.
    
    • docker node ls 显示已加入节点信息:
    ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
    03g1y59jwfg7cf99w4lt0f662    worker2   Ready   Active
    9j68exjopxe7wfl6yuxml7a7j    worker1   Ready   Active
    dxn1zf6l61qsb1josjja83ngz *  manager1  Ready   Active        Leader
    

    在节点上启动docker service(container)

    • docker service create --replicas 1 --name container_name image_name:tag cmd
    $ docker service create --replicas 1 --name helloworld alpine ping docker.com
    
    9uk4639qpg7npwf3fn2aasksr
    

    replicas: 启动container数量
    上面命令会在当前机器节点启动helloworld container, 在conntainer里运行ping命令

    • docker service ls 列出service状态:
    $ docker service ls
    
    ID            NAME        SCALE  IMAGE   COMMAND
    9uk4639qpg7n  helloworld  1/1    alpine  ping docker.com
    

    在Manager节点上管理Node节点:

    • docker service inspect --pretty <SERVICE-ID> 查看node节点启动信息:
    [manager1]$ docker service inspect --pretty helloworld
    
    ID:     9uk4639qpg7npwf3fn2aasksr
    Name:       helloworld
    Service Mode:   REPLICATED
     Replicas:      1
    Placement:
    UpdateConfig:
     Parallelism:   1
    ContainerSpec:
     Image:     alpine
     Args:  ping docker.com
    Resources:
    Endpoint Mode:  vip
    
    • docker service ps <SERVICE-ID> 显示node节点container信息
    [manager1]$ docker service ps helloworld
    
    NAME                                    IMAGE   NODE     DESIRED STATE  CURRENT STATE           ERROR               PORTS
    helloworld.1.8p1vev3fq5zm0mi8g0as41w35  alpine  worker2  Running        Running 3 minutes
    
    • docker service rm helloworld 删除节点container
    • docker service update --image image_name:tag container_name 更新节点container, 应用新的所有配置, 环境变量等
    $ docker service update --image redis:3.0.7 redis
    redis
    

    相关文章

      网友评论

          本文标题:Docker Swarm

          本文链接:https://www.haomeiwen.com/subject/qfcgtltx.html