DEV Community

Alex Aslam
Alex Aslam

Posted on

GitLab CI + Kubernetes: Auto-Scaling Magic & Effortless Deployments šŸš€

Hey there, DevOps warrior! šŸ‘‹ Let’s talk about a nightmare we’ve all had:

It’s 2 AM. Your app just went viral, and your servers are melting faster than a popsicle in July. You’re frantically scaling pods manually, praying your users don’t notice the 500 errors. Sound familiar?

What if you could automate everything—deployments, scaling, rollbacks—while sipping coffee? Enter GitLab CI/CD + Kubernetes: the dynamic duo that’ll turn you into the calmest engineer in the room.


Why GitLab CI + Kubernetes? (Spoiler: It’s Like DevOps Autopilot)

  • Zero Downtime Deployments: Ship updates while users happily browse.
  • Auto-Scaling: Kubernetes scales pods up/down based on traffic. No more midnight panic.
  • GitOps Bliss: Define infrastructure as code (IaC) and let GitLab manage the rest.

Let’s turn you into a Kubernetes CI/CD wizard.


Step 1: Connect GitLab to Your Kubernetes Cluster

A. Link Your Cluster

  1. In GitLab, go to Infrastructure > Kubernetes.
  2. Click Add Kubernetes Cluster and follow the prompts.
  3. Copy-paste your kubeconfig or let GitLab auto-generate one.

![Screenshot: GitLab Kubernetes cluster integration page]

B. Install GitLab’s Agent (Optional but Highly Recommended)

The GitLab Agent lets you:

  • Securely connect to your cluster.
  • Automatically sync manifests.
  • Monitor deployments from GitLab’s UI.
helm upgrade --install gitlab-agent gitlab/gitlab-agent \  
  --namespace gitlab-agent \  
  --set image.tag=v16.0.0 \  
  --set config.token=YOUR_AGENT_TOKEN  
Enter fullscreen mode Exit fullscreen mode

Step 2: Build a Killer Deployment Pipeline

Here’s a .gitlab-ci.yml pipeline that:

  1. Builds a Docker image.
  2. Deploys to Kubernetes.
  3. Auto-scales based on traffic.
stages:  
  - build  
  - deploy  

build:  
  stage: build  
  image: docker:24  
  services:  
    - docker:dind  
  script:  
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .  
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY  
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA  

deploy:  
  stage: deploy  
  image: bitnami/kubectl:latest  
  script:  
    - kubectl apply -f kubernetes/deployment.yaml  
    - kubectl apply -f kubernetes/hpa.yaml  # Apply Horizontal Pod Autoscaler  
  environment:  
    name: production  
    url: https://myapp.com  
Enter fullscreen mode Exit fullscreen mode

Step 3: Auto-Scaling 101 (Because Traffic Spikes Happen)

A. Horizontal Pod Autoscaler (HPA)

Add hpa.yaml to scale pods based on CPU/memory:

apiVersion: autoscaling/v2  
kind: HorizontalPodAutoscaler  
metadata:  
  name: myapp-hpa  
spec:  
  scaleTargetRef:  
    apiVersion: apps/v1  
    kind: Deployment  
    name: myapp  
  minReplicas: 2  
  maxReplicas: 10  
  metrics:  
    - type: Resource  
      resource:  
        name: cpu  
        target:  
          type: Utilization  
          averageUtilization: 50  
Enter fullscreen mode Exit fullscreen mode

B. Cluster Autoscaler

For cloud clusters (AWS/GCP), the Cluster Autoscaler automatically adds nodes when pods can’t schedule.


Pro Tips to Avoid Facepalms 🤦

  1. Test Rollbacks:
   rollback:  
     script:  
       - kubectl rollout undo deployment/myapp  
     when: manual  
Enter fullscreen mode Exit fullscreen mode
  1. Use GitLab Environments: Track deployments, view logs, and monitor directly in GitLab.
  2. Limit Permissions: Use RBAC to restrict GitLab’s access to only what it needs.

Real-World Example: How Acme Corp Saved Christmas

Acme Corp’s e-commerce site had a Black Friday traffic tsunami. Their old setup crashed. After switching to GitLab CI + Kubernetes:

  • Auto-scaling handled 10x traffic spikes.
  • Rollbacks fixed a bad deploy in 30 seconds.
  • GitLab’s monitoring spotted a memory leak before users did.

Advanced Tricks for CI/CD Nerds

  • Canary Deployments: Split traffic between old/new versions with Flagger + GitLab.
  • Spot Instances: Save $$$ by auto-scaling with AWS Spot or GCP Preemptible VMs.
  • ChatOps: Trigger deployments via Slack:
  deploy:  
    script:  
      - kubectl apply ...  
    rules:  
      - if: $CI_PIPELINE_SOURCE == "chat"  
Enter fullscreen mode Exit fullscreen mode

Your DevOps Superpower Awaits

GitLab CI + Kubernetes isn’t just a toolchain—it’s peace of mind. You’ll:

  • Deploy fearlessly (even on Fridays šŸ˜‰).
  • Sleep through traffic spikes.
  • Become the office hero.

Next Steps:

  1. Connect your cluster to GitLab.
  2. Steal the pipeline above.
  3. Go deploy something spectacular.

Hit a snag? Drop a comment below. Let’s debug together! šŸ› ļø

Now go automate ALL THE THINGS. šŸš€


P.S. Your future self (well-rested, stress-free, and sipping coffee) says: ā€œThank you.ā€ ā˜•

Top comments (0)