Skip to content
ImagineWorld

GitOps with Flux for Edge Applications

node_idnode-book_edge-computing-systems-with-kubernetes_chapter-07-gitops-with-flux-for-edge-applications
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num7
chapter_titleGitOps with Flux for Edge Applications
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 7 — GitOps with Flux for Edge Applications

Section titled “Chapter 7 — GitOps with Flux for Edge Applications”

Previous chapters have already covered the basics of building your home lab using K3s. It’s time to implement simple use cases that you can use in edge computing. This chapter covers how to implement GitOps for your applications using Flux in edge computing environments, starting with the basic theory of GitOps and the necessary tools to manage a Git repository for deployments. Then, we will look at how to install Flux to implement a basic GitOps workflow for a demo application. This chapter includes how to automate an application deployment using a mono repository (monorepo) configuration, the Helm operator, and the image updater feature of Flux. Finally, we will end the chapter with the installation of basic monitoring dashboards in Flux, essential troubleshooting commands for Flux, and how to uninstall Flux.

In this chapter, we’re going to cover the following main topics:

  • Implementing GitOps for edge computing
  • Flux and its architecture
  • Designing GitOps with Flux for edge applications
  • Building your container image with GitHub Actions
  • Installing and configuring Flux for GitOps
  • Troubleshooting Flux installations
  • Installing Flux monitoring dashboards
  • Uninstalling Flux

In this chapter, to implement GitOps using Flux, you will need the following:

  • Three single node K3s clusters using a device with an ARM processor such as a Raspberry Pi.
  • Previous experience with Git.
  • GitHub repository and its token; you also need some basic experience using Git.
  • Docker Hub account to push new image releases of your application.

With this, you are ready for this first use case to implement GitOps at the edge using Flux. So, let’s get started.

For more detail and code snippets, check out this resource on GitHub: https://github.com/PacktPublishing/Edge-Computing-Systems-with-Kubernetes/tree/main/ch7

To start this topic, let’s get started with the concept of GitOps. The https://www.gitops.tech/ website states: “ GitOps is a way of implementing Continuous Deployment for cloud native applications. It focuses on a developer-centric experience when operating infrastructure, by using tools developers are already familiar with, including Git and Continuous Deployment tools.” This means that GitOps helps you with your continuous deployment (CD) in general. In software engineering, it is common to refer to continuous deployment and continuous delivery with the CD acronym.

Also, the GitLab page https://about.gitlab.com/topics/gitops mentions that GitOps contains the following basic components:

  • Infrastructure as code (IaC): This refers to a declarative way to provide infrastructure or deployments for your applications.
  • Merge requests or pull requests (PRs): A way to manage infrastructure or application code updates across multiple changes and collaborators.
  • Source code management (SCM): Systems such as Git enable merge request- or pull request-based workflows and a mechanism to manage this, usually using a Git repository. In this way, a team can have an approval-and-review mechanism to apply changes.
  • Continuous integration and continuous delivery (CI/CD): CI and CD include, by nature, all the processes of building, checking, and deploying applications and changes to those software applications. GitOps is used to automate CD for a cloud native application.

In this chapter, you will find tools that provide mechanisms for CI/CD automation pipelines. Let’s pay attention to the following diagram that shows a summary of how GitOps works:

Figure 7.1 – GitOps

The basics of starting with GitOps are outlined here, as depicted in Figure 7.1:

  1. Commit code to the source: Here, a developer makes changes and pushes their changes to a source or repository—for example, a Git repository hosted at GitHub.
  2. Synchronize changes: A GitOps tool such as, for example, Flux periodically detects changes in the source.
  3. Provision or reconcile changes: Once the GitOps tool detects changes, it aims to automate the process of updating a deployment based on the declarative configuration changes found. This could include processes to modify, such as changing configurations, updating the container image for an application using containers, and so on. Sometimes, if a resource or deployment doesn’t exist, you have to provision resources or reconcile these, comparing changes. This means that a GitOps tool regularly works with declarative definitions to reflect the state of your infrastructure or application.

Finally, the user will see changes when accessing their application, and you can also add some additional processes such as notifications if a process was finalized, or an alert if something fails.

In general, this is how a GitOps process works and how a GitOps tool operates. In this chapter, we are going to focus on Flux as our GitOps tool, to implement GitOps processes to automate our applications’ deployments and updates.

