๐Ÿ”ง DevOps

Flux

Flux CD

A GitOps tool for Kubernetes. A CNCF project.

Detailed Explanation

Flux is a GitOps continuous delivery (CD) tool for Kubernetes. It was developed by Weaveworks and is now a CNCF graduated project. It automatically synchronizes the declarative configuration in a Git repository to the cluster, continuously maintaining the "desired state."

Key features

  • GitOps principles: Uses Git as the Single Source of Truth
  • Pull-based: The cluster fetches changes from Git and applies them (safer than the push approach)
  • Declarative configuration: Defines all deployment settings in YAML
  • Automatic reconciliation: Automatically restores the actual state if it differs from the desired state
  • Helm/Kustomize integration: Fully compatible with existing K8s packaging tools

Flux Components

  • Source Controller: Monitors Git and Helm repositories
  • Kustomize Controller: Applies Kustomization resources
  • Helm Controller: Manages HelmRelease
  • Notification Controller: Handles notifications and events
  • Image Automation Controller: Automatically updates container images

Code Example

GitRepository - Source definition

# git-repository.yaml
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 1m
  url: https://github.com/myorg/my-app-config
  ref:
    branch: main
  secretRef:
    name: github-token  # ํ”„๋ผ์ด๋น— ์ €์žฅ์†Œ์šฉ
---
# Secret for private repository
apiVersion: v1
kind: Secret
metadata:
  name: github-token
  namespace: flux-system
stringData:
  username: git
  password: ${GITHUB_TOKEN}

Kustomization - Deployment configuration

# kustomization.yaml - Flux Kustomization ๋ฆฌ์†Œ์Šค
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 10m
  targetNamespace: production
  sourceRef:
    kind: GitRepository
    name: my-app
  path: ./deploy/production
  prune: true  # Git์—์„œ ์‚ญ์ œ๋œ ๋ฆฌ์†Œ์Šค ์ž๋™ ์ •๋ฆฌ
  healthChecks:
    - apiVersion: apps/v1
      kind: Deployment
      name: my-app
      namespace: production
  timeout: 5m
  # ๋ณ€์ˆ˜ ์น˜ํ™˜
  postBuild:
    substitute:
      CLUSTER_NAME: prod-cluster
      ENVIRONMENT: production
    substituteFrom:
      - kind: ConfigMap
        name: cluster-config
---
# ์˜์กด์„ฑ ์žˆ๋Š” ๋ฐฐํฌ
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: my-app-db
  namespace: flux-system
spec:
  interval: 10m
  sourceRef:
    kind: GitRepository
    name: my-app
  path: ./deploy/database
  prune: true
---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: my-app-api
  namespace: flux-system
spec:
  dependsOn:
    - name: my-app-db  # DB๊ฐ€ ๋จผ์ € ๋ฐฐํฌ๋œ ํ›„ API ๋ฐฐํฌ
  interval: 10m
  sourceRef:
    kind: GitRepository
    name: my-app
  path: ./deploy/api
  prune: true

HelmRelease - Helm chart deployment

# helm-release.yaml
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: HelmRepository
metadata:
  name: bitnami
  namespace: flux-system
spec:
  interval: 1h
  url: https://charts.bitnami.com/bitnami
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: redis
  namespace: production
spec:
  interval: 10m
  chart:
    spec:
      chart: redis
      version: "17.x"
      sourceRef:
        kind: HelmRepository
        name: bitnami
        namespace: flux-system
  values:
    architecture: replication
    replica:
      replicaCount: 3
    auth:
      existingSecret: redis-password
  # ์—…๊ทธ๋ ˆ์ด๋“œ/๋กค๋ฐฑ ์„ค์ •
  upgrade:
    remediation:
      retries: 3
  rollback:
    cleanupOnFail: true

Automatic image updates

# image-automation.yaml
apiVersion: image.toolkit.fluxcd.io/v1beta1
kind: ImageRepository
metadata:
  name: my-app
  namespace: flux-system
spec:
  image: docker.io/myorg/my-app
  interval: 1m
---
apiVersion: image.toolkit.fluxcd.io/v1beta1
kind: ImagePolicy
metadata:
  name: my-app
  namespace: flux-system
spec:
  imageRepositoryRef:
    name: my-app
  policy:
    semver:
      range: ">=1.0.0"
---
apiVersion: image.toolkit.fluxcd.io/v1beta1
kind: ImageUpdateAutomation
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 1m
  sourceRef:
    kind: GitRepository
    name: my-app
  git:
    checkout:
      ref:
        branch: main
    commit:
      author:
        email: fluxbot@myorg.com
        name: Flux Bot
      messageTemplate: |
        Automated image update
        - {{range .Updated.Images}}{{println .}}{{end}}
    push:
      branch: main

Practical Conversation Example

DevOps engineer
"I'm trying to decide between ArgoCD and Flux. Both are GitOps tools, so..."
Senior Engineer
"ArgoCD has a powerful UI, so it's good for viewing deployment status visually, while Flux is lighter and CLI-centric. If you're going to use a Hub-Spoke pattern in a multi-cluster environment, Flux's Kustomization dependency management is convenient."
DevOps engineer
"Our team uses a lot of Helm charts. Does Flux support Helm well too?"
Senior Engineer
"Yes, you can manage Helm deployments declaratively with the HelmRelease CRD. If you store values.yaml in Git and change it, it upgrades automatically. Rollback is also automatic if you configure it in the spec."

Cautions

Caution with the Prune option

When `prune: true` is set, resources deleted from Git are also deleted from the cluster. If you delete a file by mistake, production resources may disappear, so be careful.

Secret management

Do not store plaintext Secrets in Git. Use Mozilla SOPS, Sealed Secrets, or an external secrets manager (Vault).

Sync interval

If the interval is too short, it can put load on the Git server, and if it is too long, deployment delays occur. In general, 1-5 minutes is appropriate.

Bootstrap order

Flux itself is also managed via GitOps, so plan the dependency order carefully during the initial bootstrap.

Learn More