有没有更好的方法在Docker swarm上运行命令或shell

时间:2021-11-21 01:04:47

Lets say I want to edit a config file for an NGINX Docker service that is replicated across 3 nodes.

假设我想编辑一个跨3个节点复制的NGINX Docker服务的配置文件。

Currently I list the services using docker service ls.

目前我使用docker service ls列出服务。

Then get the details to find a node running a container for that service using docker serivce ps servicename.

然后获取详细信息以使用docker serivce ps servicename查找运行该服务容器的节点。

Then ssh to a node where one of the containers is running.

然后ssh到其中一个容器正在运行的节点。

Finally, docker exec -it containername bash. Then I edit the config file.

最后,docker exec -it containername bash。然后我编辑配置文件。

Two questions:

  1. Is there a better way to do this rather than ssh to a node running a container? Maybe there is a swarm or service command to do so?
  2. 有没有更好的方法来执行此操作而不是ssh到运行容器的节点?也许有一个群或服务命令这样做?

  3. If I were to edit that config file on one container would that change be replicated to the other 2 containers in the swarm?
  4. 如果我要在一个容器上编辑该配置文件,那么该更改是否会被复制到swarm中的其他2个容器?

The purpose of this exercise would be to edit configuration without shutting down a service.

此练习的目的是在不关闭服务的情况下编辑配置。

1 个解决方案

#1


0  

You should not be exec'ing into containers to change their configuration, and so docker has not created an easy way to do this within Swarm Mode. You could use classic swarm to avoid the need to ssh into the other host, but I still don't recommend this.

您不应该执行容器来更改其配置,因此docker还没有创建一种在Swarm模式下执行此操作的简单方法。您可以使用经典swarm来避免ssh到其他主机,但我仍然不建议这样做。

The correct way to do this is to migrate your configuration file into a docker config entry. Version your config name. Then when you want to update it, you create a new version with the desired changes, and do a rolling update of your service to use that new configuration.

执行此操作的正确方法是将配置文件迁移到docker配置条目。版本化您的配置名称。然后,当您想要更新它时,可以创建具有所需更改的新版本,并对服务进行滚动更新以使用该新配置。

Unless the config is mounted from an external source like NFS, changes to one config in one container will not apply to other containers running on other nodes. If that config is stored locally inside your container as part of it's internal copy-on-write filesystem, then no changes from one container will be visible in any other container.

除非从NFS等外部源安装配置,否则对一个容器中的一个配置的更改将不适用于在其他节点上运行的其他容器。如果该配置作为其内部写时复制文件系统的一部分存储在容器内部,那么任何其他容器中都不会显示来自一个容器的更改。

#1


0  

You should not be exec'ing into containers to change their configuration, and so docker has not created an easy way to do this within Swarm Mode. You could use classic swarm to avoid the need to ssh into the other host, but I still don't recommend this.

您不应该执行容器来更改其配置,因此docker还没有创建一种在Swarm模式下执行此操作的简单方法。您可以使用经典swarm来避免ssh到其他主机,但我仍然不建议这样做。

The correct way to do this is to migrate your configuration file into a docker config entry. Version your config name. Then when you want to update it, you create a new version with the desired changes, and do a rolling update of your service to use that new configuration.

执行此操作的正确方法是将配置文件迁移到docker配置条目。版本化您的配置名称。然后,当您想要更新它时,可以创建具有所需更改的新版本,并对服务进行滚动更新以使用该新配置。

Unless the config is mounted from an external source like NFS, changes to one config in one container will not apply to other containers running on other nodes. If that config is stored locally inside your container as part of it's internal copy-on-write filesystem, then no changes from one container will be visible in any other container.

除非从NFS等外部源安装配置,否则对一个容器中的一个配置的更改将不适用于在其他节点上运行的其他容器。如果该配置作为其内部写时复制文件系统的一部分存储在容器内部,那么任何其他容器中都不会显示来自一个容器的更改。