There are some principles that you have to follow if you are using GitOps to automate your CD pipelines. Based on Weaveworks, these principles are as follows:

  • Declarative definitions: You can often find these definitions using YAML files, but they could also be found in other formats such as JSON.
  • State of your applications versioned with Git: GitOps tools use Git repositories to store changes and states for applications.
  • Approve changes that can be applied automatically to your resources: Git repositories or services provide ways to automate a trigger tool when some changes or merges are detected.
  • Software agents listening to changes notifying or alerting: GitOps tools also have daemons listening to changes in a ready-to-launch action, such as changing a repository with a new image tag in the case of applications using containers.

Now you know how a GitOps tool works and how GitOps processes help you to automate your CD pipeline, it’s time to find out about the benefits of GitOps.

Based on Weaveworks, these are as follows:

  • Increased productivity: An automated process reduces the execution time; in this case, more changes and updates made for your applications in less time.
  • Enhanced developer experience: GitOps tools usually launch automated processes on your Git repository, and these will be launched automatically without knowing the internals of how it works—for example, for applications using Kubernetes, a developer doesn’t have to know Kubernetes in some cases. However, this depends on how your application is structured.
  • Improved stability: Logs of GitOps tools are included by default, which helps to meet some security and monitoring features.
  • Higher reliability: GitOps tools give you the ability to implement rollback mechanisms, reducing downtime for your applications if a change has an impact on the operation of your system.
  • Consistency and standardization: GitOps tools have structures to define your applications, and give you best practices for your applications’ definitions, pipelines, or updates.
  • Stronger security guarantees: GitOps tools have security features such as cryptography for secrets, and tracking and managing changes. This brings a way to secure your applications.

Now, let’s move on to understand how GitOps works in a cloud native context.

As we know, cloud native refers to the use of applications using technologies such as containers, microservices, and CI/CD in the context of a development-operations (DevOps) culture. So, you can find an intersection for this concept when a GitOps tool is designed to run on cloud native environments—for example, Kubernetes clusters.

A GitOps tool can help you to automate the CD process for your Kubernetes applications. Tools such as Argo CD or Flux can help you to implement GitOps for your applications.

But in this chapter, we are going to focus more on applications that run in low-resource environments using ARM processors. In this case, Flux has support for ARM while Argo CD doesn’t. This chapter focuses on implementing GitOps with Flux using ARM devices. So, let’s get started with a brief introduction to Flux in the next section.

The Flux website, https://fluxcd.io, says: “ Flux is a set of continuous and progressive delivery solutions for Kubernetes that are open and extensible.” Flux gives you the ability to have your Kubernetes clusters in sync with the source that contains declarative definitions of your applications, commonly stored in Git repositories.

Flux also uses the Kubernetes API to manage its objects. It also uses its own GitOps Toolkit, which gives you the tools to build a CD system on top of Kubernetes. You can see how Flux works in the following diagram:

Figure 7.2 – Flux architecture

This diagram reflects a typical workflow for GitOps, starting with a commit and a GitOps tool that is constantly checking for changes in the application’s definitions—in this case, YAML files. When Flux detects this change, it automatically provisions for the reconciliation of your applications, based on declarative definitions.

To bring essential functionalities to GitOps, Flux provides the following main features:

  • Support for Git repositories of multiple providers
  • Supported integrations for tools such as Kustomize and Helm
  • Event-triggered and periodic reconciliation
  • Integration with Kubernetes role-based access control (RBAC)
  • Alerting external systems (webhook senders)
  • External events handling (webhook receivers)
  • Automated container image updates to Git (image scanning and patching)

As a GitOps tool for Kubernetes, Flux could be installed on ARM devices. In this way, Flux could be a good match for edge computing. But first, let’s look at how Flux matches edge computing requirements.

Flux matches edge computing requirements for the following reasons:

  • Has less complexity for GitOps compared with tools such as Argo CD, Tekton, and others
  • Can be installed on ARM devices for low-resource environments
  • Requires low resource consumption to operate

This is how Flux works and how it matches edge computing requirements. Now, let’s see how we are going to organize our applications to implement GitOps for the edge using Flux in the next section.

Designing GitOps with Flux for edge applications

Section titled “Designing GitOps with Flux for edge applications”

We are going to implement GitOps for edge computing with Flux, but first, we have to explain the whole workflow and the main parts of this implementation. For this, let’s explore the following diagram, which explains the components and workflow of GitOps, implementing an image automation updater for your applications:

