πŸ”§ DevOps

GitOps

Git Operations

A method of declaratively managing infrastructure and applications using a Git repository as the single source of truth.

Detailed Explanation

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 4 Principles of GitOps

  • Declarative: Define the desired state of the system declaratively
  • Versioned: All changes are version-controlled in Git
  • Automatically Applied: Approved changes are automatically applied to the system
  • Continuously Reconciled: An agent continuously compares the actual state with the desired state and reconciles them

Push vs Pull Model

  • Push Model: The CI pipeline deploys directly to the cluster (traditional approach)
  • Pull Model: An agent inside the cluster fetches changes from Git and applies them (recommended for GitOps)

The Pull model is superior in terms of security because it does not expose cluster credentials externally.

Major GitOps Tools

  • ArgoCD: A GitOps CD tool with a powerful UI
  • Flux: A lightweight GitOps toolkit
  • Jenkins X: A Jenkins-based GitOps platform

Code Example

ArgoCD Application Definition

# 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 λ¬΄μ‹œ

ArgoCD ApplicationSet (Multi-Cluster)

# 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

GitOps Repository Structure Example

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

Environment-Specific Configuration with Kustomization

# 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

Practical Conversation Example

DevOps engineer
"Every time I deploy to production, I run kubectl apply manually, and mistakes are frequent. I can't track the deployment history either..."
Platform engineer
"Adopting GitOps will solve that. Store all K8s manifests in Git, and ArgoCD detects changes in Git and automatically applies them to the cluster. A deployment becomes a Git commit, and a rollback is done with just git revert."
DevOps engineer
"Then what happens if someone modifies things directly with kubectl?"
Platform engineer
"If you turn on the selfHeal option, ArgoCD automatically restores the cluster to the state in Git. Manual changes get overwritten quickly, so every change is forced to go through Git. That's the core of GitOps."

Cautions

Secret Management

Never store plaintext Secrets in Git. Use Sealed Secrets, SOPS, External Secrets Operator, Vault, etc.

Drift Detection

If selfHeal is turned off, manual changes are retained. Check the Sync Status in the ArgoCD UI regularly.

Separating CI/CD

Separate CI (build/test) from CD (deployment). Divide the roles so that CI builds images and CD updates the GitOps repository.

Beware of Prune

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.

Learn More