DEV Community

Michael Levan
Michael Levan

Posted on

GitOps With Flux (Complete Setup Guide)

In a previous blog post talking about what GitOps is, which you can find here, you learned about the overall need for GitOps and why it’s important.

Now that you know a bit about GitOps, it’s time to get it up and running in your own cluster!

In this blog post, you’ll learn how to utilize Flux, which is a GitOps tool in your Kubernetes cluster.

Creating A Kubernetes Cluster

Depending on what type of environment you’re in or what you have access to, you can get a Kubernetes cluster up and running in a few different ways:

  • On your localhost with something like Minikube
  • On a Kubernetes service like AKS, EKS, or GKE
  • On a bunch of VMs
  • On Rasberry Pis using K3s
  • And a bunch of other methods...

Regardless of which method you go with, Flux will work.

For the purposes of this blog post, you can use Minikube as it’s great for a local environment and you don’t have to worry about spending money in the cloud or deploying a bunch of virtual machines.

To get started with Minikube, check out this guide.

The App

The whole idea behind GitOps is for a tool (like Flux) to look in your Git repos, check out the Kubernetes manifest, and confirm that what’s in the Kubernetes manifest is what’s on your Kubernetes cluster. Because of that, we need a Kubernetes application.

For the purposes of this blog post, you’ll use an Nginx app. It’s nothing too complex and perfect for just getting started.

You can find the public repository here.

Flux Configuration

When you’re getting ready to install Flux, there are a few components:

  • The command-line tool
  • Connecting to GitHub
  • GitHub credentials
  • Deploying Flux

In this section, we’ll go over everything you need to get Flux up and running.

Installing Flux

To install Flux, you’ll have to check out the installation guide found here for your operating system.

For example, to install flux on MacOS, you can run the following command:

brew install fluxcd/tap/flux
Enter fullscreen mode Exit fullscreen mode

To check that the installation was successful and that the Kubernetes cluster you’re using is compatible with Flux, you can run the following command:

flux check --pre
Enter fullscreen mode Exit fullscreen mode

Image description

Install Flux

To install flux, you’ll need to specify your GitHub username.

The flux bootstrap github command creates the GitHub repository if it doesn’t exist and commits the toolkit to the main or master branch. Then it configures the Kubernetes cluster to sync with the repository.

For example, to use my username adminturneddevops on the kubernetes-examples GitHub repo, it would look like the below.

flux bootstrap github \
  --owner=adminturneddevops \
  --repository=kubernetes-examples \
  --branch=main \
  --path=./clusters/my-cluster \
  --personal
Enter fullscreen mode Exit fullscreen mode

You’ll be prompted for a Personal Access Token (PAT) for your GitHub organization. After the installation is complete, an output similar to the screenshot below will be shown.

Image description

Within the repository, you’ll see a new directory with the Flux configuration.

Image description

Deploying An Application

Now that Flux is installed, it’s time to deploy an application. The first thing you should do is clone the code repository from The App section and fork it to your GitHub organization.

Once you have the cloned repository, cd (change directory) into the repository on your terminal/command-line.

Add The App To Flux

First, add the nginx app to Flux. The command below will pull the Nginx app in 30 second intervals.

flux create source git kubernetes-examples \
  --url=https://github.com/AdminTurnedDevOps/kubernetes-examples \
  --branch=main \
  --interval=30s \
  --export > ./clusters/my-cluster/podinfo-source.yaml
Enter fullscreen mode Exit fullscreen mode

Once you run the command above, a new file called podinfo-source.yaml will be added to the repository. Push the new file/code up to GitHub.

Deploy The Nginx App

To deploy the app, you’re going to use Kubernetes kustomize, which is a tool for customizing Kubernetes applications in a declarative fashion.

First, create a file called podinfo-kustomization.yaml and put it into your GitHub repository.

Next, run the command bellow. The command below will use the kubernetes-examples app for a source.

flux create kustomization kubernetes-examples \
  --target-namespace=default \
  --source=kubernetes-examples \
  --path="./kustomize" \
  --prune=true \
  --interval=5m \
  --export > clusters/my-cluster/podinfo-kustomization.yaml
Enter fullscreen mode Exit fullscreen mode

Notice how there is a kustomize directory. That directory is created manually (it already exists in the GitHub repo). That’s where the application configuration goes (example: the Nginx Kubernetes manifest). Without the Kubernetes manifest for your app inside of the kustomize directory, the Flux configuration won’t work.

Once you run the command above, commit and push it to your GitHub repo.

Ensure That The App Is Deployed

To ensure that the app is deployed, you can use the flux get command to see the apps that are running and managed by Flux.

flux get kustomizations --watch
Enter fullscreen mode Exit fullscreen mode

Top comments (0)