如何查看 docker-Composhealthcheck 日志?

在我的 docker-compose.yml,我有以下 service healthcheck节。我想知道 MariaDB 是否真的准备好处理查询了。名为 cmdservice被配置为依赖于 condition: service_healthy

  db:
image: mariadb:10
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_DATABASE: database
healthcheck:
test: ["CMD", "mysql", "--user=user", "--password=password", "--execute='SELECT 1'", "--host=127.0.0.1", "--port=3306"]
interval: 1s
retries: 30

这个健康检查不起作用,表明服务是不健康的。

如何检查 test CMD 的输出?

45206 次浏览

你可使用:

docker inspect --format "\{\{json .State.Health }}" <container name> | jq

产出:

{
"Status": "unhealthy",
"FailingStreak": 63,
"Log": [
{
"Start": "2017-03-11T20:49:19.668895201+03:30",
"End": "2017-03-11T20:49:19.735722044+03:30",
"ExitCode": 1,
"Output": "ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''SELECT 1'' at line 1\n"
}
]
}

然后找 输出部分。

只获取输出:

docker inspect --format "\{\{json .State.Health }}" mariadb_db_1 | jq '.Log[].Output'

对于群体模式,请使用以下格式(谢谢指出@shogunner) :

\{\{json.Spec.TaskTemplate.ContainerSpec.Healthcheck}}

随时交换 jq的任何工具,你使用的 json 漂亮打印。

docker-compose ps将指示每个服务的状态,如果定义了 healthcheck,则包括其健康状况。这是一个很好的基本概述。

% docker-compose ps
Name                                Command                       State                                       Ports
----------------------------------------------------------------------------------------------------------------------------------------------------------------
remix-theme-editor_analytics_1            /bin/sh -c /analytics/run. ...   Up
remix-theme-editor_base_1                 /bin/bash                        Exit 0
remix-theme-editor_flower_1               /entrypoint --environment  ...   Exit 137
remix-theme-editor_frontend_1             /bin/sh -c perl -p -i -e ' ...   Exit 137
remix-theme-editor_js-app_1               npm run                          Exit 0
remix-theme-editor_mq_1                   docker-entrypoint.sh rabbi ...   Up (healthy)            15671/tcp, 15672/tcp, 25672/tcp, 4369/tcp, 5671/tcp, 5672/tcp
remix-theme-editor_mysql-migration_1      /entrypoint_mysql-migratio ...   Exit 0
remix-theme-editor_mysql_1                /bin/sh -c /entrypoint_wra ...   Up (health: starting)   127.0.0.2:3308->3306/tcp
remix-theme-editor_page-renderer_1        npm run start:watch              Up
remix-theme-editor_python-app_1           /entrypoint                      Exit 2
remix-theme-editor_redis_1                docker-entrypoint.sh /bin/ ...   Up (health: starting)   6379/tcp
remix-theme-editor_scheduler_1            /entrypoint --environment  ...   Exit 137
remix-theme-editor_socket_1               /entrypoint --environment  ...   Exit 1
remix-theme-editor_static-builder_1       npm run watch                    Up
remix-theme-editor_static-http_1          nginx -g daemon off;             Up                      127.0.0.2:6544->443/tcp, 80/tcp
remix-theme-editor_web_1                  /entrypoint --environment  ...   Exit 1
remix-theme-editor_worker_1               /entrypoint --environment  ...   Exit 1
remix-theme-editor_worker_screenshots_1   /entrypoint --environment  ...   Exit 1

如果您想了解更多细节,请将 docker inspectdocker ps -q <service-name>结合使用。

% docker inspect --format "\{\{json .State.Health }}" $(docker-compose ps -q mq) | jq
{
"Status": "starting",
"FailingStreak": 48,
"Log": [
{
"Start": "2018-10-03T00:40:18.671527745-05:00",
"End": "2018-10-03T00:40:18.71729051-05:00",
"ExitCode": -1,
"Output": "OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused \"exec: \\\"nc\\\": executable file not found in $PATH\": unknown"
},
...

你总是可以通过自己执行 healthcheck 代码来调试 healthcheck。例如:

% docker exec -it $(docker-compose ps -q socket) nc -w2 127.0.0.1 5672
(UNKNOWN) [127.0.0.1] 5672 (?) : Connection refused

您也可以在 shell 中执行同样的操作:

% docker exec -it $(docker-compose ps -q socket) bash
root@b5da5207d344:~/src# nc -w2 127.0.0.1 5672
(UNKNOWN) [127.0.0.1] 5672 (?) : Connection refused
root@b5da5207d344:~/src# echo $?
1

最后,您可以简单地在第一个终端窗口中使用 docker-compose up,在另一个终端窗口中使用 docker-compose logs -f。这将显示来自 docker 组合管理容器的所有日志。

群体模式:

  1. 首先使用 docker service ps service_name 在经理身上查找失败的任务 ID 和相应的节点。
manager$ docker service ps service_name
ID                  NAME                 IMAGE            NODE                DESIRED STATE       CURRENT STATE               ERROR                              PORTS
liwww3qzg9dz        service_name.1       image_name:1.3   s-3                 Running             Running 27 seconds ago
hcgxmwk2efj0         \_ service_name.1   image_name:1.3   s-3                 Shutdown            Failed about a minute ago   "task: non-zero exit (137): do…"

在本例中,hcgxmwk2efj0是任务 id,s-3是节点名。

  1. 然后使用 docker inspect --format "\{\{json .Status.ContainerStatus.ContainerID }}" task_id 在经理身上获取容器 ID。
manager$ docker inspect --format "\{\{json .Status.ContainerStatus.ContainerID }}" hcgxmwk2efj0
"412b09d5244047b31471248fd9a0807e5ea42406fb8f5b1701df2244933e30c8"
  1. 然后 ssh 到 那个节点,并使用命令 docker inspect --format "\{\{json .State.Health }}" container_id | jq获取 healthcheck 的日志。(此命令不需要 | jq)
s-3$ docker inspect --format "\{\{json .State.Health }}" 412b09d5244047b31471248fd9a0807e5ea42406fb8f5b1701df2244933e30c8 | jq
{
"Status": "unhealthy",
"FailingStreak": 3,
"Log": [
{
"Start": "2021-09-07T06:10:05.233163051Z",
"End": "2021-09-07T06:10:07.585487343Z",
"ExitCode": 0,
"Output": "... log 1 ..."
},
{
"Start": "2021-09-07T06:10:37.644936244Z",
"End": "2021-09-07T06:10:39.881196276Z",
"ExitCode": 0,
"Output": "... log 2 ..."
},
{
"Start": "2021-09-07T06:11:10.16172012Z",
"End": "2021-09-07T06:11:25.161912411Z",
"ExitCode": -1,
"Output": "Health check exceeded timeout (15s)"
},
{
"Start": "2021-09-07T06:11:55.297395088Z",
"End": "2021-09-07T06:12:10.302928565Z",
"ExitCode": -1,
"Output": "Health check exceeded timeout (15s)"
},
{
"Start": "2021-09-07T06:12:40.371234778Z",
"End": "2021-09-07T06:12:55.371393914Z",
"ExitCode": -1,
"Output": "Health check exceeded timeout (15s)"
}
]
}