Containers and orchestration
Containers package applications and their dependencies; orchestration (Kubernetes, etc.) runs and manages many containers at scale across a cluster.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Containers let engineers package an application and its libraries into a single, portable unit so the app runs the same on a developer laptop, test servers and cloud VMs. Orchestration lets an engineer run, scale, update and recover hundreds or thousands of containers automatically across many machines (a cluster). You will use these in cloud deployment, CI/CD pipelines and production services.
Key ideas
(Definitions are bold and given before they are used.)
- Container: a lightweight runtime instance that runs an application and its dependencies packaged together. Containers use operating-system-level isolation (no full guest OS per container), so they are smaller and start faster than virtual machines.
- Image: a read-only filesystem (composed of layers) plus metadata to create a container. An image is layered: most build steps produce a new image layer; some metadata-only instructions may not create a new filesystem layer (and BuildKit may optimize layers); the final image is a union of layers, with copy-on-write at runtime.
- Container runtime: software that runs containers from images (examples: containerd, CRI-O, historically Docker Engine). These runtimes support the OCI (Open Container Initiative) image format and invoke an OCI-compliant low-level runtime (e.g., runc or crun) that implements the OCI Runtime Spec.
- Namespace (Linux): a kernel feature that provides isolation of kernel resources (example namespaces: PID, mount, network, UTS, IPC, user). Containers use namespaces to give each container its own view of system resources.
- cgroup (control group): a Linux kernel feature that limits and accounts for resource usage (CPU, memory, block I/O) of a group of processes. Containers use cgroups to enforce resource limits.
- Pod (Kubernetes): the smallest deployable unit in Kubernetes. A pod contains one or more tightly-coupled containers that share the same network namespace (same IP and ports) and may share storage volumes.
- Cluster (Kubernetes): a set of machines (physical or virtual) running Kubernetes. A cluster has a control plane and worker nodes.
- Node: a machine (VM or physical) in the cluster that runs pods. A node runs a container runtime, kubelet and kube-proxy.
- Control plane: the set of components that make global decisions about the cluster (kube-apiserver, etcd, scheduler, controller-manager, and in cloud environments a cloud-controller-manager).
- Deployment (Kubernetes): a controller object that declaratively defines a desired state for pods (template) and number of replicas. The deployment creates and manages ReplicaSets to reach and maintain that state, and supports rolling updates.
- Service (Kubernetes): an abstract way to expose a set of pods as a network service. A Service provides a stable IP and DNS name and can load-balance traffic to multiple pods that match a label selector. Common types: ClusterIP (internal), NodePort (exposes a port on each node), LoadBalancer (requests a cloud LB), ExternalName (DNS CNAME), and headless Services (set
clusterIP: None) for direct Pod discovery (e.g., StatefulSets). Kubernetes uses the CNI (Container Network Interface) to implement pod networking; common plugins include Flannel and Calico. - ReplicaSet: ensures a specified number of pod replicas are running. Usually managed by a Deployment; you rarely create ReplicaSet directly.
- Scheduler: control-plane component that assigns pods to nodes based on resource requests, taints/tolerations and scheduling policies.
- Namespace (Kubernetes): a logical partition of cluster resources to support multiple teams or projects; resources (pods, services) are created inside a namespace.
Assumptions often made in exam derivations or architecture questions:
- Linux kernel supports namespaces and cgroups (assume modern Linux). Specify if using a different OS.
- Kubernetes control plane is reachable by kubelets and kubectl for the examples.
What to draw (exact diagram instructions):
- Docker image & container diagram: draw a rectangle labelled "Host OS (Linux)". Inside it, draw layered rectangles labelled "Base image layer", "App layer" stacked (union filesystem). Next draw a thin rectangle around a set of process boxes labelled "Container (processes)". Annotate arrows: layers → image, runtime → creates container processes, kernel provides namespaces and cgroups.
- Kubernetes architecture: draw two columns. Left column: Control plane box containing four small boxes: kube-apiserver, etcd, kube-scheduler, controller-manager. Right column: three Node boxes; inside each node draw kubelet, kube-proxy, container runtime, and several Pods (each pod drawn as a labelled rounded box with 1–2 containers inside). Draw arrows from kube-apiserver to nodes (control). Label service as a virtual IP that load-balances to pods across nodes.
Formulas
Remember commands and manifest patterns (syntax snippets to memorize):
docker build -t myimage:tag . — build image from Dockerfile in current directory.
docker run -d -p 8080:80 --name myapp myimage:tag — run container detached, map host port 8080 to container port 80.
docker ps -a — list containers.
docker exec -it mycontainer /bin/sh — open a shell inside a running container.
Dockerfile common instructions to remember: FROM, RUN, COPY, ADD, WORKDIR, CMD, ENTRYPOINT, EXPOSE, ENV.
Kubernetes manifest minimal pod (YAML) pattern to remember: apiVersion, kind: Pod, metadata, spec with containers list, each with name, image, ports.
Key kubectl commands: kubectl apply -f manifest.yaml, kubectl get pods -n namespace, kubectl describe pod pod-name -n namespace, kubectl logs pod-name [-c container], kubectl get svc.
Create a deployment: kubectl apply -f deployment.yaml with apiVersion: apps/v1, kind: Deployment, spec.selector matching spec.template.metadata.labels (required), spec.replicas, and spec.template for the pod template.
Expose a service: kubectl expose deployment my-deploy --type=NodePort --port=80 --target-port=8080.
Scale deployment: kubectl scale deployment my-deploy --replicas=5.
Rolling update (declarative) is done by changing image in the deployment manifest and kubectl apply.
Worked examples
Example 1 — Dockerfile build and run (step-by-step):
Task: Create and run a small nginx container serving a static page on host port 8080.
Files: index.html containing "Hello from container" and Dockerfile with contents:
FROM nginx:alpineCOPY index.html /usr/share/nginx/html/index.htmlSteps:
- Build image:
docker build -t hello-nginx:1.0 .(command before numbers) - Run container:
docker run -d -p 8080:80 --name hello hello-nginx:1.0 - Verify:
curl http://localhost:8080should return the page. If container fails,docker logs helloanddocker ps -ato debug. Final answer: Container serves page on host port 8080.
Example 2 — Kubernetes create deployment and expose (step-by-step trace):
Task: Create a Deployment of 3 replicas, then scale to 5, and expose via NodePort.
Deployment manifest (short): kind: Deployment, spec.replicas: 3, template contains container nginx:1.19 port 80.
Steps trace:
kubectl apply -f deploy.yaml→ API server persists the Deployment in etcd; the Deployment controller creates a ReplicaSet; the ReplicaSet controller creates Pods; the scheduler binds Pods to Nodes; kubelets pull images and start containers.kubectl get podsshows 3 pods:mydeploy-xxxxx-abcde(three names). Each pod has STATUS Running.kubectl expose deployment mydeploy --type=NodePort --port=80 --target-port=80creates a Service with a stable ClusterIP and a NodePort (e.g. 31234; typically in range 30000-32767). View the assigned NodePort withkubectl get svc.kubectl scale deployment mydeploy --replicas=5→ controller increases ReplicaSet desired replicas to 5; two more pods created.kubectl get podsshows 5 pods. Service load-balances across available pods. Explain checks: usekubectl describe podto view node assignment and resource requests.
Example 3 — Pod networking and port mapping (theory model answer): Question: Why do pods share ports between containers and how to expose multiple containers in a pod? Answer: Containers in a pod share the same network namespace. This means they share the same IP address and can communicate via localhost and ports (for example container A listens on 8080, container B can connect to localhost:8080). Note: two containers cannot bind to the same TCP/UDP port simultaneously inside a pod. To expose the pod to the outside, a Service selects pods by labels and routes traffic to container ports.
Common mistakes
- Saying "Kubernetes runs containers directly" without mentioning the container runtime and kubelet.
- Calling a pod and a container the same: a pod may contain multiple containers.
- Forgetting that pods are ephemeral: pods can be replaced by controllers; save state in volumes or external storage.
- Misunderstanding Service types: ClusterIP is internal only; NodePort exposes a port on all nodes.
- Mixing up Dockerfile
CMDandENTRYPOINT: their behaviors combine in specific ways — test if unsure. - Assuming Docker Engine must be present for Kubernetes; newer Kubernetes uses CRI and runtimes like containerd (dockershim was deprecated).
For your exam
Kinds of questions:
- Definitions and short notes (define container, image, pod, deployment, service).
- Diagrams (draw Kubernetes architecture, image layers, pod network model).
- Commands and manifests (write a Dockerfile, write minimal pod/deployment YAML, explain fields).
- Traces and algorithms (explain steps when
kubectl applya deployment; how rolling updates happen). - Short troubleshooting: given logs or status, say why pods are CrashLoopBackOff.
What to include for full marks:
- Precise definitions of terms asked for (use bold terms as in Key ideas).
- Correct and labelled diagrams (control plane vs nodes, image layers, namespaces vs cgroups) with brief captions.
- Correct command syntax or manifest with required fields (apiVersion, kind, metadata, spec).
- Explanations of assumptions (e.g., Linux kernel features available, network model). If asked to draw a sequence, list the control-plane/data flow:
kubectl → kube-apiserver → etcd (persist); controllers and the scheduler watch the API via the apiserver; the scheduler binds Pods to Nodes; kubelets watch the API, start/stop containers, and post status back to the apiserver (and thus etcd).
Quick check
- What two kernel features give containers isolation and resource limits?
- In Kubernetes what is the smallest deployable unit?
- Which object provides a stable IP and load-balances to pods? Answers: Namespaces and cgroups; Pod; Service.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?