
Module 7 Lesson 4: Kubernetes Integration
Master the cloud. Learn how to connect your GitLab project to a Kubernetes cluster and use the 'GitLab Agent' to automate secure deployments to K8s.
Module 7 Lesson 4: Kubernetes Integration
Kubernetes is the ultimate destination for Docker containers (Review Docker Module 12). GitLab has a special tool called the GitLab Agent for Kubernetes that makes this connection seamless.
1. What is the GitLab Agent?
In the "Old way," you gave GitLab your Kubernetes "Keys" so it could push code. This was a security risk. The New Way: You install a small agent inside your Kubernetes cluster. The agent reaches out to GitLab and says: "Hey, is there a new image ready? I'll pull it and update the cluster."
- This is called Pull-based GitOps.
2. Setting Up the Agent
- In GitLab, go to Operate -> Kubernetes clusters.
- Click Connect a cluster.
- Name your agent and copy the provided command.
- Run that command on your Kubernetes cluster (
kubectl apply...).
3. Deploying via CI/CD (The Push Way)
If you prefer the standard CI/CD way, GitLab provides a pre-configured "Context" that allows your pipeline to run kubectl commands without manual login.
deploy_to_k8s:
stage: deploy
image: line/kubectl
script:
- kubectl config use-context path/to/my-agent:agent-name
- kubectl set image deployment/myapp web=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
4. The Kubernetes Dashboard
Once connected, you can view your cluster's health, pods, and logs directly inside the GitLab UI. You don't need to be a "Kubernetes Expert" to see if your app is crashing!
Exercise: The Cluster Connection
- Research: What is the difference between a Certificate-based connection and the GitLab Agent? (Hint: One is being deprecated).
- If you have a local cluster (like Minikube), try to install the GitLab Agent.
- Why is "Pull-based" deployment safer than "Push-based" for a production database?
- Search: What is Auto DevOps, and how does it use Kubernetes to build, test, and deploy an app with zero configuration?
Summary
Kubernetes integration is the "Final Boss" of DevOps. By connecting GitLab to a cluster, you move from managing "Servers" to managing "States," allowing your application to scale and self-heal in the cloud automatically.
Next Lesson: Final optimization: Optimizing Docker builds (Kaniko vs DinD).