Shoulder.dev Logo Shoulder.dev

Advanced Features of FluxCD

FluxCD is an open-source GitOps tool for Kubernetes that enables declarative infrastructure and continuous delivery. In this example, we will explore advanced features of FluxCD: canary deployments and multi-cluster support.

Canary deployments allow you to gradually roll out a new version of your application to a small percentage of users while keeping the majority on the stable version. This approach helps minimize the risk of introducing new bugs or issues.

Multi-cluster support enables managing and synchronizing multiple Kubernetes clusters from a single Git repository. This feature is particularly useful for organizations with multiple environments or teams working on different projects.

First, let’s set up a canary deployment using FluxCD.

  1. Create a new Git branch for the canary release:
git checkout -b canary
  1. Update your application’s Kustomization file to include a new deployment with a smaller replica count:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- deployment.yaml

patchesStrategic:
- patch: |
spec:
replicas: 3
path: deployment-stable.yaml

- patch: |
spec:
replicas: 1
path: deployment-canary.yaml
  1. Create a new deployment file for the canary release:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-canary
spec:
selector:
matchLabels:
app: my-app
replicas: 1
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-registry/my-app:canary
  1. Commit and push the changes to the Git repository:
git add .
git commit -m "Add canary deployment"
git push origin canary
  1. Configure FluxCD to roll out the canary deployment gradually:
apiVersion: flux.tools/v1beta1
kind: GitRepository
metadata:
name: my-repo
spec:
url: https://github.com/my-org/my-repo.git
ref: canary
path: path/to/my/app

---

apiVersion: flux.tools/v1beta1
kind: Kustomization
metadata:
name: my-app
spec:
interval: 30s
syncPolicy:
automated:
canary:
branches:
- canary
percentage: 25

This configuration tells FluxCD to sync the canary branch every 30 seconds and roll out the new deployment to 25% of the traffic.

Now, let’s set up multi-cluster support.

  1. Create a new Git repository for the second cluster:
git clone https://github.com/my-org/my-repo.git my-repo-secondary
cd my-repo-secondary
  1. Update the Kustomization file to include a new namespace for the second cluster:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: my-namespace-secondary

resources:
- deployment.yaml

patchesStrategic:
- patch: |
spec:
replicas: 3
path: deployment-stable.yaml

- patch: |
spec:
replicas: 1
path: deployment-canary.yaml
  1. Create a new FluxCD configuration file to sync the second cluster:
apiVersion: flux.tools/v1beta1
kind: GitRepository
metadata:
name: my-repo-secondary
spec:
url: file:///path/to/my/repo-secondary
interval: 30s
syncPolicy:
automated: {}

---

apiVersion: flux.tools/v1beta1
kind: Kustomization
metadata:
name: my-app-secondary
spec:
interval: 30s
syncPolicy:
automated:
pr: master

This configuration tells FluxCD to sync the master branch of the secondary repository every 30 seconds.

  1. Commit and push the changes to the secondary Git repository:
git add .
git commit -m "Add configuration for secondary cluster"
git push origin master
  1. Configure FluxCD to manage both clusters:
apiVersion: flux.tools/v1beta1
kind: Flux
metadata:
name: my-flux
spec:
controllers:
- git:
repository:
url: https://github.com/my-org/my-repo.git
branch: canary
syncOptions:
interval: 30s
syncPolicy:
automated:
canary:
branches:
- canary
percentage: 25

- git:
repository:
url: file:///path/to/my/repo-secondary
interval: 30s
syncPolicy:
automated: {}

This configuration tells FluxCD to manage both the primary and secondary clusters using the respective Git repositories.

Tests to verify the answer:

  1. Verify that the canary deployment is rolled out gradually:
  • Check the number of replicas for the canary deployment.
  • Monitor the traffic to the canary deployment and ensure it represents 25% of the total traffic.
  1. Verify that the second cluster is synchronized:
  • Check the status of the deployments in the second cluster.
  • Monitor the logs of the FluxCD controller to ensure it is syncing the second cluster correctly.