[CKA] Pod 구성 실습 패턴

Secret volume, sidecar logging, nodeSelector, static Pod 등 자주 쓰는 Pod 구성 패턴을 정리합니다.

Secret을 읽기 전용 volume으로 마운트

apiVersion: v1
kind: Pod
metadata:
  name: secret-reader
  namespace: practice
spec:
  containers:
  - name: app
    image: busybox
    command: ["sleep", "4800"]
    volumeMounts:
    - name: secret-volume
      mountPath: /etc/secret-volume
      readOnly: true
  volumes:
  - name: secret-volume
    secret:
      secretName: dotfile-secret

sidecar로 파일 로그 노출

애플리케이션과 sidecar가 같은 emptyDir을 /var/log에 마운트합니다. sidecar는 파일을 표준 출력으로 전달하므로 kubectl logs에서 읽을 수 있습니다.

apiVersion: v1
kind: Pod
metadata:
  name: cart-app
spec:
  containers:
  - name: app
    image: busybox
    command: ["/bin/sh", "-c", "while true; do date >> /var/log/app.log; sleep 2; done"]
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: sidecar
    image: busybox:1.28
    args: ["/bin/sh", "-c", "tail -n+1 -F /var/log/app.log"]
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}
kubectl logs cart-app -c sidecar

특정 노드 선택

spec:
  nodeSelector:
    disktype: ssd

단순한 label 일치에는 nodeSelector가 충분합니다. 선호 조건이나 복잡한 논리가 필요하면 node affinity를 사용합니다.

static Pod

grep staticPodPath /var/lib/kubelet/config.yaml
kubectl run web --image=nginx --dry-run=client -o yaml > web.yaml
sudo cp web.yaml /etc/kubernetes/manifests/web.yaml

static Pod manifest는 해당 노드의 kubelet이 관리합니다. init container가 멈춘 Pod는 애플리케이션 컨테이너보다 init container 로그를 먼저 확인합니다.

kubectl logs <pod> -c <init-container>

sidecar logging의 배경은 Kubernetes 로깅 아키텍처에서 확인할 수 있습니다.