Isaac.

devops

Docker Kubernetes Deployment

Deploy Docker containers at scale with Kubernetes.

By Emem IsaacAugust 22, 20223 min read
#kubernetes#docker#orchestration#deployment#production
Share:

A Simple Analogy

Kubernetes is like an operations manager for containers. It schedules work, handles failures, scales up when busy, and ensures everything runs smoothly 24/7.


Why Kubernetes?

  • Orchestration: Manage hundreds of containers
  • Self-healing: Restart crashed containers
  • Auto-scaling: Scale based on demand
  • Rolling updates: Zero-downtime deployments
  • Service discovery: Auto DNS for containers

Basic Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
      - name: api
        image: myregistry.azurecr.io/myapi:latest
        ports:
        - containerPort: 80
        env:
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: connection-string
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /health
            port: 80
          initialDelaySeconds: 30
          periodSeconds: 10

Service for Load Balancing

apiVersion: v1
kind: Service
metadata:
  name: api-service
spec:
  type: LoadBalancer
  selector:
    app: api
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

ConfigMap and Secrets

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  log-level: "info"
  environment: "production"

---
apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
stringData:
  connection-string: "postgresql://user:password@db-service:5432/myapp"
  api-key: "secret-key-here"

StatefulSet for Databases

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: postgres-service
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:15
        ports:
        - containerPort: 5432
        volumeMounts:
        - name: postgres-storage
          mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
  - metadata:
      name: postgres-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi

Horizontal Pod Autoscaling

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80

Kubectl Commands

# Apply configuration
kubectl apply -f deployment.yaml

# View pods
kubectl get pods
kubectl describe pod api-deployment-xyz

# View services
kubectl get svc

# Scale manually
kubectl scale deployment api-deployment --replicas=5

# Update image
kubectl set image deployment/api-deployment api=myregistry.azurecr.io/myapi:v2

# Rollback
kubectl rollout undo deployment/api-deployment

# View logs
kubectl logs pod-name
kubectl logs -f pod-name  # Follow logs

Best Practices

  1. Set resource limits: Define CPU and memory
  2. Health checks: Liveness and readiness probes
  3. Security: Use RBAC and network policies
  4. Monitoring: Integrate Prometheus/Grafana
  5. GitOps: Store configs in Git

Related Concepts

  • Helm for templating
  • Ingress for routing
  • Network policies for security
  • RBAC for access control

Summary

Kubernetes automates container orchestration at scale. Define deployments, services, and scaling policies to build resilient, self-healing systems.

Share:

Written by Emem Isaac

Expert Software Engineer with 15+ years of experience building scalable enterprise applications. Specialized in ASP.NET Core, Azure, Docker, and modern web development. Passionate about sharing knowledge and helping developers grow.

Ready to Build Something Amazing?

Let's discuss your project and explore how my expertise can help you achieve your goals. Free consultation available.

💼 Trusted by 50+ companies worldwide | ⚡ Average response time: 24 hours