CHAPTER 16
Intermediate
CI/CD with Kubernetes
Updated: May 15, 2026
30 min read
# CHAPTER 16
CI/CD with Kubernetes
1. Introduction
In Chapter 15, we discussed the complexity of manually managing Load Balancers and routing traffic to perform advanced deployments like Blue-Green or Canary. While this is tedious on traditional servers, Kubernetes (K8s)—the undisputed king of container orchestration—was built specifically to handle these advanced deployment strategies natively. In this chapter, we will bridge the gap between our CI pipeline and a Kubernetes cluster. We will explore how to configure GitHub Actions to authenticate with K8s, update deployment manifests dynamically, and trigger "Rolling Updates" across massive fleets of containers seamlessly.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the integration points between a CI pipeline and a K8s cluster.
- Define a Kubernetes Deployment manifest.
-
Authenticate a CI runner with a Kubernetes API using
kubeconfig.
- Utilize CI tools to inject dynamic Image Tags into K8s manifests.
- Understand Kubernetes Native Rolling Updates.
3. Beginner-Friendly Explanation
Imagine a fleet of 50 delivery trucks (Containers) managed by an automated Dispatcher (Kubernetes).- The Old Way: You (the CI Pipeline) have to call all 50 drivers individually, tell them to pull over, change their cargo, and restart their trucks.
- The Kubernetes Way: You don't talk to the trucks. You talk exclusively to the Dispatcher. The CI Pipeline hands the Dispatcher a single piece of paper (a YAML manifest) that says: "I want all trucks to carry cargo Version 2.0." The Dispatcher looks at the paper, automatically pulls over one truck at a time, changes the cargo, verifies it runs, and then moves to the next truck until all 50 are updated.
Kubernetes abstracts away the complexity of the servers; your CI pipeline only has to update a single text file to trigger massive, highly-available deployments.
4. The K8s Deployment Manifest
Before we can deploy, we must define what we want Kubernetes to run. This is done via a YAML file (e.g.,deployment.yml) stored in our Git repository alongside our application code.
yaml
5. The CI/CD Kubernetes Workflow
The ultimate goal of a K8s CI/CD pipeline is to update theimage: line in the manifest above, and tell the cluster to apply the changes.
-
1.
Build & Push: The CI pipeline compiles the code and builds a Docker Image tagged with the unique Git commit hash (e.g.,
mycompany/myapp:a1b2c3d). It pushes this to Docker Hub.
-
2.
Update Manifest: The CI pipeline uses a tool (like
sedorkustomize) to rewrite thedeployment.ymlfile, changingimage: mycompany/myapp:latesttoimage: mycompany/myapp:a1b2c3d.
- 3. Authenticate: The CI pipeline securely loads the K8s cluster credentials.
-
4.
Deploy: The CI pipeline runs
kubectl apply -f deployment.yml. Kubernetes takes over and gracefully updates the running containers.
6. Mini Project: Deploy App to K8s Cluster
Let's build a GitHub Actions workflow that executes the K8s deployment phase.Step-by-Step Architecture Concept:
yaml
7. Real-World Scenarios
A gaming company relied on manual scripts to deploy server updates. During a major launch, an engineer ran the script, and all 100 game servers restarted simultaneously, disconnecting thousands of players. They migrated to Kubernetes and integrated it with GitLab CI. Because Kubernetes performs Rolling Updates by default, when the CI pipeline rankubectl apply, K8s intelligently updated only 10 servers at a time, waiting for them to report "Healthy" before moving to the next 10. The entire 100-server fleet was updated with zero downtime, and the CI pipeline managed the entire process just by updating a single YAML string.
8. Best Practices
- GitOps (ArgoCD): The pipeline above uses "Push-based" deployment (the CI runner pushes commands into the cluster). For massive enterprises, the gold standard is "Pull-based" deployment (GitOps). You install a tool like ArgoCD *inside* your cluster. Your CI pipeline ONLY updates the YAML file in GitHub. ArgoCD constantly watches GitHub; when it sees a change, the cluster pulls the new configuration itself. This means your CI pipeline never needs K8s admin credentials, massively increasing security.
9. Security Recommendations
-
Kubeconfig Vaulting: The
kubeconfigfile grants absolute administrator access to your entire Kubernetes cluster. It is the most dangerous secret your company owns. Never commit it to Git. If you use push-based deployments, it must be stored in a highly secure CI vault (like GitHub Secrets), and the CI runner must only have access to the specific Kubernetes Namespace it needs to deploy into, enforcing least privilege.
10. Troubleshooting Tips
-
ImagePullBackOff: The most common CI/K8s error. The CI pipeline will report
kubectl rollout statusfailed. If you investigate the cluster, you will seeImagePullBackOff. This means the pipeline successfully pushed the YAML to K8s, but K8s couldn't download the Docker image. This usually means your CI pipeline forgot to authenticate K8s to your private Docker Registry (via animagePullSecret), or the image name/tag in the YAML was misspelled.
11. Exercises
- 1. Explain the architectural difference between pushing code directly to traditional servers versus applying a YAML manifest to a Kubernetes API.
-
2.
What is the operational purpose of dynamically substituting the
latestimage tag with the Git commit hash (github.sha) during the CI pipeline?
12. FAQs
Q: Do I need to installkubectl on the GitHub runner?
A: No, GitHub Actions provides Ubuntu runners that already have kubectl, Docker, and the AWS CLI pre-installed, allowing you to run these commands instantly.
13. Interview Questions
- Q: Describe the integration architecture between a CI/CD pipeline and a Kubernetes cluster. How does the pipeline dictate which specific container image the cluster should pull and execute?
-
Q: Contrast "Push-based" Kubernetes deployments (using
kubectl applyin a CI runner) with "Pull-based" GitOps deployments (using ArgoCD). What is the primary security advantage of the GitOps model?