|
| 1 | +--- |
| 2 | +id: "kubernetes-024" |
| 3 | +title: "Init Container가 완료되지 않아 Pod가 시작되지 않는 문제" |
| 4 | +category: "kubernetes" |
| 5 | +difficulty: 1 |
| 6 | +tags: ["init-container", "pending", "pod", "debugging"] |
| 7 | +hints: |
| 8 | + - "kubectl describe pod 출력에서 Init Containers 섹션의 State를 확인하세요." |
| 9 | + - "Init Container의 로그를 확인해 어떤 명령이 실행 중인지 살펴보세요." |
| 10 | + - "Init Container가 대기하는 대상 서비스가 실제로 존재하는지 확인하세요." |
| 11 | +--- |
| 12 | + |
| 13 | +## 상황 |
| 14 | + |
| 15 | +신규 마이크로서비스를 배포했는데 Pod가 `Init:0/1` 상태에서 멈춰 있습니다. 메인 컨테이너가 시작되지 않아 서비스 전체가 동작하지 않습니다. 제공된 정보를 분석하여 원인을 찾으세요. |
| 16 | + |
| 17 | +## 데이터 |
| 18 | + |
| 19 | +### kubectl get pods 출력 |
| 20 | + |
| 21 | +```bash |
| 22 | +NAME READY STATUS RESTARTS AGE |
| 23 | +order-service-5c8d7f9b6-tn4k2 0/1 Init:0/1 0 8m |
| 24 | +order-service-5c8d7f9b6-gx7m3 0/1 Init:0/1 0 8m |
| 25 | +``` |
| 26 | + |
| 27 | +### kubectl describe pod order-service-5c8d7f9b6-tn4k2 (발췌) |
| 28 | + |
| 29 | +```yaml |
| 30 | +Init Containers: |
| 31 | + wait-for-db: |
| 32 | + Image: busybox:1.36 |
| 33 | + Command: |
| 34 | + - sh |
| 35 | + - -c |
| 36 | + - until nslookup postgres-primary.database.svc.cluster.local; do echo "Waiting for DB..."; sleep 2; done |
| 37 | + State: Running |
| 38 | + Started: Mon, 20 Jan 2025 09:10:00 +0000 |
| 39 | + Ready: False |
| 40 | +Containers: |
| 41 | + order-api: |
| 42 | + Image: registry.example.com/order-service:v3.0.1 |
| 43 | + State: Waiting |
| 44 | + Reason: PodInitializing |
| 45 | +Events: |
| 46 | + Type Reason Age From Message |
| 47 | + ---- ------ ---- ---- ------- |
| 48 | + Normal Scheduled 8m default-scheduler Successfully assigned default/order-service-5c8d7f9b6-tn4k2 |
| 49 | + Normal Pulled 8m kubelet Container image "busybox:1.36" already present on machine |
| 50 | + Normal Created 8m kubelet Created container wait-for-db |
| 51 | + Normal Started 8m kubelet Started container wait-for-db |
| 52 | +``` |
| 53 | + |
| 54 | +### kubectl logs order-service-5c8d7f9b6-tn4k2 -c wait-for-db (최근 출력) |
| 55 | + |
| 56 | +```log |
| 57 | +Server: 10.96.0.10 |
| 58 | +Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local |
| 59 | +
|
| 60 | +nslookup: can't resolve 'postgres-primary.database.svc.cluster.local' |
| 61 | +Waiting for DB... |
| 62 | +nslookup: can't resolve 'postgres-primary.database.svc.cluster.local' |
| 63 | +Waiting for DB... |
| 64 | +nslookup: can't resolve 'postgres-primary.database.svc.cluster.local' |
| 65 | +Waiting for DB... |
| 66 | +``` |
| 67 | + |
| 68 | +### kubectl get svc -n database 출력 |
| 69 | + |
| 70 | +```bash |
| 71 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 72 | +postgres-main ClusterIP 10.96.45.120 <none> 5432/TCP 30d |
| 73 | +``` |
| 74 | + |
| 75 | +## 해설 |
| 76 | + |
| 77 | +### 원인 분석 |
| 78 | + |
| 79 | +Init Container `wait-for-db`는 `postgres-primary.database.svc.cluster.local` DNS가 해석될 때까지 무한 대기하도록 설정되어 있습니다. 그러나 `database` 네임스페이스의 실제 Service 이름은 `postgres-main`이지 `postgres-primary`가 아닙니다. |
| 80 | + |
| 81 | +Init Container가 존재하지 않는 Service를 찾고 있어 DNS 조회가 계속 실패하고, Init Container가 영원히 완료되지 않으므로 메인 컨테이너도 시작되지 않습니다. |
| 82 | + |
| 83 | +### 해결 방법 |
| 84 | + |
| 85 | +```bash |
| 86 | +# 1. database 네임스페이스의 실제 Service 이름 확인 |
| 87 | +kubectl get svc -n database |
| 88 | + |
| 89 | +# 2. Deployment의 Init Container 명령을 올바른 서비스 이름으로 수정 |
| 90 | +kubectl edit deployment order-service |
| 91 | +# Init Container의 nslookup 대상을 수정: |
| 92 | +# postgres-primary.database.svc.cluster.local |
| 93 | +# → postgres-main.database.svc.cluster.local |
| 94 | + |
| 95 | +# 3. 롤아웃 상태 확인 |
| 96 | +kubectl rollout status deployment order-service |
| 97 | + |
| 98 | +# 4. Pod가 정상 Running인지 확인 |
| 99 | +kubectl get pods |
| 100 | +``` |
| 101 | + |
| 102 | +### 실무 팁 |
| 103 | + |
| 104 | +Init Container로 의존 서비스 대기 패턴을 구현할 때는, 서비스 이름을 하드코딩하지 말고 환경 변수나 ConfigMap으로 관리하세요. 또한 무한 대기 대신 타임아웃을 설정하면(`timeout 120 sh -c 'until ...'`) Init Container가 영원히 멈추는 것을 방지할 수 있습니다. |
0 commit comments