Figure 7.3 – Flux GitOps implementation using image updater feature

Our GitOps workflow implementation has the following steps:

  1. A developer changes the application and submits changes with a PR to be merged into the main branch. You could make changes and push them directly to the main branch, but this is not a good practice since you may have submitted broken or unreviewed changes. In later examples of this chapter, we use GitHub to host our Git repository.
  2. The repository has activated GitHub Actions and triggered a small pipeline just to build an image.
  3. An image is built in the pipeline and tagged with a new version, then it is pushed to the public Docker registry. In most business scenarios, you have to use private repositories in the cloud or on-premises.
  4. The image updater feature checks for new changes and tags for previous newly generated images that your application is going to use.
  5. Once Flux detects the new image, it looks for files configured to be updated with the new image tag. Once Flux updates the files’ definitions with the new tag, the changes are pushed to the repository.
  6. Flux detects changes in the definition of files that were updated with the new image tag. Then, Flux triggers a reconciliation process to update your applications.
  7. Objects in the Kubernetes cluster associated with the definition files are updated. Then, your application will run with the new image.

To implement the GitOps workflow just described, we are assuming the next networking and GitHub configurations:

  • Single node K3s clusters using the 192.168.0.0/24 network so that they can access the same network. You can use a different private network such as the 172.16.0.0/16 or 10.0.0.0/8 networks, for example.
  • Each cluster is using MetalLB as the bare metal load balancer service, using different IP ranges for load balancing. Cluster 1 is using IP addresses in the range of 192.168.0.51-60, cluster 2 is using 192.168.0.61-70, and cluster 3 is using 192.168.0.71-80 to do some basic IP address distribution for this network. The first addresses are typically used by the default load balancer of Traefik, so this IP address could be different in your network. Take a look at Chapter 5, K3s Homelab for Edge Computing Experiments, to configure MetalLB using the same or similar IP ranges.
  • You have a GitHub account and a token to access or create repositories in your account. Here’s what we’re doing:

With this, we are ready to start implementing this scenario in the next section.

For our GitOps implementation, we are going to use a monorepo. We have chosen to do this to reduce the management of many repositories and centralize all work in a single repository. For this use case, we are going to organize our cluster configurations and applications’ definitions in a single repository. Let’s explore the following screenshot to understand how our new repository will be organized:

Figure 7.4 – Monorepo structure

Now let’s describe what each directory and file does, as follows:

  • clusters: This is the main directory that contains all the configuration of Flux and deployments in separated directories.
  • cluster1 - cluster3: Inside these folders, the definitions of Flux and your applications are organized. cluster1 will be the cluster in charge of updating YAML definitions for the application for all clusters. So, cluster2 and cluster3 don’t need the image updater components in their installations.
  • flux-system: Contains Flux definitions to deploy it. Includes the gotk-components.yaml, gotk-sync.yaml, and kustomization.yaml files, which configure different components to implement our image updater GitOps workflow.
  • manifests: Contains the necessary definitions to deploy your application.
  • namespace.yaml: Creates a production namespace for your application.
  • helm-charts.yaml: A definition to access your Helm chart.
  • helm-release.yaml: Includes a definition and values to deploy your application using the Helm chart defined in helm-charts.yaml.
  • app-demo-registry.yaml: Contains an image to scan on Docker Hub.
  • app-demo-policy.yaml: Contains an expression to check inside files where you want to update the container image.
  • flux-system-automation.yaml: Looks for a folder to update changes.

This repository is designed for your applications. It is a monorepo for a production environment with different clusters. You can do more complex configurations using Kustomize, but that is out of the scope of this chapter.

Important Note

You can also find some approaches to how to organize your repositories on the Flux website. For more information, check out the following link: https://fluxcd.io/docs/guides/repository-structure.

Now, it’s time to see the workflow that we are going to implement in our GitOps use case for edge computing.

Understanding the application and GitHub Actions

Section titled “Understanding the application and GitHub Actions”

To start implementing GitOps with Flux, we have to set a small pipeline that creates a container image every time we modify the source code of our application. To simplify our work, this configuration will be based on the https://github.com/sergioarmgpl/fluxappdemo repository, which contains a simple Python application using Flask. This application has two directories: src and .github/workflows. The src directory contains the source of the application, while the workflows folder has the GitHub Actions configuration.

