K3S: GitOps & CI/CD
Introduction to GitOps and Continuous Deployment
GitOps is the answer to that chaotic, last-minute “did anyone deploy the latest version?” question. It treats Git as the single source of truth, ensuring deployments are automated, consistent, and—most importantly—not reliant on someone’s memory.
What is GitOps, and why use it for Kubernetes deployments?
GitOps automates Kubernetes deployments by continuously syncing cluster configurations from a Git repository. This means fewer manual kubectl commands and more predictable deployments.
Difference between CI/CD and GitOps
| Feature | CI/CD | GitOps |
|---|---|---|
| Focus | Automating build/test/deploy pipelines | Managing infrastructure & application state via Git |
| Triggers | Push events, PRs, manual approvals | Git repository state changes |
| Deployment Tools | Jenkins, GitHub Actions, GitLab CI | ArgoCD, FluxCD |
Popular GitOps tools: ArgoCD, FluxCD
- ArgoCD: User-friendly, UI-based GitOps controller.
- FluxCD: Lightweight and native to Kubernetes.
Using ArgoCD for Continuous Deployment
ArgoCD lets you deploy, sync, and rollback applications with just a Git commit. Let’s get it up and running in K3s.
Installing ArgoCD in K3s
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yamlAccessing the ArgoCD UI
kubectl port-forward svc/argocd-server -n argocd 8080:443Now, visit https://localhost:8080 and log in.
Deploying an application using ArgoCD
argocd app create myapp --repo https://github.com/myrepo.git --path k8s-manifests --dest-server https://kubernetes.default.svc --dest-namespace defaultArgoCD will now sync your app automatically with the Git repo!
Integrating K3s with GitHub Actions and Jenkins
Automation is key. Let’s get GitHub Actions and Jenkins to deploy our applications on K3s.
Using GitHub Actions for Kubernetes Deployment
name: Deploy to K3s
on: push
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up kubectl
run: |
echo "${{ secrets.KUBECONFIG }}" | base64 --decode > kubeconfig.yaml
export KUBECONFIG=kubeconfig.yaml
- name: Apply Kubernetes manifests
run: kubectl apply -f k8s/This workflow triggers a deployment whenever changes are pushed.
Using Jenkins for Kubernetes Deployment
- Install the Kubernetes plugin in Jenkins.
- Use this Jenkinsfile:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/myrepo.git'
}
}
stage('Deploy to K3s') {
steps {
sh 'kubectl apply -f k8s/'
}
}
}
}Now Jenkins will deploy changes whenever the pipeline runs.
Automating Deployments using Helm and Kustomize
Using Helm for Package Management
Helm makes Kubernetes deployments repeatable and maintainable.
Installing Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bashDeploying an application using Helm
helm repo add myrepo https://charts.example.com/
helm install myapp myrepo/mychartUsing Kustomize for Kubernetes Configuration Management
Kustomize lets you manage configurations without duplicate YAML files.
Writing a kustomization.yaml file
resources:
- deployment.yaml
- service.yaml
patches:
- target:
kind: Deployment
name: myapp
patch: |-
- op: replace
path: /spec/replicas
value: 5Applying Kustomize
kubectl apply -k .Implementing Rollback and Version Control
Mistakes happen—let’s make sure we can roll back quickly.
Rolling back a deployment in Kubernetes
kubectl rollout undo deployment myapp-deploymentManaging versions in GitOps workflows
- Use branches for testing and approval before merging to production.
- Maintain tags and release versions to track deployments.
Best practices for handling failed deployments
- Always enable rollback policies in CI/CD pipelines.
- Implement health checks to detect issues early.
- Use progressive rollouts instead of deploying everything at once.
Hands-On Exercise
Time to put all this into action:
- Deploy an application using ArgoCD and sync it with Git.
- Set up GitHub Actions or Jenkins for CI/CD with K3s.
- Package an application using Helm and deploy it to K3s.
- Implement Kustomize for managing Kubernetes configurations.
Master this, and your deployments will be faster, safer, and way less stressful. 🚀