stern로 pod log 편하게 보기

cloud2000·2026년 4월 30일

설치

# macOS
brew install stern

# Linux (직접 바이너리)
curl -Lo stern https://github.com/stern/stern/releases/latest/download/stern_linux_amd64
chmod +x stern && sudo mv stern /usr/local/bin/

# kubectl 플러그인으로
kubectl krew install stern
kubectl stern <pod-query>

기본 구조

stern <pod-query> [flags]
#     ↑ 정규식 또는 resource/name 형태

pod-query는 정규식(regex) 또는 <resource>/<name> 형태로 지정합니다. Pod가 삭제되면 자동으로 tail에서 제거되고, 새 Pod가 추가되면 자동으로 tail에 포함됩니다.


Pod 선택 방법 3가지

# 1. Pod 이름 정규식 (가장 많이 사용)
stern "dify-api.*"          # dify-api로 시작하는 모든 Pod
stern ".*worker.*"          # worker가 포함된 모든 Pod
stern "api|worker"          # api 또는 worker가 포함된 Pod

# 2. 라벨 셀렉터
stern -l app=dify-api -n dify
stern -l "app in (api,worker)" -n dify
stern -l tier=agent,env=prod -n yms

# 3. Kubernetes 리소스 직접 지정
stern deployment/dify-api -n dify
stern statefulset/dify-db -n dify
stern daemonset/log-collector -n monitoring

자주 쓰는 옵션

# ── 네임스페이스 ─────────────────────────────────────────────
stern -l app=dify -n dify                  # 특정 네임스페이스
stern -l app=dify -A                       # 전체 네임스페이스

# ── 시간 / 줄 수 ─────────────────────────────────────────────
stern -l app=dify --since 30m             # 최근 30분
stern -l app=dify --since 2h             # 최근 2시간
stern -l app=dify --tail 100             # 시작 시 최근 100줄만
stern -l app=dify --tail 0               # 새 로그만 (기존 로그 없이)

# ── 컨테이너 선택 ──────────────────────────────────────────────
stern -l app=dify -c api                  # api 컨테이너만
stern -l app=dify -c "api|worker"         # 정규식으로 복수 선택
stern -l app=dify --container-state all  # 종료된 컨테이너 포함

# ── 필터링 ─────────────────────────────────────────────────────
stern -l app=dify -i "ERROR|WARN"         # ERROR 또는 WARN 포함 줄만
stern -l app=dify -e "health.*check"      # healthcheck 로그 제외
stern -l app=dify -e "GET /ping" -e "GET /metrics"  # 여러 패턴 제외

# ── 동시 연결 수 (기본 5개) ────────────────────────────────────
stern -l app=dify --max-log-requests 50   # Pod 많을 때 필수

출력 포맷

# 기본 출력 (Pod명 + 컨테이너명 컬러 구분)
stern -l app=dify -n dify

# 타임스탬프 추가
stern -l app=dify --timestamps            # 전체 타임스탬프
stern -l app=dify --timestamps short      # 짧은 형식

# 출력 포맷 선택
stern -l app=dify -o default   # 기본 (pod container log)
stern -l app=dify -o raw       # 원본 로그만 (Pod명 없음)
stern -l app=dify -o json      # JSON 구조화 출력
stern -l app=dify -o ppretty   # 예쁜 형식

# 커스텀 템플릿 (Go template)
stern -l app=dify \
  --template '{{.NodeName}} | {{.PodName}} | {{.Message}}{{"\n"}}'

stern -l app=dify \
  --template '{{with .Labels}}{{index . "app"}}{{end}} {{.Message}}{{"\n"}}'

JSON 출력 예시 (-o json):

{
  "message": "INFO worker started",
  "nodeName": "node-01",
  "namespace": "dify",
  "podName": "dify-api-7d9f8c-xk2p1",
  "containerName": "api",
  "labels": {"app": "dify-api"}
}

실전 활용 패턴

# ── 에러 모니터링 ─────────────────────────────────────────────
stern -l tier=agent -n dify -i "ERROR|Exception|Traceback" --tail 0

# ── 특정 LOT 추적 ─────────────────────────────────────────────
stern -l tier=agent -n dify -i "2024-0812"

# ── 파일로 저장하며 화면 출력 (컬러 코드 없이) ─────────────────
stern -l app=dify --color never | tee dify-$(date +%Y%m%d_%H%M).log

# ── JSON 로그 파이프 처리 ──────────────────────────────────────
stern -l app=dify -o json | jq '.message'
stern -l app=dify -o json | jq 'select(.message | test("ERROR"))'

# ── 순서대로 로그 출력 (병렬 X, 순차) ─────────────────────────
stern -l app=dify --max-log-requests 1 --no-follow

# ── 특정 노드의 Pod만 ─────────────────────────────────────────
stern ".*" -n dify --node node-gpu-01

# ── 멀티 네임스페이스 ──────────────────────────────────────────
stern -l app=agent -n dify -n monitoring

설정 파일 (영구 기본값 지정)

기본 설정 파일 경로는 ~/.config/stern/config.yaml입니다.

# ~/.config/stern/config.yaml
tail: 50
max-log-requests: 100
timestamps: short
color: always
# 기본 네임스페이스 (선택)
namespace: dify

에이전트 환경 실전 예시

# 전체 에이전트 실시간 에러 모니터링
stern -l tier=agent -n dify \
  --tail 0 \
  --max-log-requests 50 \
  -i "ERROR|CRITICAL"

# SPC 에이전트만 — LOT 분석 추적
stern -l app=spc-analyzer -n yms \
  --since 1h \
  -i "LOT|Cpk|OOC"

# 에이전트 전체 로그 JSON으로 수집 → 파일 저장
stern -l tier=agent -n yms \
  -o json \
  --color never \
  --timestamps \
  | tee /var/log/yms-agents/$(date +%Y%m%d).jsonl

# 헬스체크, 메트릭 노이즈 제거하며 보기
stern -l tier=agent -n yms \
  -e "GET /health" \
  -e "GET /metrics" \
  -e "heartbeat"

요약 치트시트

옵션설명예시
-l라벨 셀렉터-l app=api
-n네임스페이스-n yms
-A전체 네임스페이스
-c컨테이너 (정규식)-c "api\|worker"
--since시간 범위--since 1h
--tail초기 줄 수--tail 100
-i포함 필터 (정규식)-i "ERROR"
-e제외 필터 (정규식)-e "health"
-o출력 포맷-o json
--max-log-requests동시 Pod 수--max-log-requests 50
--timestamps타임스탬프--timestamps short
--color never파일 저장 시\| tee log.txt
profile
유티클라우드

0개의 댓글