302 β€” Deploy to Kubernetes from CI/CD

Advanced

Automate Kubernetes deployments from GitHub Actions. Build images, push to registry, and deploy to Kubernetes clusters automatically on every commit.

Learning Objectives

1
Deploy to Kubernetes from CI/CD pipelines
2
Update deployments with new image tags
3
Implement kubectl apply from GitHub Actions
4
Configure secrets and config maps via CI
5
Monitor deployment rollouts automatically
Step 1

Prepare Kubernetes manifests

Create deployment and service YAML files.

Commands to Run

mkdir k8s-deploy && cd k8s-deploy
mkdir -p k8s
cat > k8s/deployment.yaml << 'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
  labels:
    app: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
      - name: web
        image: username/web-app:latest
        ports:
        - containerPort: 3000
        env:
        - name: NODE_ENV
          value: production
EOF
cat > k8s/service.yaml << 'EOF'
apiVersion: v1
kind: Service
metadata:
  name: web-app
spec:
  type: LoadBalancer
  selector:
    app: web-app
  ports:
  - port: 80
    targetPort: 3000
EOF
ls k8s/

What This Does

Standard Kubernetes manifests: Deployment (3 replicas) and LoadBalancer Service. Image will be updated by CI pipeline with actual commit tag.

Expected Outcome

Manifests created in k8s/ directory. Ready for kubectl apply and CI/CD automation.

Pro Tips

  • 1
    Keep manifests in k8s/ directory
  • 2
    Use specific image tags, not :latest in production
  • 3
    LoadBalancer requires cloud provider
  • 4
    Start with NodePort for local testing
  • 5
    Add resource limits in production
Was this step helpful?

All Steps (0 / 10 completed)