Docker & Kubernetes: Complete DevOps Pipeline Setup

8 januari 2025 · CodeMatic Team

Docker and Kubernetes DevOps

Modern software development requires robust DevOps practices. Docker and Kubernetes have become the industry standard for containerization and orchestration. This guide will walk you through building a complete, production-ready CI/CD pipeline.

Why Docker and Kubernetes?

Docker provides consistent environments from development to production, while Kubernetes offers:

  • Automatic scaling based on demand
  • Self-healing capabilities
  • Rolling updates with zero downtime
  • Resource management and optimization
  • Multi-cloud portability

Docker Best Practices

Multi-Stage Builds

Use multi-stage builds to create smaller, more secure images. Separate build dependencies from runtime dependencies, reducing image size by up to 80%.

FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:18-alpine AS runtime
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]

Layer Caching

Order Dockerfile instructions from least to most frequently changing. Copy package files before source code to leverage Docker's layer caching, dramatically speeding up builds.

Security Hardening

Run containers as non-root users, scan images for vulnerabilities, and use minimal base images (Alpine Linux). Regularly update base images to patch security vulnerabilities.

Kubernetes Architecture

Deployments and ReplicaSets

Use Deployments to manage your application lifecycle. They provide:

  • Rolling updates and rollbacks
  • Desired state management
  • Scaling capabilities

Services and Ingress

Services provide stable network endpoints for pods. Use ClusterIP for internal communication, NodePort or LoadBalancer for external access, and Ingress for HTTP/HTTPS routing with SSL termination.

CI/CD Pipeline Setup

GitHub Actions Workflow

A typical CI/CD pipeline includes:

  1. Code checkout and dependency installation
  2. Running tests (unit, integration, E2E)
  3. Building Docker images
  4. Pushing to container registry
  5. Deploying to Kubernetes
  6. Running smoke tests

Blue-Green Deployments

Implement blue-green deployments in Kubernetes for zero-downtime updates. Run two identical production environments, switch traffic between them, and keep the previous version ready for instant rollback.

Monitoring and Observability

Essential monitoring tools for Kubernetes:

  • Prometheus: Metrics collection and alerting
  • Grafana: Visualization and dashboards
  • ELK Stack: Log aggregation and analysis
  • Jaeger: Distributed tracing

Resource Management

Properly configure resource requests and limits:

  • Requests: Guaranteed resources for the container
  • Limits: Maximum resources a container can use
  • Use Horizontal Pod Autoscaler for automatic scaling
  • Implement Vertical Pod Autoscaler for right-sizing

Real-World Implementation

We implemented a complete Docker/Kubernetes pipeline for a SaaS platform serving 100,000+ users. The results:

  • 99.9% uptime with automatic failover
  • 50% reduction in infrastructure costs
  • Deployment time reduced from 2 hours to 5 minutes
  • Automatic scaling handles 10x traffic spikes

Conclusion

Docker and Kubernetes form the foundation of modern DevOps practices. By following these best practices and implementing a complete CI/CD pipeline, you can achieve faster deployments, better reliability, and significant cost savings. Start with the basics and gradually adopt more advanced features as your needs grow.