Kubernetes orchestrates containerized applications at scale. Here's what developers need to know to deploy and manage applications effectively.
Core Concepts#
Pod: Smallest deployable unit, one or more containers
Service: Stable network endpoint for pods
Deployment: Manages pod replicas and updates
ConfigMap: External configuration
Secret: Sensitive data storage
Namespace: Logical cluster partitioning
Deployment#
1# deployment.yaml
2apiVersion: apps/v1
3kind: Deployment
4metadata:
5 name: api-server
6 labels:
7 app: api-server
8spec:
9 replicas: 3
10 selector:
11 matchLabels:
12 app: api-server
13 template:
14 metadata:
15 labels:
16 app: api-server
17 spec:
18 containers:
19 - name: api
20 image: myregistry/api-server:1.0.0
21 ports:
22 - containerPort: 3000
23 env:
24 - name: NODE_ENV
25 value: production
26 - name: DATABASE_URL
27 valueFrom:
28 secretKeyRef:
29 name: api-secrets
30 key: database-url
31 resources:
32 requests:
33 memory: "256Mi"
34 cpu: "250m"
35 limits:
36 memory: "512Mi"
37 cpu: "500m"
38 livenessProbe:
39 httpGet:
40 path: /health
41 port: 3000
42 initialDelaySeconds: 10
43 periodSeconds: 10
44 readinessProbe:
45 httpGet:
46 path: /ready
47 port: 3000
48 initialDelaySeconds: 5
49 periodSeconds: 5Service#
1# service.yaml
2apiVersion: v1
3kind: Service
4metadata:
5 name: api-server
6spec:
7 selector:
8 app: api-server
9 ports:
10 - protocol: TCP
11 port: 80
12 targetPort: 3000
13 type: ClusterIP
14
15---
16# External access via LoadBalancer
17apiVersion: v1
18kind: Service
19metadata:
20 name: api-server-external
21spec:
22 selector:
23 app: api-server
24 ports:
25 - protocol: TCP
26 port: 80
27 targetPort: 3000
28 type: LoadBalancerIngress#
1# ingress.yaml
2apiVersion: networking.k8s.io/v1
3kind: Ingress
4metadata:
5 name: api-ingress
6 annotations:
7 kubernetes.io/ingress.class: nginx
8 cert-manager.io/cluster-issuer: letsencrypt-prod
9spec:
10 tls:
11 - hosts:
12 - api.example.com
13 secretName: api-tls
14 rules:
15 - host: api.example.com
16 http:
17 paths:
18 - path: /
19 pathType: Prefix
20 backend:
21 service:
22 name: api-server
23 port:
24 number: 80ConfigMap and Secrets#
1# configmap.yaml
2apiVersion: v1
3kind: ConfigMap
4metadata:
5 name: api-config
6data:
7 LOG_LEVEL: info
8 CACHE_TTL: "300"
9 FEATURE_FLAGS: |
10 {
11 "newUI": true,
12 "betaFeatures": false
13 }
14
15---
16# secret.yaml
17apiVersion: v1
18kind: Secret
19metadata:
20 name: api-secrets
21type: Opaque
22stringData:
23 database-url: postgresql://user:pass@host:5432/db
24 jwt-secret: your-secret-key
25 api-key: external-api-key1# Using in deployment
2spec:
3 containers:
4 - name: api
5 envFrom:
6 - configMapRef:
7 name: api-config
8 - secretRef:
9 name: api-secrets
10 # Or individual values
11 env:
12 - name: DATABASE_URL
13 valueFrom:
14 secretKeyRef:
15 name: api-secrets
16 key: database-urlHorizontal Pod Autoscaler#
1# hpa.yaml
2apiVersion: autoscaling/v2
3kind: HorizontalPodAutoscaler
4metadata:
5 name: api-server-hpa
6spec:
7 scaleTargetRef:
8 apiVersion: apps/v1
9 kind: Deployment
10 name: api-server
11 minReplicas: 2
12 maxReplicas: 10
13 metrics:
14 - type: Resource
15 resource:
16 name: cpu
17 target:
18 type: Utilization
19 averageUtilization: 70
20 - type: Resource
21 resource:
22 name: memory
23 target:
24 type: Utilization
25 averageUtilization: 80Persistent Storage#
1# pvc.yaml
2apiVersion: v1
3kind: PersistentVolumeClaim
4metadata:
5 name: data-storage
6spec:
7 accessModes:
8 - ReadWriteOnce
9 resources:
10 requests:
11 storage: 10Gi
12 storageClassName: standard
13
14---
15# Using in deployment
16spec:
17 containers:
18 - name: api
19 volumeMounts:
20 - name: data
21 mountPath: /app/data
22 volumes:
23 - name: data
24 persistentVolumeClaim:
25 claimName: data-storageJobs and CronJobs#
1# cronjob.yaml
2apiVersion: batch/v1
3kind: CronJob
4metadata:
5 name: cleanup-job
6spec:
7 schedule: "0 2 * * *" # Daily at 2 AM
8 jobTemplate:
9 spec:
10 template:
11 spec:
12 containers:
13 - name: cleanup
14 image: myregistry/cleanup:1.0.0
15 env:
16 - name: DATABASE_URL
17 valueFrom:
18 secretKeyRef:
19 name: api-secrets
20 key: database-url
21 restartPolicy: OnFailure
22 backoffLimit: 3Rolling Updates#
1# deployment.yaml
2spec:
3 strategy:
4 type: RollingUpdate
5 rollingUpdate:
6 maxSurge: 1
7 maxUnavailable: 01# Update image
2kubectl set image deployment/api-server api=myregistry/api-server:1.1.0
3
4# Watch rollout
5kubectl rollout status deployment/api-server
6
7# Rollback if needed
8kubectl rollout undo deployment/api-server
9
10# View history
11kubectl rollout history deployment/api-serverResource Quotas#
1# quota.yaml
2apiVersion: v1
3kind: ResourceQuota
4metadata:
5 name: team-quota
6 namespace: team-a
7spec:
8 hard:
9 requests.cpu: "10"
10 requests.memory: 20Gi
11 limits.cpu: "20"
12 limits.memory: 40Gi
13 pods: "50"
14 services: "10"Network Policies#
1# network-policy.yaml
2apiVersion: networking.k8s.io/v1
3kind: NetworkPolicy
4metadata:
5 name: api-network-policy
6spec:
7 podSelector:
8 matchLabels:
9 app: api-server
10 policyTypes:
11 - Ingress
12 - Egress
13 ingress:
14 - from:
15 - podSelector:
16 matchLabels:
17 app: frontend
18 ports:
19 - protocol: TCP
20 port: 3000
21 egress:
22 - to:
23 - podSelector:
24 matchLabels:
25 app: database
26 ports:
27 - protocol: TCP
28 port: 5432Useful Commands#
1# Get resources
2kubectl get pods
3kubectl get services
4kubectl get deployments
5
6# Describe resource
7kubectl describe pod <pod-name>
8
9# View logs
10kubectl logs <pod-name>
11kubectl logs -f <pod-name> # Follow
12kubectl logs <pod-name> -c <container> # Specific container
13
14# Execute in pod
15kubectl exec -it <pod-name> -- /bin/sh
16
17# Port forwarding
18kubectl port-forward <pod-name> 3000:3000
19kubectl port-forward svc/api-server 3000:80
20
21# Apply configuration
22kubectl apply -f deployment.yaml
23kubectl apply -f ./k8s/
24
25# Delete resources
26kubectl delete -f deployment.yaml
27kubectl delete pod <pod-name>Best Practices#
Resource Management:
✓ Set resource requests and limits
✓ Use namespaces for isolation
✓ Implement resource quotas
✓ Configure autoscaling
Reliability:
✓ Define liveness and readiness probes
✓ Use multiple replicas
✓ Configure pod disruption budgets
✓ Implement graceful shutdown
Security:
✓ Use secrets for sensitive data
✓ Apply network policies
✓ Run as non-root
✓ Keep images updated
Operations:
✓ Use rolling updates
✓ Tag images with versions
✓ Monitor and alert
✓ Document configurations
Conclusion#
Kubernetes provides powerful orchestration for containerized applications. Focus on proper resource configuration, health checks, and security. Start simple with deployments and services, then add complexity as needed.