So, let’s first explore the src directory. You can see an overview of the repository in the following screenshot:

Figure 7.5 – fluxappdemo repository

The src directory contains the following files:

  • Dockerfile: This has the configuration to build a Docker image; it also calls a small unit test included in tests.py.
  • Operations.py: This has a class called Operations that contains a runningInfo method. This function receives two parameters: msg1 and msg2. With these parameters, it returns the following message: Running app <msg1> in namespace <msg2>.
  • build_push.sh: This is a sample script to build an image manually. It receives two parameters; the first one is your Docker username and the second is a tag for the image. You can run it as follows:
    $ /bin/bash build_push.sh <DOCKER_USERNAME> <IMAGE_TAG>
  • index.py: This is the main Python file to run our application. It has a function called hello_world that gets the MESSAGE and NAMESPACE environment variables and then calls the runningInfo function to return the following message: Running app <MESSAGE> in namespace <NAMESPACE>. So, every time you call the application in route / and port 5000, it will show the message, then route /_health return Running message, /_version a custom message. You could use this route to explore the application. To take a look at the code, check out the following link: https://github.com/sergioarmgpl/fluxappdemo/blob/main/src/index.py.
  • requirements.txt: Includes all the necessary libraries to run the code.
  • tests.py: This file includes a small test for the runningInfo function inside the Operations class.

You can see an overview of the src directory in the following screenshot:

Figure 7.6 – src directory with source code

.github/workflows contains a github-actions-fluxappdemo.yml file. This file contains a CI pipeline definition that builds and pushes your container ARM image using the following name and tag format: <DOCKER_USER>/fluxappdemo:RELEASE.YEAR-MONTH-DAYT-HOUR-MINUTE-SECONDZ.

DOCKER_USER is your Docker username.

You can see this file in the following screenshot:

Figure 7.7 – GitHub Actions workflows file

With this brief explanation, let’s move on to configure your own pipeline to build and push your container image.

Building your container image with GitHub Actions

Section titled “Building your container image with GitHub Actions”

To build and push your image with GitHub Actions, you should follow the given steps:

  1. Fork the https://github.com/sergioarmgpl/fluxappdemo repository. This is going to create a repository named https://github.com/<GITHUB_USER>/fluxappdemo.

GITHUB_USER is the username of your GitHub account. Replace it with your own username.

  1. Create DOCKERHUB_USERNAME and DOCKERHUB_TOKEN secrets for your repository. These will be created as encrypted secrets for a repository. To create the secrets, open the following page in the browser: https://github.com/<GITHUB_USER>/fluxappdemo/settings/secrets/actions.

After adding the variables, your repository will look like this:

Figure 7.8 – GitHub repository secrets

  1. Modify the .github/workflows/github-actions-fluxappdemo.yml file in the last line in the tags section with your user. It will look like this:
    tags: <DOCKER_USER>/fluxappdemo

Here, DOCKER_USER is your Docker Hub username.

  1. Commit and push the changes.
  2. (Optional) To check whether your GitHub action is running, you can check out the following link: https://github.com/<GITHUB_USER>/fluxappdemo/actions. The following screenshot provides an example of how this should look:

Figure 7.9 – GitHub Actions showing current workflows

  1. (Optional) To check new container tags created for your account, check out the following link: https://hub.docker.com/repository/docker/<DOCKERHUB_USERNAME>/fluxappdemo/tags. The following screenshot provides an example of how this should look:

Figure 7.10 – Docker Hub tags for a repository

  1. (Optional) To test whether your GitHub Actions pipeline works, modify the source code of the application inside the src directory and commit and push the changes. Then, a new workflow will be running.

Important Note

For more information about creating encrypted secrets for a repository, check out the following link: https://docs.github.com/en/actions/security-guides/encrypted-secrets. To create a token to access your Docker Hub account to push new images, check out this link: https://docs.docker.com/docker-hub/access-tokens. Finally, to fork a repository, check out the following link: https://docs.github.com/en/get-started/quickstart/fork-a-repo.

Now that we have configured a GitHub Actions pipeline to automate the creation of a container image with its tags, it’s time to start configuring Flux to complete our GitOps workflow in the next section.

Installing and configuring Flux for GitOps

Section titled “Installing and configuring Flux for GitOps”

