Flux v2 is a GitOps tool for Kubernetes that helps automate application deployment and image updates. This configuration and setup guide will cover various options for setting up Flux v2, including setting up a Git repository as a source, configuring image updates, and setting up notifications.
Prerequisites
Before setting up Flux v2, make sure you have the following:
- An empty Kubernetes cluster
- A working
kubectl
command - The Flux CLI installed. You can install it using Homebrew with
brew install fluxcd/tap/flux
(source)
Repo Layout and Flux Bootstrap
When working with Flux, you’ll generally set up all your Kustomizations, install Flux to your local machine, and then run flux bootstrap
to tell Flux to set up everything in the cluster for you. You don’t set up the cluster by hand at any point.
When you run flux bootstrap
, you tell Flux the Git repo to use, and the branch and path within that repo to start looking for its configuration. This means Flux will need access to your Git repo, which typically means it’ll need access to GitHub or GitLab. You’ll need to set up an access token for Flux to use (source).
Here’s an example of how to run flux bootstrap
:
flux bootstrap github \
--owner=YOUR_GITHUB_USERNAME \
--repository=YOUR_REPO_NAME \
--branch=main \
--path=path/to/flux/config \
--personal-token-file=/path/to/personal/access/token
Replace YOUR_GITHUB_USERNAME
, YOUR_REPO_NAME
, and /path/to/personal/access/token
with your GitHub username, repository name, and the path to your personal access token file, respectively.
Configuring Image Updates
Flux can automatically sync images in your cluster with the images in a container registry. To configure image updates, you can use the fluxctl image
command.
Here’s an example of how to sync images from Docker Hub:
fluxctl image sync \
--k8s-fwd-ns=flux-system \
--url=docker.io \
--poll-interval=1m
Replace docker.io
with the URL of your container registry, and 1m
with the poll interval you prefer (source).
Setting up Notifications
Flux can send notifications when certain events occur, such as when a deployment fails or when a new Git commit is pushed. To set up notifications, you can use the fluxctl notification
command.
Here’s an example of how to send notifications to Slack:
apiVersion: notification.toolkit.fluxcd.io/v1beta1
kind: Notification
metadata:
name: slack-notifications
spec:
receiver:
name: slack
webhook: WEBHOOK_URL
template:
base:
spec:
template:
spec:
containers:
- name: webhook
image: fluxcd/slack-webhook:latest
env:
- name: SLACK_WEBHOOK
value: WEBHOOK_URL
- name: MESSAGE
value: "{{ .Common.Message }}"
eventTypes:
- gitpoll
- imagepull
- imagepush
- deploy
Replace WEBHOOK_URL
with your Slack webhook URL (source).
Customizing Flux
You can customize Flux during bootstrap in various ways, such as installing optional components, configuring vertical scaling, sharding, and multi-tenancy, and setting up proxy settings. For more information, see the Flux documentation on configuration.