Kubernetes Basics
Introduction to Kubernetes: container orchestration, pods, services, and deployments.
A Simple Analogy
Kubernetes is like an orchestra conductor. It manages many musicians (containers) playing together, ensuring they stay in sync, restart when needed, and scale up when the performance requires more sound. The conductor handles all the complex coordination so musicians focus on playing.
What Is Kubernetes?
Kubernetes (k8s) is an open-source container orchestration platform that automates deployment, scaling, and management of containerized applications across clusters of machines.
Why Use Kubernetes?
- Auto-scaling: Scale containers up or down based on demand
- Self-healing: Restart failed containers automatically
- Rolling updates: Deploy new versions without downtime
- Service discovery: Containers find each other automatically
- Load balancing: Distribute traffic across containers
- Multi-host deployment: Run across multiple machines
Core Concepts
| Concept | Purpose | |---------|---------| | Pod | Smallest deployable unit (usually one container) | | Deployment | Manages replicas and updates of pods | | Service | Exposes pods to internal/external traffic | | Node | Worker machine running pods | | Cluster | Set of nodes managed together |
Basic YAML Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app
image: my-app:1.0
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer
Deploy with:
kubectl apply -f deployment.yaml
Common kubectl Commands
kubectl get pods # List all pods
kubectl get services # List services
kubectl describe pod <pod-name> # Pod details
kubectl logs <pod-name> # View logs
kubectl exec -it <pod-name> bash # Access pod shell
kubectl scale deployment my-app --replicas=5 # Scale up
kubectl rollout status deployment/my-app # Check deployment status
Rolling Deployment Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: my-api:2.0
resources:
requests:
memory: "256Mi"
cpu: "250m"
Related Concepts to Explore
- Helm (package management for k8s)
- Ingress (advanced routing)
- StatefulSets (for stateful applications)
- ConfigMaps and Secrets (configuration management)
- Persistent volumes (data persistence)
Summary
Kubernetes automates container orchestration at scale. Master pods, deployments, and services to reliably run and manage containerized applications in production environments.