Before configuring Flux, let’s understand what we are going to install in this section. In this section, we are going to install Flux and its components that detect new image tags for your container. Once new images are detected, Flux modifies the HelmRelease definition file inside your deployment repository. Then, Flux will automatically reconcile changes, updating the application deployment defined in this HelmRelease file that is using the Helm chart published at https://sergiops.xyz/helm-charts. Together with the GitHub Actions workflow defined in the Building your container image with GitHub Actions section, the complete workflow is going to work like this:

  1. The user pushes changes from its local repository copy to the original source code repository located at https://github.com/<GITHUB_USER>/fluxappdemo.
  2. GitHub Actions builds and pushes the image to Docker Hub at https://hub.docker.com/repository/docker/<DOCKER_USER>/fluxappdemo.
  3. Flux detects the new tag generated when the image was updated.
  4. Flux replaces the HelmRelease definition with the new tag. For this, Flux modifies, commits, and pushes the changes to the repository with your deployment definitions at https://github.com/<GITHUB_USER>/fluxdemo-production.git.
  5. Flux reconciles the changes, and the application is updated with the new image tag.

Note

The <GITHUB_USER> and <DOCKER_USER> values have to be replaced with your GitHub and Docker users.

To start building this use case scenario with Flux, you have to install the Flux CLI. Here’s how to do it:

  1. To install the Flux CLI on Linux, run the following command:
    $ curl -s https://fluxcd.io/install.sh | sudo bash
  2. Or, if you have macOS, you can install Flux with Homebrew using the following command:
    $ brew install fluxcd/tap/flux

Important Note

You can check for alternative installation at the official website, https://fluxcd.io.

Now, you have to install the Flux image updater feature, necessary to automate the CD process every time that a new image tag is detected. To install Flux and all necessary components, follow the given steps on each cluster:

  1. Set your environment variables; in this case, we need to set our GitHub user and the token of our GitHub account, as follows:
    $ export GITHUB_USER=<YOUR_USER>
    $ export GITHUB_TOKEN=<YOUR_GITHUB_TOKEN>
    $ export DOCKER_USER=<YOUR_DOCKER_USERNAME>

Note

Check out the following link to create a token for your GitHub account: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token.

  1. Let’s set the email address and username associated with your GitHub account. For this, run the following commands:
    $ git config --global user.email "<YOUR_EMAIL>"
    $ git config --global user.name "<YOUR_NAME>"
  2. Now, install Flux and all the necessary components for image updater implementation. For this, run the following commands:
    $ CLUSTER_N=1
    $ flux bootstrap github \
    --kubeconfig /etc/rancher/k3s/k3s.yaml \
    --components-extra=image-reflector-controller,image-automation-controller \
    --owner=$GITHUB_USER \
    --repository=fluxdemo-production \
    --branch=main \
    --path=./clusters/cluster$CLUSTER_N \
    --read-write-key \
    --personal

The repository parameter is the name of the GitHub repository to create— for example, fluxdemo-production.

CLUSTER_N is an environment variable with the number of the cluster where you are installing Flux—for example, CLUSTER_1. The YAML files used to install Flux will be stored inside the clusters/production/<CLUSTER_N> directory.

The process will show the message waiting for Kustomization “flux-system/flux-system” to be reconciled. Once Flux is installed, you will see the message all components are healthy.

This command is going to create a repository with your user account. The link to access this repository will be https://github.com/<GITHUB_USER>/fluxdemo-production.git.

GITHUB_USER is the name of your GitHub username. Replace this value in the link with your own.

Important Note

Omit the --components-extra=image-reflector-controller,image-automation-controller line for cluster2 and cluster3. cluster1 is in charge of updating the application definitions for all clusters. Also refer to the help for this command by running flux bootstrap github --help for more options, especially if you are using an organization or enterprise or another versioning tool.

  1. Clone the new repository and change it into this directory:
    $ git clone https://github.com/$GITHUB_USER/fluxdemo-production.git
    $ cd fluxdemo-production

This is going to ask you to enter your username and password to clone your repository. This will be used in the next steps to customize and create deployment definitions. Proceed as follows:

  1. Create a namespace for your application inside a directory called clusters/<clusterN>/manifests. For this, run the following commands:
    $ mkdir -p ./clusters/cluster$CLUSTER_N/manifests
    $ kubectl create ns production --dry-run=client -o YAML > ./clusters/cluster$CLUSTER_N/manifests/namespace.yaml

