Post

Azure AKS GitOps Multibranch Flux Setup with Kustomize

Learn how to set up multibranch GitOps on Azure using Flux and Kustomize for managing Kubernetes deployments across environments.

Azure AKS GitOps Multibranch Flux Setup with Kustomize

Flux operator is specifically made to function in this way if you are using a single Git repository with three branches (development, staging, and production), and you want Flux to reconcile only the environment whose branch changed. To setup multiple branches in the monorepo we need to:

  • Create one GitRepository per branch.
  • Create one Kustomization per environment, each pointing to the corresponding GitRepository and, thus the branch.
  • Flux will then only reconcile the Kustomization when its branch changes - not others.

One repo, three branches

  • Repo: https://github.com/your-org/my-app
  • Branches (dev, staging, prod)

Folder structure should look like this (not limited to have such folder structure):

  • dev branch -> app/dev/kustomization.yaml
  • staging branch -> app/staging/kustomization.yaml

Define a GitRepository per branch - create one GitRepsitory per environment/branch

1
2
3
4
5
6
7
8
9
10
11
# gitrepository-dev.yaml
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
    name: app-dev
    namespace: flux-system
spec:
    interval: 1m
    url: https://github.com/your-org/my-app
    ref:
        branch: dev
1
2
3
4
5
6
7
8
9
10
11
# gitrepository-staging.yaml
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
    name: app-dev
    namespace: flux-system
spec:
    interval: 1m
    url: https://github.com/your-org/my-app
    ref:
        branch: staging
1
2
3
4
5
6
7
8
9
10
11
# gitrepository-prod.yaml
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
    name: app-dev
    namespace: flux-system
spec:
    interval: 1m
    url: https://github.com/your-org/my-app
    ref:
        branch: prod

Define Kustomization per branch/environment

Create a Kustomization per environment that points to the corresponding Git branch and folder:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# kustomization-dev.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
    name: dev
    namespace: flux-system
spec:
    interval: 5m
    path: ./apps/dev
    prune: true
    sourceRef:
        kind: GitRepository
        name: app-dev
    targetNamespace: dev
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# kustomization-staging.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
    name: staging
    namespace: flux-system
spec:
    interval: 5m
    path: ./apps/staging
    prune: true
    sourceRef:
        kind: GitRepository
        name: app-staging
    targetNamespace: staging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# kustomization-prod.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
    name: prod
    namespace: flux-system
spec:
    interval: 5m
    path: ./apps/prod
    prune: true
    sourceRef:
        kind: GitRepository
        name: app-prod
    targetNamespace: prod

How Reconciliation Works

Flux works like this:

  • Wathches each GitRepository separately.
  • When only (e.g. staging) branch changes, Flux will detect a new commit on staging, and only the staging Kustomization will reconcile.
  • Dev and Prod Kustomizations will remain idle unless their respective branches changes.
  • This gives is full branch-based isolation of environments.
This post is licensed under CC BY 4.0 by the author.