Kubernetes CD with Flux

Deploying resources to a cluster is one of the most basic tasks in using Kubernetes. There are a lot of tools out there designed to help with this task, each with their own advantages and drawbacks.

Flux CD, developed by Weaveworks, is a tool that tries to bring a Gitops workflow to Kubernetes operations in a relatively simple way. In my opinion it does this rather well, though it can be somewhat challenging to get the service up and running in your cluster. In this article I’ll try to explain Flux’s functionality and give a short tutorial in installing it in a Kubernetes cluster.

How it works

is fairly simple. It makes it so that you don’t need to give production credentials to your CI tool, and puts the border between CI and CD at the Docker repository.

First, Flux lives inside a Kubernetes cluster and has permissions to manage resources in that cluster. There are no commands coming in from outside.

You provide Flux with the URI of a Git repo which contains Kubernetes manifests in YAML files. Flux needs to be able to read and write to this repo, and will apply whatever manifests are found in it to the cluster Flux is running in.

Whenever you change or add a manifest and commit the change to Git, Flux will notice the change and apply it to the cluster.

In addition to this, you can annotate your Kubernetes deployments to indicate to Flux that it should keep the deployment’s Docker image up to date with the Docker repository from which the image hails. You can even specify image tag regex so that Flux will only install new images which match the given pattern, eg: only update the deployment with new images whose tags match “develop-.*”.

  1. Flux is running in a Kubernetes cluster, watching a Git repo that has two YAML files defining resources for app-1 and app-2.
  2. The manifest for app-1 contains an annotation indicating that Flux should install any new images that are pushed to app-1’s Docker repository.
  3. Flux runs kubectl apply -f sync/ on the Git repository containing the manifests. The resources are now running in the cluster as specified in the Git repo.
  4. The CI pipeline for app-1 pushes a new image to its Docker repository.
  5. Flux finds out that there’s a new image for app-1.
  6. Flux runs kubectl patch on app-1’s deployment, changing the container image to this new one.
  7. Finally, Flux modifies the image tag to the new one in the Git repository, and commits and pushes the change.

This hopefully illustrates how Flux can be helpful for managing your pile of Kubernetes manifests and rolling out new versions of your app. Now let’s look at how to set it up in Kubernetes.

Installing Flux

First let’s give Flux its own namespace, as well as cluster wide permissions. We’re going to give it free reign in the cluster, but you could just as easily limit it to a single namespace.

Now any pod in the flux namespace that is given the service account named flux will get admin access to the entire cluster. Now we’ll define memcached, a dependency of Flux which caches container image to speed things up.

Now Flux is going to need an SSH known_hosts file to help it recognize the Git server to which it’s going to be connecting. In the following command replace github.com with whichever Git server you’ll be using: ssh-keyscan github.com . Use the results of this in the following manifest, replacing the line starting with github.com .

Let’s also create an empty secret for Flux to use:

Now let’s finish off by defining the Flux daemon itself:

Make sure you change the --git-url option to the SSH URI of the Git repo you’re using, and the --git-branch to your chosen branch. --git-path tells Flux to only look for manifests under the given path in the Git repo. The --git-user and --git-email are relevant for when Flux commits changes to the Git repo. I’ve set the poll and sync intervals to one minute each, ensuring that Flux will be pretty quick to pick up new changes.

Take all these manifests and apply them to your cluster with kubectl apply -f flux-init/ (wherever your files are).

Check that both Flux and memcached are running and healthy with kubectl -n flux get pods .

Giving Flux permissions on your Git repo

Now that Flux is up and running in your cluster, you’ll need to allow it to read/write to your Git repo. If you don’t plan on using the continuous deployment feature, you won’t need the write part of that, but it will at least need to be able to check out the repo over SSH.

To find Flux’s public key, we’ll need to get the log output of the container.

Now from the output, copy the public key. It’s between the quotation marks after identity.pub= .

Log into your Git server and go to the settings of the repository you’ll be using to sync manifests. There should be a section to add public keys and permissions. Add the copied public key and give it write permissions (or read if you don’t want to use continuous deployment).

If you wait a couple of minutes and look at Flux’s log again you should be able to see that it’s successfully checked out the Git repo. If there are errors, you’ll need to do some troubleshooting. I’ve discovered it can be fairly finicky with the Git URL.

Synchronize resources between Git and Kubernetes

Let’s try committing a manifest to our Git repo, and seeing if Flux picks up on it. Add the following contents to a new file resource.yml and commit it to the sync directory of your Git repo.

Now wait a few minutes and run kubectl get ns . Does mytestns come up? If not, you have a problem, and may want to check Flux’s logs again. But if it does come up, you’ve successfully set up Flux!

Synchronize a deployment with a Docker repository

Let’s try out the final key feature of Flux. To do this, we’re going to need a Kubernetes deployment and a Docker image. So first I’m going to create a new image and push it to Docker hub. I’m using my own Docker account so you should switch out j18e with your own Docker username:

Now let’s create a deployment that uses this new image. Put the following contents into a new YAML file and check it into the sync directory of your Git repo:

Notice the two annotations in our new resource. The first tells Flux that it should be automating the deployment’s container images, and the second provides a pattern to match against the tags of any new images that get pushed to the repository. Currently it’s set up to take any new images, but you could just as easily set the tag to "glob:master-*" to make sure it only matches images whose tags begin with master- . This is the connection between your continuous integration server and Flux.

Wait a couple minutes and make sure the deployment has been applied with kubectl get deployment mynewapp -o yaml . You should be able to see the image mynewapp:0.1.1 listed in the output.

Now push a new version of your app to Docker hub. You don’t need to actually change the image; a new tag will do just fine:

Wait another couple of minutes and again run kubectl get deployment mynewapp -o yaml . Do you see the new tag in the deployment? You can look at Flux’s logs as well to see it picking up on the change.

Run git pull in your repo and see if Flux managed to commit the change to Git. If all of the above worked, you’ve successfully tested the main features of Flux and can start managing your operations environment using Gitops!

Cloud architect, Kubernetes enthusiast