What is Kubernetes?
Kubernetes (K8s) is an open-source platform for automating deployment, scaling, and management of containerized applications.
Core Concepts
Pods
Smallest deployable unit in Kubernetes.
yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp
image: myapp:1.0
ports:
- containerPort: 3000
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"Deployments
Manages pod replicas and updates.
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:1.0
ports:
- containerPort: 3000Services
Exposes pods to network traffic.
yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- port: 80
targetPort: 3000
type: LoadBalancerConfigMaps and Secrets
Configuration Management
yaml
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: myapp-config
data:
NODE_ENV: "production"
API_URL: "https://api.example.com"
---
# Secret
apiVersion: v1
kind: Secret
metadata:
name: myapp-secret
type: Opaque
data:
DATABASE_URL: <base64-encoded>
API_KEY: <base64-encoded>Ingress
HTTP Routing
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myapp-service
port:
number: 80Scaling
Horizontal Pod Autoscaler
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: myapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80Namespaces
Resource Isolation
yaml
apiVersion: v1
kind: Namespace
metadata:
name: productionMonitoring
Health Checks
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
template:
spec:
containers:
- name: myapp
image: myapp:1.0
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 5Best Practices
- 1Use labels: Organize resources with labels
- 2Set resource limits: Prevent resource abuse
- 3Use namespaces: Separate environments
- 4Implement health checks: Ensure pod health
- 5Use ConfigMaps/Secrets: Separate config from code
- 6Rolling updates: Deploy without downtime
- 7Backup etcd: Regular backups of cluster state
Common Commands
bash
# Get pods
kubectl get pods
# Get services
kubectl get services
# Apply configuration
kubectl apply -f deployment.yaml
# Get logs
kubectl logs -f pod/mypod
# Exec into container
kubectl exec -it pod/mypod -- sh
# Scale deployment
kubectl scale deployment myapp --replicas=5
# Get pod details
kubectl describe pod mypodConclusion
Kubernetes provides powerful container orchestration. Start simple and evolve as your needs grow.