Di episode ini kita akan coba bahas GitOps untuk manage Kubernetes deployment menggunakan Git sebagai source of truth. Kita akan mempelajari GitOps principle, tool seperti ArgoCD dan Flux, dan best practice untuk implement GitOps workflow.

Catatan
Untuk kalian yang ingin membaca episode sebelumnya, bisa click thumbnail episode 39 di bawah ini
Di episode sebelumnya, kita menjelajahi Kustomize, yang menyediakan declarative approach untuk customize Kubernetes manifest. Sekarang kita akan mendalami GitOps, yang menggunakan Git sebagai source of truth untuk Kubernetes deployment.
Catatan: Disini saya akan menggunakan Kubernetes Cluster yang di install melalui K3s.
GitOps adalah serangkaian practice yang menggunakan Git repository sebagai single source of truth untuk declarative infrastructure dan application. Daripada manually apply kubectl command, Anda commit desired state Anda ke Git, dan automated tool synchronize cluster Anda untuk match state tersebut. Pikirkan GitOps seperti Infrastructure as Code untuk Kubernetes - seluruh cluster configuration Anda live di Git.
GitOps didasarkan pada empat core principle:
1. Declarative
Seluruh system Anda dijelaskan secara declarative di Git.
2. Versioned dan Immutable
Semua change tracked di Git dengan full history.
3. Pulled Automatically
Automated tool pull change dari Git dan apply ke cluster.
4. Continuously Reconciled
Cluster state continuously dibandingkan dengan Git dan di-reconcile.
1. Single Source of Truth
Git adalah authoritative source untuk semua cluster state.
2. Audit Trail
Setiap change tracked dengan commit history.
3. Easy Rollback
Revert ke any previous state dengan revert Git commit.
4. Collaboration
Tim collaborate melalui pull request dan code review.
5. Automation
Continuous deployment tanpa manual intervention.
6. Disaster Recovery
Recreate entire cluster dari Git.
Traditional vs GitOpsDeveloper → kubectl apply → ClusterManual, error-prone, no audit trail.
Developer → Git Commit → GitOps Tool → ClusterAutomated, auditable, reproducible.
ArgoCD adalah declarative, GitOps continuous delivery tool untuk Kubernetes.
Installation
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yamlAccess ArgoCD UI
kubectl port-forward svc/argocd-server -n argocd 8080:443Get Initial Password
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -dFlux adalah tool untuk keep Kubernetes cluster di sync dengan source dari configuration.
Installation
curl -s https://fluxcd.io/install.sh | sudo bash
flux installBootstrap dari Git
flux bootstrap github \
--owner=your-username \
--repository=fleet-infra \
--branch=main \
--path=./clusters/my-cluster \
--personalgitops-repo/
├── clusters/
│ ├── dev/
│ │ ├── kustomization.yaml
│ │ └── apps/
│ ├── staging/
│ │ ├── kustomization.yaml
│ │ └── apps/
│ └── production/
│ ├── kustomization.yaml
│ └── apps/
├── apps/
│ ├── web-app/
│ │ ├── base/
│ │ └── overlays/
│ └── api-app/
│ ├── base/
│ └── overlays/
└── infrastructure/
├── networking/
├── storage/
└── monitoring/apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../apps/web-app/overlays/production
- ../../apps/api-app/overlays/production
- ../../infrastructure/monitoringapiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../../base
namespace: production
replicas:
- name: web-app
count: 3
images:
- name: web-app
newTag: "v1.0.0"
commonLabels:
environment: productionapiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: web-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/your-org/gitops-repo
targetRevision: main
path: apps/web-app/overlays/production
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=trueapiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: full-stack
namespace: argocd
spec:
project: default
sources:
- repoURL: https://github.com/your-org/gitops-repo
targetRevision: main
path: apps/web-app/overlays/production
- repoURL: https://github.com/your-org/gitops-repo
targetRevision: main
path: apps/api-app/overlays/production
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: trueapiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: web-app
namespace: flux-system
spec:
interval: 10m
sourceRef:
kind: GitRepository
name: gitops-repo
path: ./apps/web-app/overlays/production
prune: true
wait: trueapiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
name: gitops-repo
namespace: flux-system
spec:
interval: 1m
url: https://github.com/your-org/gitops-repo
ref:
branch: main
secretRef:
name: git-credentials# Edit deployment
vim apps/web-app/overlays/production/kustomization.yaml
# Commit change
git add apps/web-app/overlays/production/kustomization.yaml
git commit -m "Increase web-app replicas to 5"
git push origin mainArgoCD atau Flux automatically detect Git commit.
# ArgoCD sync automatically
argocd app sync web-app
# Atau Flux reconcile automatically
flux reconcile kustomization web-appCluster di-update untuk match Git state.
git checkout -b feature/increase-replicas# Update configuration
vim apps/web-app/overlays/production/kustomization.yamlgit push origin feature/increase-replicas
# Create PR di GitHubTim review change di PR.
# After approval, merge PR
git merge feature/increase-replicas
git push origin mainGitOps tool automatically apply change ke cluster.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../apps/web-app/overlays/dev
- ../../apps/api-app/overlays/dev
namespace: devapiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../apps/web-app/overlays/production
- ../../apps/api-app/overlays/production
namespace: productionapiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: dev-cluster
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/your-org/gitops-repo
targetRevision: main
path: clusters/dev
destination:
server: https://kubernetes.default.svc
syncPolicy:
automated:
prune: true
selfHeal: trueapiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: prod-cluster
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/your-org/gitops-repo
targetRevision: main
path: clusters/production
destination:
server: https://kubernetes.default.svc
syncPolicy:
automated:
prune: true
selfHeal: trueProblem: Beberapa change applied manually, yang lain melalui GitOps.
# JANGAN LAKUKAN INI - Manual change bypass GitOps
kubectl apply -f deployment.yaml
kubectl set image deployment/web-app web-app=myapp:2.0Solusi: Semua change melalui Git:
# Update Git
vim apps/web-app/overlays/production/kustomization.yaml
git commit -m "Update image ke 2.0"
git pushProblem: Siapa pun dapat push langsung ke main.
Solusi: Require pull request dan review:
# Require pull request review
# Require status check untuk pass
# Require branch untuk up to dateProblem: Sensitive data exposed di Git.
# JANGAN LAKUKAN INI - Secret di Git
apiVersion: v1
kind: Secret
metadata:
name: db-secret
data:
password: c2VjcmV0MTIz # base64 encodedSolusi: Gunakan secret management tool:
# Gunakan ArgoCD Sealed Secret atau External Secret Operator
# Store secret di external vaultProblem: Broken configuration deployed ke production.
Solusi: Test sebelum merge:
# Validate YAML
kustomize build apps/web-app/overlays/production
# Run test
kubectl apply -k apps/web-app/overlays/production --dry-run=clientProblem: Cluster drift dari Git state.
Solusi: Monitor sync status:
# Check ArgoCD sync status
argocd app get web-app
# Check Flux reconciliation
flux get kustomization web-app# Jangan pernah push langsung ke main
git checkout -b feature/my-change
# Make change
git push origin feature/my-change
# Create PR, get review, mergegitops-repo/
├── clusters/
├── apps/
└── infrastructure/apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-rbac-cm
namespace: argocd
data:
policy.default: role:readonly
policy.csv: |
p, role:admin, applications, *, */*, allow
p, role:dev, applications, get, dev/*, allow
g, developers, role:dev# Monitor sync status
argocd app list
# Check untuk drift
argocd app diff web-app
# Set up alert untuk sync failuregitops-repo/
├── infrastructure/
│ ├── networking/
│ ├── storage/
│ └── monitoring/
├── apps/
└── clusters/# GitOps Repository
## Structure
- `clusters/` - Cluster configuration
- `apps/` - Application configuration
- `infrastructure/` - Infrastructure configuration
## Workflow
1. Create feature branch
2. Make change
3. Create pull request
4. Get review
5. Merge ke main
6. GitOps tool sync automatically| Aspek | GitOps | Traditional |
|---|---|---|
| Source of Truth | Git | Manual command |
| Audit Trail | Full Git history | Limited log |
| Rollback | Git revert | Manual |
| Collaboration | Pull request | Ad-hoc |
| Automation | Continuous | Manual |
| Disaster Recovery | Git clone | Manual restore |
| Compliance | Auditable | Difficult |
# Check application status
argocd app list
argocd app get web-app
# Check sync status
argocd app sync web-app
# View application detail
argocd app info web-app# Check reconciliation status
flux get kustomization
flux get helmrelease
# Check source status
flux get source git
# View reconciliation log
flux logs --all-namespaces --followPada episode 40 ini, kita telah membahas GitOps di Kubernetes secara mendalam. Kita sudah belajar GitOps principle, tool seperti ArgoCD dan Flux, dan best practice untuk implement GitOps workflow.
Key takeaway:
GitOps transform bagaimana Anda manage Kubernetes deployment dengan membuat Git single source of truth dan automating synchronization.
Catatan
Untuk kalian yang ingin melanjutkan ke episode selanjutnya, bisa click thumbnail episode 41 di bawah ini