Create a Helm chart source Flux object to point to your own Helm chart, as follows:

$ flux create source helm helm-charts \
--kubeconfig /etc/rancher/k3s/k3s.yaml \
--url=https://sergiops.xyz/helm-charts \
--interval=1m \
--namespace=production \
--export > ./clusters/cluster$CLUSTER_N/manifests/helm-charts.yaml

In this example, we are using a Helm chart with a sample application in https://sergiops.xyz/helm-charts.

  1. Create a Flux HelmRelease object to create a YAML definition for your application deployment, as follows:
    $ flux create helmrelease app-demo --chart app-demo \
    --source HelmRepository/helm-charts.production \
    --chart-version 0.0.1 \
    --interval=1m \
    --namespace production \
    --export > ./clusters/cluster$CLUSTER_N/manifests/helm-release.yaml

Let’s add a section called values to the file by running the following command:

cat << EOF >> ./clusters/cluster$CLUSTER_N/manifests/helm-release.yaml
values:
replicaCount: 3
containerPort: 5000
dockerImage: $DOCKER_USER/fluxappdemo:RELEASE.2022-01-16T05-42-20Z # {"\$imagepolicy": "flux-system:app-demo"}
namespace: "production"
domain: "app-demo-cluster$CLUSTER_N.domain.tld"
changeCause: "First Deployment cluster $CLUSTER_N"
message: "cluster$CLUSTER_N"
appname: "app-demo-cluster$CLUSTER_N"
node: "machine$CLUSTER_N"
EOF

Pay attention to the commented line # {"$imagepolicy": "flux-system:app-demo"}.

This part tells Flux to find where to replace the image with the new tag. Finally, the file will look like this:

apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: app-demo
namespace: production
spec:
chart:
spec:
chart: app-demo
sourceRef:
kind: HelmRepository
name: helm-charts
namespace: production
version: 0.0.1
interval: 1m0s
values:
replicaCount: 3
containerPort: 5000
dockerImage: <DOCKER_USER>/fluxappdemo:RELEASE.2022-01-16T05-42-20Z # {"$imagepolicy": "flux-system:app-demo"}
namespace: "production"
domain: "app-demo-cluster1.domain.tld"
changeCause: "First Deployment cluster 1"
message: "cluster1"
appname: "app-demo-cluster1"
node: "machine1"

This HelmRelease object provided with the Flux installation provides a declarative way to parametrize a deployment. In this case, the values inside the values section correspond to the different parameters that you can send to our Helm chart. The creation of Helm charts is out of the scope of this book, but you can find good resources at the end of the chapter in the Further reading section.

Note

You can check the repository at https://github.com/sergioarmgpl/fluxdemo-production to see the final results of creating and modifying configuration files for your demo application with the previous commands.

This HelmRelease object is stored in a file and is the object that you need to modify, commit, and push your changes to your repository. After this, Flux detects the changes and updates your application. This file is inside of your repository in the clusters/cluster$CLUSTER_N/manifests/helm-release.yaml path. In this way, you can test how Flux updates your application, where the CLUSTER_N variable is the cluster number that you are modifying.

Important Note

If you want to create your own Helm chart repository, you can check out https://helm.sh/docs/topics/chart_repository and go to the GitHub Pages e xample section. In this example, we are using the charts located at https://sergiops.xyz/helm-charts and https://github.com/sergioarmgpl/helm-charts/tree/gh-pages. You can check or clone this repository to create your own.

You can omit Steps 11-13 if you are configuring cluster2 and cluster3 because cluster1 will be in charge of updating all the deployments’ definitions.

  1. (Optional) Create an image repository to detect new releases or tags for your image, as follows:
    $ flux create image repository app-demo \
    --kubeconfig /etc/rancher/k3s/k3s.yaml \
    --image=$DOCKER_USER/fluxappdemo \
    --namespace=flux-system \
    --interval=1m \
    --export > ./clusters/cluster$CLUSTER_N/manifests/app-demo-registry.yaml
  2. (Optional) Create an image policy to define an expression to match to detect new image tags or releases from your image registry. This use case is going to use the following format to tag the new Docker images:

RELEASE.YEAR-MONTH-DAYT-HOUR-MINUTE-SECONDZ

This convention is based on RFC3339 and ISO 7601, which refers to the standards for date and time on the internet:

