欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用Docker Compose和Swarm的多容器应用程序

程序员文章站 2022-06-03 12:08:56
...

Docker Compose to Orchestrate Containers显示了如何使用Docker Compose运行两个链接的Docker容器。 使用Docker Swarm集群化展示了如何配置Docker Swarm集群。

该博客将展示如何在Docker Swarm集群中运行使用Docker Compose创建的多容器应用程序。

Docker 1.7.0发行了更新版本的Docker Compose和Docker Swarm。

Docker 1.7.0 CLI

获取最新的Docker CLI:

curl https://get.docker.com/builds/Darwin/x86_64/docker-latest > /usr/local/bin/docker

并检查版本为:

docker -v
Docker version 1.7.0, build 0baf609

Docker机器0.3.0

获取最新的Docker机器为:

curl -L https://github.com/docker/machine/releases/download/v0.3.0/docker-machine_darwin-amd64 > /usr/local/bin/docker-machine

并检查版本为:

docker-machine -v
docker-machine version 0.3.0 (0a251fe)

Docker Compose 1.3.0

通过以下方式获取最新的Docker Compose:

curl -L https://github.com/docker/compose/releases/download/1.3.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

并将版本验证为:

docker-compose -v
docker-compose version: 1.3.0
CPython version: 2.7.9
OpenSSL version: OpenSSL 1.0.1j 15 Oct 2014

Docker Swarm 0.3.0

Swarm作为Docker容器运行,可以下载为:

docker pull swarm

您可以在docs.docker.com/swarm使用Docker Swarm的集群中了解Docker Swarm

创建Docker Swarm集群

Docker Swarm的关键组件如下所示:

使用Docker Compose和Swarm的多容器应用程序

