编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

Docker Swarm部署-等待服务/容器出现:Docker Swarm deploy - wait for service/container to be present

ujos 云计算 2022-5-10 14:49 11人围观

腾讯云服务器
Docker Swarm部署-等待服务/容器出现的处理方法

我有一个有效的工作组设置和滚动更新部署.由于部署后我必须执行一些任务(例如数据库迁移),因此我向堆栈添加了管理器"服务.该服务仅限于节点管理器-因此,我始终可以找到它.

I have a working swarm setup and rolling-updates deployment. As i have to execute some tasks after deployment (like database migrations) i added a "manager" service to the stack. this service is limited to the node-manager - so i always have a way to find it.

要获取当前的容器ID,请使用以下命令: export MANAGER_ --tls ps --filter label = com.docker.swarm.service.name = projectname-php-manager -q)

To get the current containerID i use this command: export MANAGER_ --tls ps --filter label=com.docker.swarm.service.name=projectname-php-manager -q)

这有效...但是在部署期间不起作用.

This works ... but not during deploy.

stack deploy 即将退出(因为容器已启动),甚至退出,直到管理器容器得到更新.我还添加了 sleep 10 ,以获取containerID,但结果有所不同.

The stack deploy exits to soon (befor the container is up) or even befor the manager container gets updated. I also added a sleep 10 befor geting the containerID but the results vary.

是否可以等待或知道何时部署特定服务?

Is there a way to wait or to know when a specific service is deployed?

完整部署看起来像这样(在gitlab-ci作业中完成-但这不是问题的根源):

The full deploy looks like this (done in a gitlab-ci job - but this is not the root of the problem):

deploy:staging: variables: DOCKER_HOST: "tcp://swarm-manager.hostname.tld:2376" DOCKER_CERT_PATH: "/home/gitlab-runner/docker/swarm-manager.hostname.tld" VERSION_TAG: "$CI_COMMIT_TAG" MYSQL_PROD_PASSWORD: "$MYSQL_PROD_PASSWORD" SECRET_TOKEN: "$SECRET_TOKEN" script: - docker --tls stack deploy -c docker-compose.prod.yml project-name --with-registry-auth --prune - sleep 10 - export MANAGER_ --tls ps --filter label=com.docker.swarm.service.name=project-name_php-manager -q) - docker --tls exec -t $MANAGER_ID bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration stage: deploy environment: name: staging url: http://projectname.com only: [tags] cache: ~ dependencies: - build:app tags: - deploy 

来自docker-compose.prod.yml的零件:

Part from docker-compose.prod.yml:

php-manager: image: dockerhub.mydomain.tld/namespace/projectname/php:${VERSION_TAG} environment: DATABASE_URL: "mysql://projectname:${MYSQL_PROD_PASSWORD}@mysql:3306/projectname?charset=utf8mb4&serverVersion=5.7" APP_ENV: prod APP_SECRET: "${SECRET_TOKEN}" VERSION: "${VERSION_TAG}" REDIS_HOST: redis networks: - default deploy: placement: constraints: [node.role == manager] replicas: 1 restart_policy: condition: on-failure 

问题解答

Docker堆栈部署创建了一些任务,这些任务试图使系统达到所需的状态.有时任务成功,有时失败,协调器将生成新任务,直到系统匹配yml文件中描述的状态为止.

Docker stack deploy creates tasks which try to get the system to the state you desire. Sometimes tasks succeed, sometimes they fail and the orchestrator will generate new tasks until the system matches the state described in your yml files.

坏消息: docker stack deploy在达到所需状态之前不支持阻塞.

这里有一些如何使用docker cli和基本的bash工具(您可以肯定地以任何其他语言类似的方式实现)获取所需信息的方法

Here some how to get the info you want using the docker cli and basic bash tools (which you can surely implement in a similar way in any other language)

在bash中,您可以执行 docker service ls --format'{{.ID}} {{.Name}}'|grep $ {serviceName} 来获取服务的ServiceId(返回的两个单词中的第一个)

In bash you could do docker service ls --format '{{.ID}} {{.Name}}' | grep ${serviceName} to get the ServiceId of your service (its the first of the two words returned)

根据 docs docker service ps所做的事情:

according to the docs docker service ps does:

列出一项或多项服务的任务

List the tasks of one or more services

它还会添加有关任务当前状态"的一些信息,这是您关心的信息.

Also it adds some information about the task 'current state' which is the information you care about.

然后使用 docker service ps $ {ServiceId} --format'{{.CurrentState}} {{.Image}}'|grep正在运行.* $ {newImageName}

如果此命令返回了某些内容,则表示有一个运行着新映像的容器.欢呼:)

If this command returns something there is a container running with your new image. Hurray :)

我希望这会为您介绍所需的所有工具.Docker服务ps还有助于找出任务失败的原因.

I hope this introduces you to all the tools you need. Docker service ps is also helpfull for finding out why a task failed.

仅供参考:根据

NEW任务已初始化.

NEW The task was initialized.

PENDING已分配任务资源.

PENDING Resources for the task were allocated.

已分配Docker将任务分配给了节点.

ASSIGNED Docker assigned the task to nodes.

ACCEPTED任务已被工作程序节点接受.如果一个工作节点拒绝任务,状态变为REJECTED.

ACCEPTED The task was accepted by a worker node. If a worker node rejects the task, the state changes to REJECTED.

准备Docker正在准备任务.

PREPARING Docker is preparing the task.

启动Docker正在启动任务.

STARTING Docker is starting the task.

正在运行任务正在执行.

RUNNING The task is executing.

完成,任务已退出,没有错误代码.

COMPLETE The task exited without an error code.

FAILED任务以错误代码退出.

FAILED The task exited with an error code.

关闭Docker请求关闭任务.

SHUTDOWN Docker requested the task to shut down.

已拒绝工作节点拒绝了任务.

REJECTED The worker node rejected the task.

ORPHANED节点关闭时间过长.

ORPHANED The node was down for too long.

这篇关于Docker Swarm部署-等待服务/容器出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^