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的更深層次的旅程。


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