πŸ”§ DevOps

Pod

Pod

The smallest deployment unit in Kubernetes. Contains one or more containers.

Detailed Explanation

A Pod is the smallest deployable unit that can be created and managed in Kubernetes. The name comes from "a pod of whales," and it groups one or more containers together, sharing the same network namespace and storage.

Containers within a Pod communicate with each other via localhost and share the same IP address. A single-container Pod is generally recommended, but multi-container Pods are used in the sidecar pattern (logging, proxy) or the ambassador pattern (external service proxy).

A Pod is designed to be ephemeral. It can be deleted and recreated at any time due to node failures, resource shortages, or scheduler decisions. Therefore, the standard practice is to manage Pods through higher-level controllers such as Deployment, StatefulSet, and DaemonSet.

In practice, a Pod spec defines resource requests/limits, liveness/readiness probes, security context, environment variables, secret mounts, and more. In production, set replicas: 3 or more to ensure high availability.

Code Example

# ν”„λ‘œλ•μ…˜ 레벨 Pod μŠ€νŽ™ (Deployment ꢌμž₯)
apiVersion: v1
kind: Pod
metadata:
  name: api-server
  labels:
    app: api-server
    version: v1.2.3
    team: backend
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/port: "9090"
spec:
  # λ³΄μ•ˆ μ„€μ •
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    fsGroup: 2000

  # μ΄ˆκΈ°ν™” μ»¨ν…Œμ΄λ„ˆ (메인 μ»¨ν…Œμ΄λ„ˆ μ‹€ν–‰ μ „)
  initContainers:
    - name: init-db-check
      image: busybox:1.36
      command: ['sh', '-c',
        'until nc -z postgres-service 5432; do
          echo "Waiting for database...";
          sleep 2;
        done']

  containers:
    # 메인 μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ»¨ν…Œμ΄λ„ˆ
    - name: api
      image: myregistry.io/api-server:v1.2.3
      imagePullPolicy: IfNotPresent

      ports:
        - name: http
          containerPort: 8080
        - name: metrics
          containerPort: 9090

      # λ¦¬μ†ŒμŠ€ μ œν•œ (ν•„μˆ˜!)
      resources:
        requests:
          cpu: "250m"
          memory: "256Mi"
        limits:
          cpu: "1000m"
          memory: "512Mi"

      # ν™˜κ²½λ³€μˆ˜
      env:
        - name: NODE_ENV
          value: "production"
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: db-config
              key: host
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secrets
              key: password

      # ν—¬μŠ€μ²΄ν¬ (ν•„μˆ˜!)
      livenessProbe:
        httpGet:
          path: /health/live
          port: 8080
        initialDelaySeconds: 30
        periodSeconds: 10
        failureThreshold: 3

      readinessProbe:
        httpGet:
          path: /health/ready
          port: 8080
        initialDelaySeconds: 5
        periodSeconds: 5
        failureThreshold: 3

      # Graceful shutdown
      lifecycle:
        preStop:
          exec:
            command: ["/bin/sh", "-c", "sleep 10"]

      # λ³Όλ₯¨ 마운트
      volumeMounts:
        - name: config-volume
          mountPath: /app/config
          readOnly: true
        - name: cache-volume
          mountPath: /app/cache

    # μ‚¬μ΄λ“œμΉ΄ μ»¨ν…Œμ΄λ„ˆ (둜그 μˆ˜μ§‘)
    - name: log-shipper
      image: fluent/fluent-bit:2.2
      resources:
        requests:
          cpu: "50m"
          memory: "64Mi"
        limits:
          cpu: "100m"
          memory: "128Mi"
      volumeMounts:
        - name: log-volume
          mountPath: /var/log/app

  volumes:
    - name: config-volume
      configMap:
        name: api-config
    - name: cache-volume
      emptyDir: {}
    - name: log-volume
      emptyDir: {}

  # μŠ€μΌ€μ€„λ§ μ„€μ •
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 100
          podAffinityTerm:
            labelSelector:
              matchLabels:
                app: api-server
            topologyKey: kubernetes.io/hostname

  terminationGracePeriodSeconds: 30
  restartPolicy: Always

How It's Said in Practice

Senior: "The api-server Pod keeps getting OOMKilled. Have you done any memory profiling?"

Junior: "Yes, memory goes up to 480MB during peak hours. The current limit is 256Mi, so I think raising it to 512Mi should work."

Interviewer: "Please explain the difference between a Pod and a Container."

Candidate: "A Pod is the deployment unit of Kubernetes and contains one or more Containers. Containers within the same Pod share network and storage, are scheduled together, and are terminated together."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "This Pod spec has no readinessProbe. During deployment, traffic could be routed to a Pod that isn't ready."

Developer: "Ah, the /health/ready endpoint exists, but I forgot to add the probe configuration. I'll add it."

Cautions

Learn More