我需要等待 Clickhouse 启动才能启动我的后端服务器,但是运行状况检查不起作用。
这是我的
docker-compose.yml
文件:
version: '3.9'
services:
server:
build: .
depends_on:
clickhouse:
condition: service_healthy
clickhouse:
image: yandex/clickhouse-server
ports:
- '8123:8123'
- '9000:9000'
- '9009:9009'
healthcheck:
test: ['CMD', 'curl', '-f', 'http://localhost:8123']
interval: 5s
timeout: 3s
retries: 5
但是,当我运行
docker-compose up --build
时,您可以看到Clickhouse服务器启动,一切都很好,但是健康检查从未通过。该命令抢先退出并出现错误:container for service "Clickhouse" is unhealthy
。但是,如果在此期间我在自己的计算机上(在 docker 容器之外)运行命令 curl -f http://localhost:8123
,那么它会返回 Ok.
那么有没有办法等待Clickhouse健康后再开始另一个服务呢?
试试这个方法:
..
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:8123/?query=SELECT%201 || exit 1
..
或
..
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:8123/ping || exit 1
..
此版本的解决方案v0.41.1
由于某种原因 localhost 未解析。使用IP 127.0.0.1.
CONTAINER REPOSITORY TAG IMAGE ID SIZE
otel-migrator signoz/signoz-schema-migrator 0.88.15 b83632b02639 29.1MB
signoz-alertmanager signoz/alertmanager 0.23.5 61e8cf1e8ddf 61MB
signoz-clickhouse clickhouse/clickhouse-server 24.1.2-alpine ccae8bc2d860 882MB
signoz-frontend signoz/frontend 0.41.1 702939f4533d 61.9MB
signoz-logspout gliderlabs/logspout v3.2.14 8584e170d2ab 32.6MB
signoz-otel-collector signoz/signoz-otel-collector 0.88.15 c387d1384cfb 182MB
signoz-query-service signoz/query-service 0.41.1 41bebb88cba6 57.6MB
signoz-zookeeper-1 bitnami/zookeeper 3.7.1 3ab0e8f032ab 510MB
docker-compose.yaml:
...
x-clickhouse-defaults: &clickhouse-defaults
restart: on-failure
# addding non LTS version due to this fix https://github.com/ClickHouse/ClickHouse/commit/32caf8716352f45c1b617274c7508c86b7d1afab
image: clickhouse/clickhouse-server:24.1.2-alpine
tty: true
depends_on:
- zookeeper-1
# - zookeeper-2
# - zookeeper-3
logging:
options:
max-size: 50m
max-file: "3"
healthcheck:
# "clickhouse", "client", "-u ${CLICKHOUSE_USER}", "--password ${CLICKHOUSE_PASSWORD}", "-q 'SELECT 1'"
test: wget --no-verbose --tries=1 http://127.0.0.1:8123/ping || exit 1
interval: 10s
timeout: 10s
retries: 3
...
测试/论证:
/ # ping localhost -c 3
PING localhost (::1): 56 data bytes
64 bytes from ::1: seq=0 ttl=64 time=0.051 ms
64 bytes from ::1: seq=1 ttl=64 time=0.042 ms
64 bytes from ::1: seq=2 ttl=64 time=0.047 ms
--- localhost ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.042/0.046/0.051 ms
/ # wget --no-verbose --tries=1 --spider http://localhost:8123/ping
Connecting to localhost:8123 ([::1]:8123)
wget: can't connect to remote host: Connection refused
/ # wget --no-verbose --tries=1 --spider http://::1:8123/ping
Connecting to ::1:8123 ([::1:8123]:80)
wget: can't connect to remote host: Network unreachable
/ # wget --no-verbose --tries=1 --spider http://127.0.0.1:8123/ping
Connecting to 127.0.0.1:8123 (127.0.0.1:8123)
remote file exists