GitOps
Git Operations
A method of declaratively managing infrastructure and applications using a Git repository as the single source of truth.
Git Operations
A method of declaratively managing infrastructure and applications using a Git repository as the single source of truth.
GitOps is an operating method that uses a Git repository as the "Single Source of Truth" to manage infrastructure and applications declaratively. First proposed by Weaveworks in 2017, it is particularly effective in Kubernetes environments.
The Pull model is superior in terms of security because it does not expose cluster credentials externally.
# argocd-application.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
project: default
# μμ€ μ μ₯μ μ€μ
source:
repoURL: https://github.com/myorg/my-app-config.git
targetRevision: main
path: deploy/production
# Helm μ°¨νΈ μ¬μ© μ
# helm:
# valueFiles:
# - values-prod.yaml
# Kustomize μ¬μ© μ
# kustomize:
# namePrefix: prod-
# λμ ν΄λ¬μ€ν° λ° λ€μμ€νμ΄μ€
destination:
server: https://kubernetes.default.svc
namespace: production
# λκΈ°ν μ μ±
syncPolicy:
automated:
prune: true # Gitμμ μμ λ 리μμ€ μλ μ 리
selfHeal: true # μλ λ³κ²½ μλ 볡ꡬ
allowEmpty: false # λΉ Application λ°©μ§
syncOptions:
- CreateNamespace=true
- PruneLast=true
- ApplyOutOfSyncOnly=true
# μ¬μλ μ μ±
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
# ν¬μ€ 체ν¬
ignoreDifferences:
- group: apps
kind: Deployment
jsonPointers:
- /spec/replicas # HPA κ΄λ¦¬ replicas 무μ
# applicationset.yaml - μ¬λ¬ ν΄λ¬μ€ν°μ λμ λ°°ν¬
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: my-app-set
namespace: argocd
spec:
generators:
# ν΄λ¬μ€ν°λ³ λ°°ν¬
- clusters:
selector:
matchLabels:
env: production
values:
revision: main
- clusters:
selector:
matchLabels:
env: staging
values:
revision: develop
template:
metadata:
name: 'my-app-{{name}}'
spec:
project: default
source:
repoURL: https://github.com/myorg/my-app-config.git
targetRevision: '{{values.revision}}'
path: deploy/{{metadata.labels.env}}
destination:
server: '{{server}}'
namespace: my-app
syncPolicy:
automated:
prune: true
selfHeal: true
my-app-config/
βββ README.md
βββ base/ # κ³΅ν΅ λ¦¬μμ€
β βββ deployment.yaml
β βββ service.yaml
β βββ configmap.yaml
β βββ kustomization.yaml
βββ overlays/ # νκ²½λ³ μ€μ
β βββ development/
β β βββ kustomization.yaml
β β βββ replica-patch.yaml
β β βββ config-patch.yaml
β βββ staging/
β β βββ kustomization.yaml
β β βββ replica-patch.yaml
β βββ production/
β βββ kustomization.yaml
β βββ replica-patch.yaml
β βββ resource-patch.yaml
β βββ hpa.yaml
βββ argocd/ # ArgoCD Application μ μ
βββ dev-app.yaml
βββ staging-app.yaml
βββ prod-app.yaml
# overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: production
resources:
- ../../base
# μ΄λ―Έμ§ νκ·Έ μ€λ²λΌμ΄λ
images:
- name: myorg/my-app
newTag: v1.2.3
# νκ²½λ³ ν¨μΉ
patchesStrategicMerge:
- replica-patch.yaml
- resource-patch.yaml
# ConfigMap μμ±
configMapGenerator:
- name: app-config
literals:
- ENV=production
- LOG_LEVEL=info
# λΌλ²¨ μΆκ°
commonLabels:
environment: production
team: platform
Never store plaintext Secrets in Git. Use Sealed Secrets, SOPS, External Secrets Operator, Vault, etc.
If selfHeal is turned off, manual changes are retained. Check the Sync Status in the ArgoCD UI regularly.
Separate CI (build/test) from CD (deployment). Divide the roles so that CI builds images and CD updates the GitOps repository.
When prune: true is set, resources deleted from Git are also deleted from the cluster. If you delete one by mistake, production services may be interrupted.