Back to Blog

CI/CD for Kubernetes: From Zero to Production in 30 Minutes

August 5, 2024
15 min read

Setting up CI/CD for Kubernetes doesn't have to be complex. In this guide, we'll build a complete pipeline that automatically deploys your applications from Git commits to production. No complicated scripts, no manual steps.

What You'll Build

Automated Builds

Every commit triggers builds and tests

GitOps Deployment

Git as single source of truth

Security Scanning

Automated vulnerability checks

Instant Rollbacks

One-click recovery from issues

Step 1: Repository Structure

Start with a clean repository structure that separates concerns:

my-app/
├── src/                    # Application source code
├── Dockerfile             # Container definition
├── k8s/                   # Kubernetes manifests
│   ├── base/             # Base configurations
│   │   ├── deployment.yaml
│   │   ├── service.yaml
│   │   └── kustomization.yaml
│   └── overlays/         # Environment-specific configs
│       ├── staging/
│       └── production/
└── .github/              # CI/CD workflows
    └── workflows/
        ├── build.yaml
        └── deploy.yaml

💡 Pro Tip: Using Kustomize allows you to maintain a DRY configuration while customizing for different environments without templating.

Step 2: Configure CI Pipeline

Here's a complete GitHub Actions workflow that builds, tests, and pushes container images:

name: Build and Push

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
      
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2
      
    - name: Log in to Container Registry
      uses: docker/login-action@v2
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
        
    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v4
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=ref,event=pr
          type=sha,prefix={{branch}}-
          
    - name: Build and push Docker image
      uses: docker/build-push-action@v4
      with:
        context: .
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max

Ready to automate your Kubernetes deployments?

KTL.AI provides pre-configured CI/CD pipelines, GitOps integration, and automated security scanning out of the box. Deploy with confidence from day one.