使用Docker Swarm进行集群化中进行了解释。

  1. Swarm入门最简单的方法是使用官方的Docker映像:
    docker run swarm create

    此命令返回发现令牌,在本文档中称为<TOKEN>,并且是唯一的集群ID。 稍后在创建主节点和节点时将使用它。 该群集ID由Docker Hub上的托管发现服务返回。

    输出显示为:

    docker run swarm create
    Unable to find image 'swarm:latest' locally
    latest: Pulling from swarm
    55b38848634f: Pull complete
    fd7bc7d11a30: Pull complete
    db039e91413f: Pull complete
    1e5a49ab6458: Pull complete
    5d9ce3cdadc7: Pull complete
    1f26e949f933: Pull complete
    e08948058bed: 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:0e417fe3f7f2c7683599b94852e4308d1f426c82917223fccf4c1c4a4eddb8ef
    Status: Downloaded newer image for swarm:latest
    1d528bf0568099a452fef5c029f39b85

    最后一行是<TOKEN>。

    请确保立即记下该群集ID,因为以后无法列出它。 这应该用#661修复。

  2. Swarm已与Docker Machine完全集成,因此是最简单的入门方法。 接下来创建一个Swarm Master:
    docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery token://<TOKEN> swarm-master

    <TOKEN>替换为上一步中获得的集群ID。

    --swarm使用Swarm配置计算机,-- --swarm-master将创建的计算机配置为Swarm master。 Swarm主服务器创建与Docker Hub上的托管服务进行对话,并通知已在集群中创建了主服务器。

  3. 连接到这个新创建的母版,并找到有关它的更多信息:
    eval "$(docker-machine env swarm-master)"
    docker info

    这将输出显示为:

    > docker info
    Containers: 2
    Images: 7
    Storage Driver: aufs
     Root Dir: /mnt/sda1/var/lib/docker/aufs
     Backing Filesystem: extfs
     Dirs: 11
     Dirperm1 Supported: true
    Execution Driver: native-0.2
    Logging Driver: json-file
    Kernel Version: 4.0.5-boot2docker
    Operating System: Boot2Docker 1.7.0 (TCL 6.3); master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015
    CPUs: 1
    Total Memory: 996.2 MiB
    Name: swarm-master
    ID: DLFR:OQ3E:B5P6:HFFD:VKLI:IOLU:URNG:HML5:UHJF:6JCL:ITFH:DS6J
    Debug mode (server): true
    File Descriptors: 22
    Goroutines: 36
    System Time: 2015-07-11T00:16:34.29965306Z
    EventsListeners: 1
    Init SHA1:
    Init Path: /usr/local/bin/docker
    Docker Root Dir: /mnt/sda1/var/lib/docker
    Username: arungupta
    Registry: https://index.docker.io/v1/
    Labels:
     provider=virtualbox
  4. 创建一个Swarm节点:
    docker-machine create -d virtualbox --swarm --swarm-discovery token://<TOKEN> swarm-node-01

    <TOKEN>替换为先前步骤中获得的集群ID。

    节点创建与Docker Hub上的托管服务进行对话,并加入之前创建的集群。 这由--swarm-discovery token://...指定,并指定之前获得的集群ID。

  5. 为了使其成为一个真正的集群,让我们创建第二个节点:
    docker-machine create -d virtualbox --swarm --swarm-discovery token://<TOKEN> swarm-node-02

    <TOKEN>替换为上一步中获得的集群ID。

  6. 列出到目前为止创建的所有节点:
    docker-machine ls

    显示的输出类似于以下内容:

    docker-machine ls
    NAME            ACTIVE   DRIVER       STATE     URL                         SWARM
    lab                      virtualbox   Running   tcp://192.168.99.101:2376
    summit2015               virtualbox   Running   tcp://192.168.99.100:2376
    swarm-master    *        virtualbox   Running   tcp://192.168.99.102:2376   swarm-master (master)
    swarm-node-01            virtualbox   Running   tcp://192.168.99.103:2376   swarm-master
    swarm-node-02            virtualbox   Running   tcp://192.168.99.104:2376   swarm-master

    属于群集的计算机在“群集”列中具有群集的名称,否则为空白。 例如,“ lab”和“ summit2015”是独立的计算机,其中所有其他计算机都是“ swarm-master”集群的一部分。 Swarm主机也由SWARM列中的(主机)标识。

  7. 连接到Swarm集群并找到有关它的一些信息:
    eval "$(docker-machine env --swarm swarm-master)"
    docker info

    输出显示为:

    > docker info
    Containers: 4
    Images: 3
    Role: primary
    Strategy: spread
    Filters: affinity, health, constraint, port, dependency
    Nodes: 3
     swarm-master: 192.168.99.102:2376
      └ Containers: 2
      └ Reserved CPUs: 0 / 1
      └ Reserved Memory: 0 B / 1.022 GiB
      └ Labels: executiondriver=native-0.2, kernelversion=4.0.5-boot2docker, operatingsystem=Boot2Docker 1.7.0 (TCL 6.3); master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015, provider=virtualbox, storagedriver=aufs
     swarm-node-01: 192.168.99.103:2376
      └ Containers: 1
      └ Reserved CPUs: 0 / 1
      └ Reserved Memory: 0 B / 1.022 GiB
      └ Labels: executiondriver=native-0.2, kernelversion=4.0.5-boot2docker, operatingsystem=Boot2Docker 1.7.0 (TCL 6.3); master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015, provider=virtualbox, storagedriver=aufs
     swarm-node-02: 192.168.99.104:2376
      └ Containers: 1
      └ Reserved CPUs: 0 / 1
      └ Reserved Memory: 0 B / 1.022 GiB
      └ Labels: executiondriver=native-0.2, kernelversion=4.0.5-boot2docker, operatingsystem=Boot2Docker 1.7.0 (TCL 6.3); master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015, provider=virtualbox, storagedriver=aufs
    CPUs: 3
    Total Memory: 3.065 GiB

    有3个节点-1个Swarm主节点和2个Swarm节点。 此群集中总共有4个容器在运行–主节点和每个节点上有一个Swarm代理,主节点上还有一个额外的swarm-agent-master。

  8. 使用以下命令列出集群中的节点:
    docker run swarm list token://<TOKEN>

    输出显示为:

    > docker run swarm list token://1d528bf0568099a452fef5c029f39b85
    192.168.99.103:2376
    192.168.99.104:2376
    192.168.99.102:2376

使用Docker Compose将Java EE应用程序部署到Docker Swarm集群

