Kubernetes Explained
Understand Kubernetes architecture, clusters, pods, deployments, services, networking, storage, scaling and how Kubernetes manages containerized applications.
Kubernetes is an open-source platform for deploying, managing and scaling containerized applications. Instead of manually starting containers on individual servers, Kubernetes coordinates workloads across a cluster of machines and continuously works to keep applications running according to their desired configuration.
Kubernetes is commonly used when applications consist of multiple services, need automatic scaling, require high availability or must run consistently across different environments. It provides a common control plane for scheduling containers, managing networking, handling storage and replacing failed workloads.
What Is Kubernetes?
Kubernetes, often abbreviated as K8s, is a container orchestration platform. It manages containers by deciding where workloads should run, monitoring their state and taking corrective actions when the actual state differs from the desired state.
Kubernetes does not replace containers. Instead, it provides the infrastructure and control mechanisms needed to run containers reliably at scale. Container runtimes such as containerd or CRI-O execute containers, while Kubernetes coordinates them.
Why Use Kubernetes?
- Deploy applications consistently across environments.
- Automatically restart failed workloads.
- Scale applications horizontally.
- Distribute workloads across multiple machines.
- Provide service discovery and internal networking.
- Manage rolling application updates.
- Coordinate persistent storage.
Kubernetes Cluster
A Kubernetes cluster is a group of machines managed as a single system. The cluster contains a control plane responsible for managing the cluster and worker nodes that run application workloads.
| Part | Purpose |
|---|---|
| Control plane | Manages the cluster and makes scheduling decisions |
| Worker nodes | Run application workloads |
| Pods | Smallest deployable Kubernetes units |
| Services | Provide stable network access to workloads |
Control Plane
The Kubernetes control plane is responsible for maintaining the desired state of the cluster. It receives configuration from users and controllers, stores cluster state and determines which actions are required to keep the cluster running correctly.
| Component | Purpose |
|---|---|
| API Server | Exposes the Kubernetes API |
| etcd | Stores cluster state |
| Scheduler | Assigns pods to suitable nodes |
| Controller Manager | Runs controllers that maintain desired state |
Kubernetes API
The Kubernetes API is the main interface for interacting with a cluster. Tools such as kubectl, deployment systems and Kubernetes controllers communicate with the API server to create, inspect and modify resources.
kubectl get pods
kubectl get deployments
kubectl get servicesWorker Nodes
Worker nodes provide the computing resources where application workloads run. A node normally contains a container runtime, the kubelet agent and networking components required to communicate with the rest of the cluster.
| Node Component | Purpose |
|---|---|
| kubelet | Ensures assigned pods are running |
| Container runtime | Runs containers |
| kube-proxy | Supports service networking |
What Is a Pod?
A pod is the smallest deployable unit in Kubernetes. A pod usually contains one application container, although multiple tightly coupled containers can share the same pod when they need to share networking and storage.
Containers inside the same pod share a network namespace and can communicate with each other through localhost. Pods are designed to be replaceable, so applications should not depend on a particular pod remaining alive forever.
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80Deployments
A Deployment manages a set of identical pods and provides declarative control over application replicas and updates. Kubernetes uses the Deployment to create the required pods and replace them when necessary.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80Replica Management
The replicas field specifies how many pod instances should normally exist. If one pod fails while three replicas are configured, Kubernetes attempts to create another pod so the desired number of replicas is restored.
| Desired Replicas | Typical Result |
|---|---|
| 1 | One pod instance |
| 3 | Three pod instances |
| 5 | Five pod instances |
Kubernetes Services
Pods can be created and replaced frequently, which means their individual IP addresses should not be treated as permanent. A Service provides a stable network endpoint and forwards traffic to matching pods.
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web-app
ports:
- port: 80
targetPort: 80| Service Type | Typical Use |
|---|---|
| ClusterIP | Internal cluster communication |
| NodePort | Expose a service through a node port |
| LoadBalancer | Expose a service through an external load balancer |
| ExternalName | Map a service name to an external DNS name |
Kubernetes Networking
Kubernetes provides networking that allows pods to communicate with one another and allows services to provide stable access to groups of pods. The exact implementation depends on the cluster's networking solution, but Kubernetes networking is designed around predictable communication between workloads.
For external HTTP and HTTPS traffic, clusters commonly use an Ingress or Gateway-based architecture. These resources can route requests to different services according to hostnames, paths and other rules.
Namespaces
Namespaces provide logical separation inside a Kubernetes cluster. They are useful for organizing resources belonging to different applications, teams or environments and can also be combined with access-control and resource-management policies.
kubectl create namespace development
kubectl get pods -n developmentConfigMaps
ConfigMaps store non-sensitive configuration data separately from container images. Applications can consume ConfigMap values through environment variables, command-line arguments or mounted files.
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_MODE: production
LOG_LEVEL: infoSecrets
Secrets are Kubernetes resources intended for sensitive configuration such as passwords, tokens and credentials. Applications can consume secrets through environment variables or mounted files.
Storage in Kubernetes
Containers are often treated as disposable, so data that must survive pod replacement should be stored separately. Kubernetes provides storage abstractions such as PersistentVolumes and PersistentVolumeClaims for applications that need persistent data.
| Resource | Purpose |
|---|---|
| PersistentVolume | Represents available persistent storage |
| PersistentVolumeClaim | Requests storage for an application |
| StorageClass | Defines how storage can be dynamically provisioned |
PersistentVolumeClaim Example
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10GiHealth Checks
Kubernetes can monitor application health using probes. Readiness probes determine whether a container is ready to receive traffic, while liveness probes help detect containers that need to be restarted.
readinessProbe:
httpGet:
path: /health
port: 8080
periodSeconds: 10
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 15
periodSeconds: 20Scaling Applications
Kubernetes can scale workloads by changing the number of pod replicas. Manual scaling is useful for simple scenarios, while the Horizontal Pod Autoscaler can adjust replicas based on resource utilization or other supported metrics.
kubectl scale deployment web-app --replicas=5Rolling Updates
Deployments support rolling updates that gradually replace old pods with new versions. This allows applications to be updated without stopping every replica at the same time.
kubectl set image deployment/web-app web=nginx:1.28
kubectl rollout status deployment/web-appSelf-Healing
One of Kubernetes' major features is its ability to continuously reconcile the cluster's actual state with the desired state. If a managed pod disappears, Kubernetes can create a replacement. If the desired replica count changes, controllers work toward the new target.
Desired State
Kubernetes configuration is generally declarative. Instead of describing every individual action that must happen, users describe the desired state of resources and Kubernetes controllers determine how to reach and maintain that state.
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: example/api:1.0Labels and Selectors
Labels are key-value pairs attached to Kubernetes resources. Selectors use those labels to identify related resources. Deployments, Services and other Kubernetes objects rely heavily on labels and selectors to connect controllers with the workloads they manage.
metadata:
labels:
app: backend
environment: productionResource Requests and Limits
Kubernetes allows workloads to specify CPU and memory requests and limits. Requests help the scheduler determine where a pod can fit, while limits constrain the amount of a resource a container can consume.
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
cpu: "500m"
memory: "512Mi"Kubernetes and Docker
Docker and Kubernetes solve related but different problems. Docker is commonly used to build and run containers, while Kubernetes coordinates containerized workloads across a cluster. Modern Kubernetes clusters can use container runtimes that implement the Kubernetes Container Runtime Interface rather than relying directly on the Docker Engine.
| Technology | Primary Role |
|---|---|
| Docker | Build and work with containers |
| Kubernetes | Orchestrate containerized workloads |
Kubernetes and Docker Compose
Docker Compose is convenient for defining and running multi-container applications on a local machine or relatively simple environments. Kubernetes is designed for cluster orchestration and provides features for scheduling, scaling, service discovery, rolling updates and self-healing.
Typical Kubernetes Workflow
Build container image
↓
Push image to a registry
↓
Create Kubernetes manifests
↓
Apply resources to the cluster
↓
Kubernetes schedules pods
↓
Services expose workloads
↓
Controllers maintain desired stateCommon Kubernetes Resources
| Resource | Purpose |
|---|---|
| Pod | Runs one or more containers |
| Deployment | Manages replicated application pods |
| Service | Provides stable network access |
| ConfigMap | Stores non-sensitive configuration |
| Secret | Stores sensitive configuration data |
| Namespace | Provides logical resource separation |
| PersistentVolumeClaim | Requests persistent storage |
| Ingress | Routes external HTTP and HTTPS traffic |
Common Mistakes
- Managing production applications as individual pods.
- Using the latest image tag without controlled versioning.
- Ignoring CPU and memory requests.
- Storing persistent data only inside containers.
- Exposing services unnecessarily.
- Treating Kubernetes Secrets as automatically secure storage.
- Skipping readiness and liveness checks.
- Giving applications more permissions than they need.
Best Practices
- Use Deployments or other controllers instead of manually managing production pods.
- Pin container images to predictable versions.
- Define appropriate resource requests and limits.
- Use readiness and liveness probes for long-running applications.
- Keep configuration separate from container images.
- Use namespaces to organize environments and teams.
- Restrict network and API access to the minimum required.
- Back up important cluster data and persistent application data.
- Use declarative manifests and version them with the application.
Frequently Asked Questions
What is Kubernetes used for?
Kubernetes is used to deploy, manage, scale and maintain containerized applications across a cluster of machines.
What is a Kubernetes pod?
A pod is the smallest deployable Kubernetes unit and contains one or more closely related containers that share networking and storage.
Is Kubernetes the same as Docker?
No. Docker is primarily used for building and working with containers, while Kubernetes orchestrates containerized workloads across a cluster.
What is a Kubernetes Deployment?
A Deployment manages replicated pods and provides declarative updates, scaling and replacement of application instances.
What is a Kubernetes Service?
A Service provides a stable network endpoint for accessing a group of pods even when individual pod IP addresses change.
Do I need Kubernetes for every Docker application?
No. Kubernetes is most useful when an application benefits from cluster orchestration, scaling, high availability or automated workload management. Smaller applications may be simpler to operate with Docker Compose or another deployment approach.
Helpful Kubernetes Tools
A Kubernetes YAML Generator helps create Kubernetes resource manifests, a Kubernetes YAML Formatter formats configuration files for easier review, a Helm Values Formatter helps organize Helm configuration, a YAML Formatter improves the readability of YAML documents, and a YAML Validator checks Kubernetes manifests and other YAML files for syntax errors before they are used.
Conclusion
Kubernetes provides a powerful platform for running containerized applications across clusters of machines. Its core concepts include pods, deployments, services, namespaces, configuration resources, persistent storage and controllers that continuously maintain the desired state. Although Kubernetes can simplify large-scale application operations, it also introduces additional infrastructure and operational complexity. Understanding the fundamentals allows developers to decide when Kubernetes is appropriate and build deployments that are reliable, scalable and maintainable.