π What is Kubernetes?
Kubernetes is an open-source container orchestration platform originally developed by Google (now maintained by the Cloud Native Computing Foundation - CNCF).
It automates the deployment, scaling, and management of containerized applications (like Docker containers).
π Think of Kubernetes as the "operating system for your data center/cloud" that manages all your containerized workloads.
π°οΈ A Quick History
- 2014 β Google open-sourced Kubernetes.
- 2015 β Donated to CNCF.
- Inspired by Googleβs internal system Borg, which ran containers at massive scale.
- Today, Kubernetes is the industry standard for container orchestration.
βοΈ Why Do We Need Kubernetes?
Containers (like Docker) are great but managing them at scale is hard:
- How do you restart a failed container?
- How do you scale up/down when traffic changes?
- How do you load-balance across containers?
- How do you roll out updates without downtime?
π Kubernetes solves all of this automatically.
π§© Key Concepts in Kubernetes
1οΈβ£ Cluster
A Kubernetes cluster consists of:
- Master Node (Control Plane) β Manages the cluster (scheduler, API server, controller).
- Worker Nodes β Where containers (Pods) actually run.
2οΈβ£ Pod
The smallest deployable unit in Kubernetes.
A Pod wraps one or more containers (usually 1).
Example: A Pod could run a web server container + a logging sidecar container.
3οΈβ£ Deployment
Defines the desired state of Pods (e.g., 3 replicas of Nginx). Kubernetes ensures the cluster always matches this state.
Example: If 1 Pod crashes, Deployment brings it back automatically.
4οΈβ£ Service
Provides networking and load balancing for Pods. Since Pods are ephemeral (they come and go), a Service gives them a stable IP and DNS name.
5οΈβ£ Ingress
Manages external access to Services (HTTP/HTTPS). Lets you expose apps to the internet with domain names, SSL, routing, etc.
6οΈβ£ ConfigMaps & Secrets
- ConfigMap β Store configuration (key-value pairs).
- Secret β Store sensitive data (like API keys, passwords).
7οΈβ£ Autoscaling
Horizontal Pod Autoscaler (HPA) β Increases/decreases Pod replicas based on CPU/memory usage.
π Example: Running Nginx in Kubernetes
nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
π Run:
kubectl apply -f nginx-deployment.yaml
kubectl apply -f nginx-service.yaml
Now you have 3 replicas of Nginx running behind a load-balanced service.
π Advantages of Kubernetes
- Self-healing β Restarts failed containers, replaces Pods, reschedules workloads.
- Scalability β Auto-scales applications up or down.
- Portability β Works on any cloud (AWS, Azure, GCP) or on-prem.
- Load Balancing & Networking β Distributes traffic across Pods.
- Rolling Updates & Rollbacks β Deploy updates without downtime.
- Resource Efficiency β Optimizes CPU/memory usage.
- Ecosystem & Extensibility β Works with Helm charts, Operators, Service Meshes, etc.
π Real-World Use Cases
- Running microservices architectures.
- Hosting scalable web apps & APIs.
- CI/CD pipelines (Jenkins, GitLab runners on Kubernetes).
- Big Data & AI workloads (TensorFlow on K8s).
- Hybrid cloud strategy β Run the same workloads on-prem & cloud.
Essential kubectl CLI Commands π§΅
π Basic Setup & Cluster Info
| Command | Usage |
| kubectl version | Show client and server version. |
| kubectl cluster-info | Display cluster info. |
| kubectl config view | View kubeconfig settings. |
| kubectl config use-context <context> | Switch to a different cluster context. |
π¦ Pods
| Command | Usage |
| kubectl get pods | List all pods in the current namespace. |
| kubectl get pods -A | List pods across all namespaces. |
| kubectl describe pod <pod-name> | Show details of a pod. |
| kubectl logs <pod-name> | View logs of a pod. |
| kubectl exec -it <pod-name> -- sh | Access a pod's shell. |
π Deployments
| Command | Usage |
| kubectl get deployments | List deployments. |
| kubectl create deployment <name> --image=<image> | Create a new deployment. |
| kubectl scale deployment <name> --replicas=<num> | Scale a deployment. |
| kubectl rollout status deployment/<name> | Check rollout status. |
| kubectl rollout undo deployment/<name> | Rollback a deployment. |
π Services & Networking
| Command | Usage |
| kubectl get services | List all services. |
| kubectl expose deployment <name> --port=<port> --type=NodePort | Expose a deployment as a service. |
| kubectl port-forward svc/<service> <local-port>:<service-port> | Forward local port to service. |
π Config & Secrets
| Command | Usage |
| kubectl get configmaps | List all configmaps. |
| kubectl create configmap <name> --from-literal=key=value | Create configmap from literal values. |
| kubectl get secrets | List secrets. |
| kubectl create secret generic <name> --from-literal=key=value | Create secret from literal values. |
π Namespace Management
| Command | Usage |
| kubectl get namespaces | List all namespaces. |
| kubectl create namespace <name> | Create a new namespace. |
| kubectl delete namespace <name> | Delete a namespace. |
π§Ή Cleanup
| Command | Usage |
| kubectl delete pod <pod-name> | Delete a pod. |
| kubectl delete deployment <name> | Delete a deployment. |
| kubectl delete service <name> | Delete a service. |
| kubectl delete all --all | Delete all resources in the namespace. |