Docker Compose to Orchestrate Containers解释了如何使用Docker Compose轻松启动多容器应用程序。

  1. 使用该博客中解释docker-compose.yml文件以如下方式启动容器:
    docker-compose up -d
    Creating wildflymysqljavaee7_mysqldb_1...
    Creating wildflymysqljavaee7_mywildfly_1...

    docker-compose.yml文件如下所示:

    mysqldb:
      image: mysql:latest
      environment:
        MYSQL_DATABASE: sample
        MYSQL_USER: mysql
        MYSQL_PASSWORD: mysql
        MYSQL_ROOT_PASSWORD: supersecret
    mywildfly:
      image: arungupta/wildfly-mysql-javaee7
      links:
        - mysqldb:db
      ports:
        - 8080:8080
  2. 检查集群中运行的容器,如下所示:
    eval "$(docker-machine env --swarm swarm-master)"
    docker info

    看到的输出为:

    docker info
    Containers: 7
    Images: 5
    Role: primary
    Strategy: spread
    Filters: affinity, health, constraint, port, dependency
    Nodes: 3
     swarm-master: 192.168.99.102:2376
      └ Containers: 2
      └ Reserved CPUs: 0 / 1
      └ Reserved Memory: 0 B / 1.022 GiB
      └ Labels: executiondriver=native-0.2, kernelversion=4.0.5-boot2docker, operatingsystem=Boot2Docker 1.7.0 (TCL 6.3); master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015, provider=virtualbox, storagedriver=aufs
     swarm-node-01: 192.168.99.103:2376
      └ Containers: 2
      └ Reserved CPUs: 0 / 1
      └ Reserved Memory: 0 B / 1.022 GiB
      └ Labels: executiondriver=native-0.2, kernelversion=4.0.5-boot2docker, operatingsystem=Boot2Docker 1.7.0 (TCL 6.3); master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015, provider=virtualbox, storagedriver=aufs
     swarm-node-02: 192.168.99.104:2376
      └ Containers: 3
      └ Reserved CPUs: 0 / 1
      └ Reserved Memory: 0 B / 1.022 GiB
      └ Labels: executiondriver=native-0.2, kernelversion=4.0.5-boot2docker, operatingsystem=Boot2Docker 1.7.0 (TCL 6.3); master : 7960f90 - Thu Jun 18 18:31:45 UTC 2015, provider=virtualbox, storagedriver=aufs
    CPUs: 3
    Total Memory: 3.065 GiB
  3. “ swarm-node-02”正在运​​行三个容器,因此让我们看一下在那里运行的容器列表:
    eval "$(docker-machine env swarm-node-02)"

    并查看正在运行的容器的列表,如下所示:

    docker ps -a
    CONTAINER ID        IMAGE                             COMMAND                CREATED             STATUS              PORTS                    NAMES
    b1e7d9bd2c09        arungupta/wildfly-mysql-javaee7   "/opt/jboss/wildfly/   38 seconds ago      Up 37 seconds       0.0.0.0:8080->8080/tcp   wildflymysqljavaee7_mywildfly_1
    ac9c967e4b1d        mysql:latest                      "/entrypoint.sh mysq   38 seconds ago      Up 38 seconds       3306/tcp                 wildflymysqljavaee7_mysqldb_1
    45b015bc79f4        swarm:latest                      "/swarm join --addr    20 minutes ago      Up 20 minutes       2375/tcp                 swarm-agent
  4. 然后可以使用以下命令再次访问该应用程序:
    curl http://$(docker-machine ip swarm-node-02):8080/employees/resources/employees

    并将输出显示为:

    <?<span class="pl-ent">xml</span><span class="pl-e"> version</span>=<span class="pl-s"><span class="pl-pds">"</span>1.0<span class="pl-pds">"</span></span><span class="pl-e"> encoding</span>=<span class="pl-s"><span class="pl-pds">"</span>UTF-8<span class="pl-pds">"</span></span><span class="pl-e"> standalone</span>=<span class="pl-s"><span class="pl-pds">"</span>yes<span class="pl-pds">"</span></span>?><<span class="pl-ent">collection</span>><<span class="pl-ent">employee</span>><<span class="pl-ent">id</span>>1</<span class="pl-ent">id</span>><<span class="pl-ent">name</span>>Penny</<span class="pl-ent">name</span>></<span class="pl-ent">employee</span>><<span class="pl-ent">employee</span>><<span class="pl-ent">id</span>>2</<span class="pl-ent">id</span>><<span class="pl-ent">name</span>>Sheldon</<span class="pl-ent">name</span>></<span class="pl-ent">employee</span>><<span class="pl-ent">employee</span>><<span class="pl-ent">id</span>>3</<span class="pl-ent">id</span>><<span class="pl-ent">name</span>>Amy</<span class="pl-ent">name</span>></<span class="pl-ent">employee</span>><<span class="pl-ent">employee</span>><<span class="pl-ent">id</span>>4</<span class="pl-ent">id</span>><<span class="pl-ent">name</span>>Leonard</<span class="pl-ent">name</span>></<span class="pl-ent">employee</span>><<span class="pl-ent">employee</span>><<span class="pl-ent">id</span>>5</<span class="pl-ent">id</span>><<span class="pl-ent">name</span>>Bernadette</<span class="pl-ent">name</span>></<span class="pl-ent">employee</span>><<span class="pl-ent">employee</span>><<span class="pl-ent">id</span>>6</<span class="pl-ent">id</span>><<span class="pl-ent">name</span>>Raj</<span class="pl-ent">name</span>></<span class="pl-ent">employee</span>><<span class="pl-ent">employee</span>><<span class="pl-ent">id</span>>7</<span class="pl-ent">id</span>><<span class="pl-ent">name</span>>Howard</<span class="pl-ent">name</span>></<span class="pl-ent">employee</span>><<span class="pl-ent">employee</span>><<span class="pl-ent">id</span>>8</<span class="pl-ent">id</span>><<span class="pl-ent">name</span>>Priya</<span class="pl-ent">name</span>></<span class="pl-ent">employee</span>></<span class="pl-ent">collection</span>>

请享用!

翻译自: https://www.javacodegeeks.com/2015/07/multi-container-applications-using-docker-compose-and-swarm.html