Flux is a GitOps tool for continuous delivery in Kubernetes, automating the synchronization of the state of manifests in a Git repository with a Kubernetes cluster, allowing for version-controlled deployment and easy tracking of changes.
Abstract
Flux is an extensible GitOps tool for Kubernetes that automates the deployment of applications by synchronizing the desired state from a Git repository to the cluster. It supports both Kustomize and Helm for managing deployments. Flux ensures that any changes made to the repository are automatically reflected in the cluster, enabling a reliable and traceable deployment process. The tool can also integrate with Flagger for progressive delivery and allows for quick recovery in case of disasters by provisioning new clusters with the same configuration. The article guides readers through installing Flux, setting up a GitHub repository, creating Kubernetes secrets, deploying Flux components, and using HelmRelease for managing Helm chart releases. It also includes a practical demonstration of deploying a HelmRelease and observing the automated updates in the cluster.
Opinions
The author endorses Flux as an effective solution for GitOps in Kubernetes, highlighting its automated synchronization feature and the ability to track changes easily.
There is an emphasis on the benefits of GitOps for developers, such as direct code pushes to production from a version-controlled system and the ability to revert changes quickly in case of issues.
The author expresses a preference for using Flux over other tools in the market, indicating that it is a reliable choice for managing operational workflows with Kubernetes.
The author suggests that Flux is user-friendly, providing a step-by-step guide on how to install and configure it, including the installation of HelmRelease CRD for managing Helm chart releases.
The inclusion of a practical example and screenshots indicates that the author believes in the value of hands-on experience and visual aids for understanding and using Flux effectively.
The conclusion and the recommended articles section reflect the author's interest in providing readers with further learning resources and related topics, suggesting a commitment to community education and knowledge sharing.
Deploying Applications in Kubernetes Using Flux
Introduction to Flux
Flux is an Open and extensible continuous delivery solution for Kubernetes. Flux is a GitOps tool for Kubernetes that synchronizes the state of manifests in a Git repository to what is running in a cluster. So what is GitOps? Is it a new tool in the market? GitOps provides a way for developers to manage operational workflow for using Kubernetes using Git. It is all about using a version-controlled system for the deployment of applications in Kubernetes. So Developers can directly push the code into production from the version-controlled system like Git. Moreover, any changes made can be easily tracked and reverted in case of any chaos. There are multiple tools in the market to run GitOps. Today in this article we would be experimenting with a tool called Flux.
Features of Flux:
Automated synchronization between a version control repository and a cluster.
Any changes made to the repository are instantly reflected in the cluster.
Developers can directly push the code into production from the repositories.
All the configuration is stored in the version control system and is up to date.
Let us now Install fluxcd in our Kubernetes cluster using a helm chart. If you are not familiar with what a helm chart is, refer to this guide. Before we Install fluxcd we will have to Install the HelmRelease CRD ( Explained later in the article ).
helm repo add fluxcd https://charts.fluxcd.io #Adding the Flux Repo
kubectl apply -f https://raw.githubusercontent.com/fluxcd/helm-operator/master/deploy/crds.yaml
#Installing the HelmRelease CRD
Flux connects to the Git Repository using an ssh key. If the ssh key already exists, A Kubernetes secret can be created from the key. Else configure the key with your GitHub given by fluxcd after installation. Since I already have an existing key pair I would be creating a Kubernetes Secret from the Private Key.
kubectl create secret generic flux-git-deploy --from-file=identity=./id_rsa -n flux --dry-run=client -o yaml | kubectl apply -f -
#This would create the kubernetes secret for flux to communicate with GitHub
Since we have made the configuration for our flux deployment to communicate with our git repo let us deploy fluxcd and HelmOperator deployment.
The Helm Operator is a Kubernetes Operator, allowing one to declaratively manage Helm chart releases. The desired state of a Helm release is described through a Kubernetes Custom Resource named HelmRelease. . Based on the creation, mutation, or removal of a HelmRelease resource in the cluster, Helm actions are performed by the operator.
Here is a sample repo which contains some sample helm charts and a sample HelmRelease file. We would now understand what is written in the HelmRelease file.
kind: HelmRelease ( Kubernetes CRD ).
metadata.name: The name of the HelmRelease.
metadata.namespace: The namespace in which the HelmRelease is supposed to be deployed in.
metadata.annotations: fluxcd.io/automated: To enable automation for fluxcd.
spec.releaseName: The name of the helm chart release name.
spec.targetNamespace: The namespace into which the helm chart has to be installed. ( Make sure you create the namespace before the HelmRelease gets Installed )
spec.chart.git: The Git Repository URL from which the helm charts has to be installed.
spec.chart.path: The path from GitHub Repository.
spec.chart.ref: The name of the GitHub branch.
Demo
Once the fluxcd and helm operator charts are installed you should see the flux components created in the flux namespace.
fluxcd components
Now go grab a cup of coffee and wait for 5 minutes. You should now have all your resources created in your cluster defined in the helm chart. These are the resources defined in our helm chart.
Resources in the helm chart
Let us watch the resources in the fluxcd-demo namespace ( spec.targetNamespace-> HelmRelease file )
watch -n 5 kubectl get all -n fluxcd-demo
fluxcd-demo namespace
And now you have all the resources defined in the helm chart created in your Kubernetes Cluster.
Woooo !!!
As we know that fluxcd will watch, pick up the changes from git, and will update our cluster, let us update the number of replicas of fluxcd-demo deployment.
Number of replicas = 1
Let us edit our HelmRelease manifests to override the values defined in the values.yaml file of the Helm Chart.
I have updated the number of replicas to 5 in GitHub by overriding the replicaCount value in the HelmRelease file.
Now go grab another cup of coffee and wait for 5 minutes.
Number of replicas = 5
We have now successfully deployed our first HelmRelease using fluxcd.
Conclusion
Thanks for reading my article. Here are some of my other articles that may interest you.