$ flux create image policy app-demo \
--image-ref=app-demo \
--namespace=flux-system \
--select-alpha=asc \
--filter-regex='^RELEASE\.(?P<timestamp>.*)Z$' \
--filter-extract='$timestamp' \
--export > ./clusters/cluster$CLUSTER_N/manifests/app-demo-policy.yaml
  1. (Optional) Now, it’s time to put all the pieces together. For this, you have to create an ImageUpdateAutomation object, which is going to detect new releases and update the images in your deployments’ YAML definitions. In the following case, it’s going to check the folder clusters to update all the YAML definitions:
    $ flux create image update flux-system \
    --git-repo-ref=flux-system \
    --git-repo-path="./clusters" \
    --checkout-branch=main \
    --push-branch=main \
    --author-name=<AUTHOR_NAME> \
    --author-email=<AUTHOR_EMAIL> \
    --commit-template="{{range .Updated.Images}}{{println .}}{{end}}" \
    --export > ./clusters/cluster$CLUSTER_N/manifests/flux-system-automation.yaml

You have to change the <AUTHOR_NAME> and <AUTHOR_EMAIL> tags with your own values. This will appear as the commit author when Flux pushes changes for image tags.

  1. Commit and push the changes to the repository with the following commands:
    $ git add -A
    $ git commit -m "feat: App YAML definitions"
    $ git push origin main

The push command is going to ask you for the user and the token that you previously created, to access your GitHub account.

Now, you can build a new image, and you can wait for Flux to automatically update your HelmRelease file with the new image detected. After 1 minute or more, you can expect the change to have been made. You will expect to see a commit in your repository made by Flux with the new tag detected, to troubleshoot whether the image updater is working.

  1. (Optional) You can force Flux to apply this configuration by running the Flux reconciliation process with the following command:
    $ flux reconcile kustomization flux-system --with-source --kubeconfig /etc/rancher/k3s/k3s.yaml
  2. The Helm chart is going to provision a LoadBalancer service type. To find the provisioned IP address, run the following command:
    $ IP_LOADBALANCER=$(kubectl get svc app-demo-cluster$CLUSTER_N-srv --output jsonpath='{.status.loadBalancer.ingress[0].ip}' -n production)

Here, the IP_LOADBALANCER variable contains the IP of the load balancer created by the HelmRelease definition, which is the endpoint for your application in this cluster. You can check the value by running the following command:

$ echo $IP_LOADBALANCER

Using Figure 7.4 as a reference, you will expect to see an IP address such as 192.168.0.52.

  1. Let’s say that, for example, the returned IP is 192.168.0.52. You can access your application with the following URL: http://192.168.0.52:5000. You can test to access other routes—for example, /_version or /_health.

Now that you have installed Flux, you can start testing the auto-reconciliation to update your files by committing and pushing the changes of your HelmRelease files. The auto-reconciliation updates everything Flux detects in new image tags of your applications. This process is described in the previous section, Designing GitOps with Flux for edge applications. After this, you can continue with the next section to learn how to troubleshoot your installation.

There are a few useful commands that can help you to troubleshoot your installation; in this section, we’re going to find out what these are. So, let’s proceed as follows:

  1. To reconcile Flux changes in Flux, run the following command:
    $ watch flux get images all --all-namespaces --kubeconfig /etc/rancher/k3s/k3s.yaml

This command is going to show new tags detected for your container, and how these new tags are set up in your HelmRelease YAML definition file.

  1. To check the image repositories in Flux, run the following command:
    $ flux get image repository app-demo --kubeconfig /etc/rancher/k3s/k3s.yaml --namespace=production
  2. To check the current policy in your cluster, run the following command:
    $ flux get image policy app-demo --kubeconfig /etc/rancher/k3s/k3s.yaml --namespace=production
  3. To get all images configured in your Flux installation, run the following command:
    $ flux get images all --all-namespaces --kubeconfig /etc/rancher/k3s/k3s.yaml
  4. To reconcile YAML definition changes in your cluster, run the following command:
    $ flux reconcile kustomization flux-system --with-source --kubeconfig /etc/rancher/k3s/k3s.yaml
  5. To watch in real time how image detection and updates to your repositories are running, run the following command:
    $ watch flux get images all --all-namespaces --kubeconfig /etc/rancher/k3s/k3s.yaml
  6. To check your application deployments, run the following command:
    $ kubectl get deploy -n production
  7. To check your Pods, run the following command:
    $ kubectl get pods -n production

