Introduction to Flux CD v2
Create a kubernetes cluster
In this guide we we''ll need a Kubernetes cluster for testing. Let's create one using kind
kind create cluster --name fluxcd --image kindest/node:v1.23.5
See cluster up and running:
kubectl get nodes
NAME STATUS ROLES AGE VERSION
fluxcd-control-plane Ready control-plane,master 2m12s v1.23.5
Run a container to work in
run Alpine Linux:
docker run -it --rm -v ${HOME}:/root/ -v ${PWD}:/work -w /work --net host alpine sh
install some tools
# install curl
apk add --no-cache curl
# install kubectl
curl -sLO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
mv ./kubectl /usr/local/bin/kubectl
# install helm
curl -o /tmp/helm.tar.gz -LO https://get.helm.sh/helm-v3.10.1-linux-amd64.tar.gz
tar -C /tmp/ -zxvf /tmp/helm.tar.gz
mv /tmp/linux-amd64/helm /usr/local/bin/helm
chmod +x /usr/local/bin/helm
test cluster access:
/work # kubectl get nodes
NAME STATUS ROLES AGE VERSION
fluxcd-control-plane Ready control-plane,master 3m26s v1.23.5
Flux CD
get flux command-line tool
Let's download the flux
command-line utility.
We can get this utility from the GitHub Releases page
It's also worth noting that you want to ensure you get a compatible version of flux which supports your version of Kubernetes. Checkout the prerequisites page.
curl -o /tmp/flux.tar.gz -sLO https://github.com/fluxcd/flux2/releases/download/v0.41.1/flux_0.41.1_linux_amd64.tar.gz
tar -C /tmp/ -zxvf /tmp/flux.tar.gz
mv /tmp/flux /usr/local/bin/flux
chmod +x /usr/local/bin/flux
Now we can run flux --help
to see its installed
Check our cluster
flux check --pre
Documentation
As with every guide, we start with the documentation
The Core Concepts is a good place to start.
We begin by following the steps under the bootstrap section for GitHub
We'll need to generate a personal access token (PAT) that can create repositories by checking all permissions under repo
.
Once we have a token, we can set it:
export GITHUB_TOKEN=<your-token>
Then we can bootstrap it using the GitHub bootstrap method
flux bootstrap github \
--owner=marcel-dempers \
--repository=docker-development-youtube-series \
--path=kubernetes/fluxcd/repositories/config/clusters/dev-cluster \
--personal \
--branch fluxcd-2022
flux check
# flux manages itself using GitOps objects:
kubectl -n flux-system get GitRepository
kubectl -n flux-system get Kustomization
Check the source code that flux bootstrap
created
git pull origin <branch-name>
Repository structure
https://fluxcd.io/flux/guides/repository-structure/
- Mono Repo
- Repo per team
- Repo per app
- apps
- example-app-1
- example-app-2
- infrastructure
- ingress-nginx
- monitoring
- clusters
-dev-cluster
-prod-cluster
build our app
cd kubernetes/fluxcd/repositories/example-app-1/src
docker build . -t example-app-1:0.0.1
#load the image to our test cluster so we dont need to push to a registry
kind load docker-image example-app-1:0.0.1 --name fluxcd
deploy our app
kubectl -n default apply -f repositories/config/apps/example-app-1/gitrepository.yaml
kubectl -n default apply -f repositories/config/apps/example-app-1/kustomization.yaml
# check our flux resources
kubectl -n default describe gitrepository example-app-1
kubectl -n default describe kustomization example-app-1
# check deployed resources
kubectl get all
kubectl port-forward svc/example-app-1 80:80
changes to our app
Once we make changes to our app.py
we can build a new image with a new tag
cd kubernetes/fluxcd/repositories/example-app-1/src
docker build . -t example-app-1:0.0.2
#load the image to our test cluster so we dont need to push to a registry
kind load docker-image example-app-1:0.0.2 --name fluxcd
# git commit & git push to branch!
deploy by updating manifest
To update our app, we simply have to update the image tag in our kubernetes YAML
file and flux
will sync it.
This is generally the role of CI, where flux
concern is mainly CD.
Here is an example on how to automate that
deploy by image scanning
An alternative method is to use your CI to build and push a newly tagged image to your registry (same as first option) and use Flux image scanner to trigger the rollout instead of automating a commit to your config repo.
We firstly need to enable image scanning as its not enabled by default.
To do this we just need to re-bootstrap flux
with an addition flag
flux bootstrap github \
--owner=marcel-dempers \
--repository=docker-development-youtube-series \
--path=kubernetes/fluxcd/repositories/config/clusters/dev-cluster \
--components-extra=image-reflector-controller,image-automation-controller \
--personal \
--branch fluxcd-2022
We need to create a image reigsitry credential where we will push our image:
kubectl -n default create secret docker-registry dockerhub-credential --docker-username "" --docker-password "" --docker-email test@test.com
kubectl -n default apply -f repositories/config/apps/example-app-1/imagerepository.yaml
Build and push our app
docker build . -t aimvector/example-app-1:0.0.2
docker push aimvector/example-app-1:0.0.2
#see changes
kubectl describe imagerepository