Continuous Deployment using Flux CD

Irtiza
3 min readSep 16, 2022

Flux is a set of continuous and progressive delivery solutions for Kubernetes that are open and extensible.

https://repository-images.githubusercontent.com/258469100/dc74e100-1d18-11eb-8bc8-dc315e8a133e
https://repository-images.githubusercontent.com/258469100/dc74e100-1d18-11eb-8bc8-dc315e8a133e

Overview

Flux is developed by keeping the GitOps notion in mind, and GitOps is a way of managing your infrastructure and applications so that the whole system is described declaratively and version controlled (most likely in a Git repository), and has an automated process that ensures that the deployed environment matches the state specified in a repository.

This story is about implementing a Continous Deployment solution keeping the following use cases in mind:

  1. Detect and Deploy changes in the application’s Kubernetes manifests repository.
  2. Detect and Deploy a new image tag from the application’s image registry.
  3. Generate an alert about the operation mentioned above on a provider, i.e, slack, google chat, etc.

Assumptions

In this story I am taking the following assumptions:

  1. A Kubernetes cluster is up and running.
  2. You are a code repository and image registry i.e. GitHub, GitLab, etc.
  3. You are using as a notification/alert provider, i.e. slack, google chat, etc.

Basic Concepts

Understand the following concepts before we proceed further:

  1. Sources.
  2. Reconciliation.
  3. Kustomization.
  4. Bootstrap.

Each one of the above concepts is explained in detail on this link.

Details

Lets get our hand dirty !

Flux Deployment

  1. Install flux-cli on your system. Binaries are available on the release page but make sure to install a version that is compatible with your k8s version.
  2. Check if it is working:
flux -v

3. Create an access token for your Git repository, it will be used by flux-cli for bootstrapping flux components on your k8s environment. Export it as an env:

export <GITLAB | GITHUB>_TOKEN=<enter the token here>

4. Bootstrap flux using flux-cli:

flux bootstrap <github | gitlab> \
--components-extra=image-reflector-controller,image-automation-controller \
--hostname=<url for you code repository, don't add repository name in url, i.e, https://github.com > \
--owner=<github-organization / gitlab groups> \
--repository=<name of manifests repository for flux components> \
--branch=<branch name> \
--path=<path for flux manifests in the repository, default is clusters/config> \
--token-auth \
--personal

Details about bootstrapping it in different environments are available on this link.

5. Flux components will be deployed in the flux-system namespace:

kubectl get pods -n flux-system

Pods for the following components must be in a running state:

a. Image Automation and Reflector Controller

b. Kustomize Controller

c. Notification Controller

d. Source Controller

If all the above components are in a running state, flux has been installed on your cluster successfully.

Application’s Continuous Deployment Resources

Now we will create manifests for our application that the above components will use to enable Continuous deployment. These manifests include:

Image Repository: it will be used to reconcile application’s image tag deployed on kubernetes with application’s container registry using on different policies, i.e, semvar, numerical etc

Git Repository: it will be used to reconcile application’s state on kubernetes with a repository where its kubernetes manifests are stored, i.e, deployment, service and ingress.

Alerts: it will be used to generate alerts on different flux alert providers

I have added a little bit of information about each file. These manifests are available on this repository.

Flux CD Manifests

One more thing that you need to do is add an Image Update Automation detection string in deployment.yaml manifest. It's going to look like this:

Verify if resources are created successfully:

  1. Verify GitRepository is in a Ready state:
$ kubectl get gitrepository -n default

2. Verify ImageRepository has scanned the tags successfully:

$ kubectl get imagerepository -n default

3. Verify alert is in a Ready state:

$ kubectl get alert -n default

Final Thoughts

I hope you would love this story and let me know if anything can be improved.

https://static.thenounproject.com/png/854669-200.png

--

--