Healthcheck ve Monitoring

Container sağlık kontrolü ve izleme

Healthcheck Nedir?

Container'ın sağlıklı çalışıp çalışmadığını kontrol eden mekanizmadır.

Dockerfile'da Healthcheck

FROM nginx

HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
  CMD curl -f http://localhost/ || exit 1

Parametreler

ParametreVarsayılanAçıklama
--interval30sKontrol aralığı
--timeout30sTimeout süresi
--start-period0sBaşlangıç bekleme
--retries3Unhealthy için deneme

Docker Run'da Healthcheck

docker run -d \
  --name webserver \
  --health-cmd="curl -f http://localhost/ || exit 1" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  nginx

Compose'da Healthcheck

version: '3.8'

services:
  web:
    image: nginx
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

  db:
    image: postgres
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

Depends_on with Healthcheck

version: '3.8'

services:
  app:
    image: myapp
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy

  db:
    image: postgres
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 5s
      timeout: 5s
      retries: 5

  redis:
    image: redis
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]

Health Status Kontrolü

# Container health status
docker inspect --format='{{.State.Health.Status}}' container_name

# Detaylı health log
docker inspect --format='{{json .State.Health}}' container_name | jq