Kubernetes (K8s) is the industry-standard container orchestration platform. This guide covers everything you need to get started with Kubernetes.
Core Kubernetes Concepts
1. Pods - The Smallest Deployable Unit
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-app-pod
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
ports:
- containerPort: 80
resources:
limits:
memory: "128Mi"
cpu: "500m"
requests:
memory: "64Mi"
cpu: "250m"
# Create pod
kubectl apply -f pod.yaml
# View pods
kubectl get pods
# Describe pod details
kubectl describe pod my-app-pod
# View logs
kubectl logs my-app-pod
2. Deployments - Managing Replicas
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:v1.0.0
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secret
key: url
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
3. Services - Exposing Applications
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer # or ClusterIP, NodePort
4. ConfigMaps and Secrets
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_ENV: production
LOG_LEVEL: info
MAX_CONNECTIONS: "100"
---
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: db-secret
type: Opaque
data:
password: cGFzc3dvcmQxMjM= # base64 encoded
url: cG9zdGdyZXM6Ly9sb2NhbGhvc3Q6NTQzMg==
5. Ingress - External Access
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
Essential kubectl Commands
# Cluster info
kubectl cluster-info
kubectl get nodes
# Deployments
kubectl get deployments
kubectl rollout status deployment/my-app
kubectl rollout history deployment/my-app
kubectl rollout undo deployment/my-app
# Scaling
kubectl scale deployment my-app --replicas=5
# Debugging
kubectl exec -it pod-name -- /bin/sh
kubectl port-forward pod-name 8080:80
kubectl top pods
kubectl top nodes
# Apply configurations
kubectl apply -f kubernetes/
kubectl delete -f kubernetes/
Helm Charts for Package Management
# Install Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# Add repository
helm repo add bitnami https://charts.bitnami.com/bitnami
# Install application
helm install my-release bitnami/postgresql
# Create your own chart
helm create my-chart
helm package my-chart
helm install my-release ./my-chart
Kubernetes mastery opens doors to cloud-native development. Start with these fundamentals and gradually explore advanced topics!