Part 11 · Docker · 11.1.11

Docker không phải Kubernetes thu nhỏ

Docker tooling build và chạy containers trên một engine/host; Kubernetes quản desired state của workloads trên cluster qua API/controllers. Kubernetes hiện không cần Docker Engine để chạy Pod.

Mental model: Docker/OCI tập trung vào image và container runtime trên host; Kubernetes thêm API, controllers, scheduler và cluster-level reconciliation. Vì vậy hãy debug theo boundary: image → runtime → Pod → scheduling/network/storage → controller → application.

1. Trách nhiệm khác nhau

ConcernDocker/OCIKubernetes
BuildDockerfile/BuildKit tạo OCI imageKhông build image; pull image từ registry
RuntimeDocker Engine → containerd → runckubelet → CRI runtime như containerd/CRI-O → OCI runtime
Desired stateCLI/Compose trên một engineAPI + controllers reconcile trên cluster
NetworkBridge/veth/DNS/NAT/published portCNI Pod network, Service, EndpointSlice, Ingress/Gateway
StorageWritable layer, bind mount, named volumeemptyDir, PV/PVC, StorageClass, CSI
Scaling/recoveryManual/Compose restart policyDeployment/StatefulSet/Job, scheduler, HPA, rescheduling
SecurityImage/runtime user, capabilities, seccomp, rootlessThêm RBAC, ServiceAccount, admission, policy, namespace/cluster controls

2. Image tới process trong Pod

docker build / buildx
  → OCI manifest + config + layers
  → registry by tag/digest
  → kubelet observes assigned Pod
  → CRI ImageService pulls/unpacks image
  → CRI RuntimeService creates Pod sandbox + containers
  → OCI runtime configures namespaces/cgroups/mounts/seccomp
  → application becomes PID 1 inside container namespace

Kubernetes bỏ dockershim từ lâu; image do Docker build vẫn chạy vì tuân OCI image format. Docker Engine và containerd không đồng nghĩa: Engine dùng containerd, nhưng kubelet nói qua CRI implementation chứ không gọi Docker CLI/API.

3. Dockerfile contract trở thành Pod behavior

Image/runtime choiceẢnh hưởng trong Kubernetes
Exec-form ENTRYPOINTPID 1 nhận SIGTERM đúng hơn khi Pod termination.
App bind 0.0.0.0Service/other Pods truy cập được Pod IP; bind loopback chỉ trong Pod.
Non-root UIDPhải tương thích runAsNonRoot, volume ownership và port.
Writable pathsPhải khai báo emptyDir/PVC/tmpfs khi root filesystem read-only.
Health endpointStartup/readiness/liveness probes dùng được nhưng semantics phải đúng.
Multi-arch manifestNode architecture pull đúng variant; sai platform gây exec format error.
Image digestImmutable rollout/reproducibility; tag mutable gây version ambiguity.

4. Compose không chuyển 1:1 sang Kubernetes

5. Resource limits: cùng kernel, khác scheduling layer

Docker runtime cgroups enforce CPU/memory/pids. Kubernetes thêm requests để scheduler đặt Pod và limits để runtime/cgroup enforce. CPU limit có thể throttle; memory limit có thể OOMKill. JVM memory gồm heap, metaspace, code cache, direct buffers, thread stacks và native/page cache.

resources:
  requests: { cpu: "500m", memory: "768Mi" }
  limits:   { cpu: "1",    memory: "1Gi" }

Request quá thấp tạo noisy neighbor/overpacking và HPA utilization sai; limit quá chặt tạo throttle/OOM. Tune từ workload và downstream capacity, không copy defaults.

6. Signals và termination timeline

Pod deletion / rollout
  → Endpoint readiness/removal bắt đầu hội tụ
  → preStop hook nếu có
  → SIGTERM tới container PID 1
  → app stop intake, drain in-flight, checkpoint
  → grace period hết thì SIGKILL

Endpoint propagation và proxy/LB connection draining không instant. App phải xử lý repeated/late requests và idempotency. preStop tiêu trong cùng terminationGracePeriodSeconds, không cộng thêm thời gian.

7. Networking mapping

Docker conceptKubernetes tương ứng gần nhấtKhông tương đương ở đâu
User-defined bridge DNSPod network + CoreDNS + ServiceService là virtual stable frontend, không chỉ container name DNS.
-p host:containerService NodePort/LoadBalancer, Ingress/GatewayK8s route qua cluster networking/controllers.
Container network namespacePod network namespaceContainers trong cùng Pod chia localhost/IP.
Docker network isolationNetworkPolicy qua CNIPolicy enforcement phụ thuộc CNI; namespace không tự deny.

8. Storage mapping

Container writable layer vẫn ephemeral. emptyDir sống theo Pod, không theo container restart nhưng mất khi Pod bị xóa/reschedule. PVC có lifecycle riêng và được CSI provision/attach/mount; access modes không tự chứng minh storage thật sự hỗ trợ concurrent application semantics. StatefulSet cho identity/order/PVC templates, không tự replicate database hoặc backup.

9. Security layers

  1. Supply chain: trusted base, pin digest, SBOM, scan/sign/provenance.
  2. Container: non-root, read-only, drop capabilities, seccomp, no privilege escalation.
  3. Pod/workload: ServiceAccount, securityContext, resource bounds.
  4. Cluster/API: RBAC, admission, audit, Secret encryption/external manager.
  5. Network/tenant: default-deny policies, namespace/cluster boundaries theo threat model.
Container boundary không phải VM boundary: workloads chia host kernel. Privileged container, hostPath và runtime socket có thể mở blast radius tới node/cluster.

10. Debug theo layer

  1. Image: digest/platform/config/user/entrypoint/file permissions.
  2. Runtime: container state, exit code, OOM, mounts, cgroups.
  3. Pod: init, probes, events, previous logs, security context.
  4. Scheduling: requests, taints, affinity, topology, PVC.
  5. Network: listener → Pod IP → EndpointSlice → Service → Ingress/Gateway/DNS.
  6. Controller: rollout conditions, ReplicaSet ownership, desired/available replicas.
  7. Application: request trace, pool/queue/downstream saturation và business invariant.

11. Interview questions

Kubernetes có dùng Docker không?
Không bắt buộc Docker Engine. Kubelet dùng CRI runtime như containerd/CRI-O; OCI images build bằng Docker vẫn chạy. Phân biệt Docker tooling/Engine với image format và container runtime.
Container chạy tốt bằng Docker nhưng fail trên K8s?
Kiểm tra architecture, non-root/file permission, read-only filesystem, bind address, env/secret, command override, probes, resource limits, volume ownership và Service targetPort.
Docker Compose và Kubernetes khác nhau cốt lõi?
Compose mô tả multi-container app trên Docker environment; Kubernetes là distributed declarative control plane với scheduling, reconciliation, service discovery, rollout, policy và cluster failure model.
Nguồn: Kubernetes container runtimes · Kubernetes and Docker · OCI · Docker Engine.