You have now learned these essential commands to troubleshoot your Flux system.

In the next section, we are going to explore Flux monitoring dashboards.

Flux itself doesn’t include a graphical user interface for management but integrates some useful dashboards using Prometheus and Grafana to visualize the state of your deployments. These dashboards have to be installed on each cluster. To install this feature, follow the next steps:

  1. Configure the Git repository that contains monitoring stack definitions for its installation. The configuration will listen for changes every 30 minutes. The code is illustrated here:
    $ flux create source git monitoring \
    --interval=30m \
    --kubeconfig /etc/rancher/k3s/k3s.yaml \
    --url=https://github.com/fluxcd/flux2 \
    --branch=main
  2. Install kube-prometheus-stack, which is going to be used to configure Prometheus for your dashboards. This stack will be installed in the monitoring namespace. The code is illustrated in the following snippet:
    $ flux create kustomization monitoring-stack \
    --interval=1h \
    --kubeconfig /etc/rancher/k3s/k3s.yaml \
    --prune=true \
    --source=monitoring \
    --path="./manifests/monitoring/kube-prometheus-stack" \
    --health-check="Deployment/kube-prometheus-stack-operator.monitoring" \
    --health-check="Deployment/kube-prometheus-stack-grafana.monitoring" \
    --health-check-timeout="5m0s"
  3. Install Grafana and configure your Flux dashboards, storing data in Prometheus and visualizing this across preconfigured dashboards in Grafana. The code is illustrated in the following snippet:
    $ flux create kustomization monitoring-config \
    --interval=1h \
    --kubeconfig /etc/rancher/k3s/k3s.yaml \
    --prune=true \
    --source=monitoring \
    --path="./manifests/monitoring/monitoring-config"
  4. Access the dashboards using the next command:
    $ kubectl -n monitoring port-forward svc/kube-prometheus-stack-grafana --address 0.0.0.0 3000:80

This is going to open port 3000 of your dashboard. Remember that the IP address that you have to access is the IP of the node where you are accessing this dashboard.

Access the dashboard using the following URL: http://<NODE_IP_ADDRESS>:3000/d/flux-control-plane.

NODE_IP_ADDRESS is the IP address cluster node where you are running the command shown in this step.

  1. To access the dashboard, use the next credentials:
    • Username: admin
      • Password: prom-operator

The login screen will look like this:

Figure 7.11 – Grafana login form

Once you are logged in, you will be redirected to the dashboard URL previously mentioned.

  1. Once the dashboard is opened, it will look like this:

Figure 7.12 – Grafana Flux control plane dashboard

Important Note

Remember that you can customize this dashboard and create your own dashboards. For more information about this dashboard, you can visit the following link: https://fluxcd.io/docs/guides/monitoring.

Your Grafana dashboards are now installed successfully, and you can see the state of your deployments. Now, it’s time to learn how to uninstall Flux in the next section.

Once you don’t need the Flux installation anymore, you can run the following command:

$ flux uninstall -s --namespace=flux-system --kubeconfig /etc/rancher/k3s/k3s.yaml

This is going to uninstall Flux from your Kubernetes cluster. Now, it’s time to finish the chapter.

In this chapter, we learned how GitOps works and how you can implement GitOps using GitHub Actions and Flux. Flux could be useful to automate your deployments on an edge environment, using a single Git repository. For this, we learned how Flux can implement GitOps to update your applications at the edge using the HelmRelease object and the image updater feature. Flux can manage your application cluster without using an external way to expose the Kubernetes API of your cluster, which is the case with tools such as Argo CD. This can be translated into cost savings and a more effective tool for edge environments using ARM devices. Argo CD, on the other hand, doesn’t support ARM and needs a way to expose your Kubernetes API from your cluster to connect the cluster to Argo CD using a public load balancer or a virtual machine on the internet. In the next chapter, we are going to learn how to add basic observability and traffic splitting to your applications using Linkerd.

Here are a few questions to validate your new knowledge:

  • How can GitHub Actions help me to implement GitOps at the edge?
  • How can I implement GitOps using Flux?
  • Which other features does Flux have that can help me to implement GitOps?
  • How can I troubleshoot my Helm releases with Flux?
  • How can I apply this simple use case in my edge computing scenarios?
  • How can I structure my repositories for GitOps?

You can refer to the following references for more information on the topics covered in this chapter: