1.0.4、Docker Swarm

安装并创建一个Docker Swarm

你可以使用Docker Swarm 去集群和调度一组Docker容器。这个章节里将向你介绍Docker Swarm,教你使用Docker Machine 和 VirtualBox在本地机器上,如何创建一个属于自己的集群系统。

先决条件

确保你的本地系统已经安装VirtualBox。如果你正在使用的是Max OS X 或 Windows并安装了Docker,那么你应该也已经安装VirtualBox。根据你的系统架构类型使用适当的指令安装Machine,更多安装细节请移步到安装 Docker Machine 指南

创建一个Docker Swarm

Docker Machine 可以获取到已经运行Docker容器的主机。在你的Docker Swarm里的每个节点必须都有使用Docker去拉取镜像并基于它创建和运行容器的权限。Docker Machine 为你的集群系统管理所有这些配置。

在你使用 docker-machine 创建一个集群之前,你将每个节点和一个发现服务关联。在这个例子里使用一个基于Docker Hub的token发现服务。这个发现服务在每个节点上通过一个token去关联一个Docker Daemon的实例。其他后端发现服务,比如,etcd,consul, 和 zookeeper 都是可用的

  1. 列出你的系统上的machines。

    $ docker-machine ls
    NAME         ACTIVE   DRIVER       STATE     URL                         SWARM
    docker-vm    *        virtualbox   Running   tcp://192.168.99.100:2376   

    这个例子是运行在一个Mac OS X系统,使用Docker Toolbox的安装。所以,docker-vm 虚拟的machine 也会被列出。


  2. 在你的系统上,创建一个名为 local  VirtualBox machine。

    $ docker-machine create -d virtualbox local
    INFO[0000] Creating SSH key...                          
    INFO[0000] Creating VirtualBox VM...                    
    INFO[0005] Starting VirtualBox VM...                    
    INFO[0005] Waiting for VM to start...                   
    INFO[0050] "local" has been created and is now the active machine.
    INFO[0050] To point your Docker client at it, run this in your shell: eval "$(docker-machine env local)"
  3. 加载 local machine 的配置到你的shell中。

    $ eval "$(docker-machine env local)"
  4. 使用Docker Swarm镜像生成一个发现token(令牌)。

    下面命令运行 swarm create 命令在一个容器里。如果在你的本例机器里没有找到 swarm:latest 镜像,Docker 会自动为你拉取。

    $ docker run swarm create
    Unable to find image 'swarm:latest' locally
    latest: Pulling from swarm
    de939d6ed512: Pull complete79195899a8a4: Pull complete79ad4f2cc8e0: Pull complete0db1696be81b: Pull complete
    ae3b6728155e: Pull complete57ec2f5f3e06: Pull complete73504b2882a3: Already exists
    swarm:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
    Digest: sha256:aaaf6c18b8be01a75099cc554b4fb372b8ec677ae81764dcdf85470279a61d6f
    Status: Downloaded newer image for swarm:latest
    fe0cc96a72cf04dba8c1c4aa79536ec3
    

    swarm create 命令会返回fe0cc96a72cf04dba8c1c4aa79536ec3 token(令牌)。

  5. 保存 token 到一个安全的地方。

    在下步的创建一个Docker swarm时,你将会用到这个token。

启动集群管理器

寄存在你的网络上一个独立系统被称作Docker Swarm管理器。集群管理器编排和调度整个集群上的容器。集群管理器管管理者一组代理(也称为节点或Docker节点)。

集群代理负责托管容器。它们是普通的Docker Daemon,你可以使用Docker远程API和它们进行同行。

在这个小节里,你创建了一个集群管理器和两个节点。

  1. 在VirtualBox下,创建一个集群管理器。

    docker-machine create \
            -d virtualbox \
            --swarm \
            --swarm-master \
            --swarm-discovery token://<TOKEN-FROM-ABOVE> \
            swarm-master
    

    例如:

    $ docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-master
    INFO[0000] Creating SSH key...                          
    INFO[0000] Creating VirtualBox VM...                    
    INFO[0005] Starting VirtualBox VM...                    
    INFO[0005] Waiting for VM to start...                   
    INFO[0060] "swarm-master" has been created and is now the active machine.
    INFO[0060] To point your Docker client at it, run this in your shell: eval "$(docker-machine env swarm-master)"
  2. 打开你的VirtualBox管理器,它应该包含local 机器和新的swarm-master 机器。

  3. 创建一个集群节点。

        docker-machine create \
        -d virtualbox \
        --swarm \
        --swarm-discovery token://<TOKEN-FROM-ABOVE> \
        swarm-agent-00
    

    例如:

    $ docker-machine create -d virtualbox --swarm --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-agent-00
    INFO[0000] Creating SSH key...                          
    INFO[0000] Creating VirtualBox VM...                    
    INFO[0005] Starting VirtualBox VM...                    
    INFO[0006] Waiting for VM to start...                   
    INFO[0066] "swarm-agent-00" has been created and is now the active machine.
    INFO[0066] To point your Docker client at it, run this in your shell: eval "$(docker-machine env swarm-agent-00)"
  4. 添加名为 swarm-agent-01 的另一个代理。

    $ docker-machine create -d virtualbox --swarm --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-agent-01

    在你的VirtualBox管理器里,你现在应该可以看到两个代理。

管理你的集群

在这个小节,你连接到集群机器,查看你的集群信息并在上面启动一个镜像。

  1. 将你的Docker环境指向到运行集群master的机器。

    $ eval $(docker-machine env --swarm swarm-master)
    
  2. 在你的新集群上使用docker 命令获取信息。

    $ docker info
    Containers: 4
    Strategy: spread
    Filters: affinity, health, constraint, port, dependency
    Nodes: 3
     swarm-agent-00: 192.168.99.105:2376
        └ Containers: 1
        └ Reserved CPUs: 0 / 8
        └ Reserved Memory: 0 B / 1.023 GiB
     swarm-agent-01: 192.168.99.106:2376
        └ Containers: 1
        └ Reserved CPUs: 0 / 8
        └ Reserved Memory: 0 B / 1.023 GiB
     swarm-master: 192.168.99.104:2376
        └ Containers: 2
        └ Reserved CPUs: 0 / 8

你可以看到每个代理和master都暴露了 2376 端口。当你创建一个集群,你可以使用任何你喜欢的端口,甚至在不同的节点上使用不同的端口。每个集群节点运行集群代理容器。

The master is running both the swarm manager and a swarm agent container. This isn’t recommended in a production environment because it can cause problems with agent failover. However, it is perfectly fine to do this in a learning environment like this one.

  1. Check the images currently running on your swarm.

    $ docker ps  -a
    CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                     NAMES78be991b58d1        swarm:latest        "/swarm join --addr    3 minutes ago       Up 2 minutes        2375/tcp                                  swarm-agent-01/swarm-agent        
    da5127e4f0f9        swarm:latest        "/swarm join --addr    6 minutes ago       Up 6 minutes        2375/tcp                                  swarm-agent-00/swarm-agent        
    ef395f316c59        swarm:latest        "/swarm join --addr    16 minutes ago      Up 16 minutes       2375/tcp                                  swarm-master/swarm-agent          
    45821ca5208e        swarm:latest        "/swarm manage --tls   16 minutes ago      Up 16 minutes       2375/tcp, 192.168.99.104:3376->3376/tcp   swarm-master/swarm-agent-master   
    
  2. 运行Docker hello-world 测试镜像,在你的集群上。

    $ docker run hello-world
    Hello from Docker.
    This message shows that your installation appears to be working correctly.
    
    
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
            (Assuming it was not already locally available.)
     3. The Docker daemon created a new container from that image which runs the
            executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.
    
    
    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash
    
    
    For more examples and ideas, visit:
     http://docs.docker.com/userguide/
    
  3. 使用 docker ps 命令去查找节点已经在运行中的容器。

    $ docker ps -a
    CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS                     PORTS                                     NAMES
    54a8690043dd        hello-world:latest   "/hello"               22 seconds ago      Exited (0) 3 seconds ago                                             swarm-agent-00/modest_goodall     
    78be991b58d1        swarm:latest         "/swarm join --addr    5 minutes ago       Up 4 minutes               2375/tcp                                  swarm-agent-01/swarm-agent        
    da5127e4f0f9        swarm:latest         "/swarm join --addr    8 minutes ago       Up 8 minutes               2375/tcp                                  swarm-agent-00/swarm-agent        
    ef395f316c59        swarm:latest         "/swarm join --addr    18 minutes ago      Up 18 minutes              2375/tcp                                  swarm-master/swarm-agent          
    45821ca5208e        swarm:latest         "/swarm manage --tls   18 minutes ago      Up 18 minutes              2375/tcp, 192.168.99.104:3376->3376/tcp   swarm-master/swarm-agent-master   

下一站

此时此刻,你已经安装了Docker Swarm,通过拉取Docker Hub上的最新的镜像。之后,在你的本地机器上使用VirtualBox,构建并运行了一个集群。如果你需要,你可以阅读 Docker Swarm特性的概述 或者,你可以通过在网络上手动安装 Swarm,开启Swarm的更深层次的旅程。


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章