How to Implement Flux for Kubernetes: Step-by-Step Guide with Examples
- Infrakube ltd
- Dec 21, 2024
- 2 min read
Flux is a GitOps tool that helps you automate the deployment of applications to Kubernetes clusters. It monitors a Git repository for changes and applies them to your Kubernetes environment. Here’s a step-by-step guide to implementing Flux in your Kubernetes cluster.

Step 1: Prerequisites
1. Kubernetes Cluster: A running Kubernetes cluster (e.g., Minikube, EKS, AKS, GKE, or a local cluster).
2. kubectl Installed: Ensure kubectl is installed and configured to connect to your Kubernetes cluster.
3. Git Repository: A Git repository where you store your Kubernetes manifests.
4. Flux CLI: Install the Flux CLI using the command:
curl -s https://fluxcd.io/install.sh | sudo bash
Step 2: Bootstrap Flux
Flux requires a Git repository to manage Kubernetes resources. You can bootstrap Flux with your existing repository or a new one.
1. Bootstrap Flux with Your Repository:
Replace <YOUR_GIT_PROVIDER> with your Git provider (e.g., github or gitlab), <ORG/USER> with your Git organization or username, and <REPO> with the repository name.
flux bootstrap <YOUR_GIT_PROVIDER> \
--repository=<REPO> \
--branch=main \
--owner=<ORG/USER> \
--path=./clusters/my-cluster \
2. Verify Installation:
Once bootstrapped, Flux components (like source-controller, kustomize-controller, etc.) will be running in the flux-system namespace. Confirm with:
kubectl get pods -n flux-system
Step 3: Add Your Kubernetes Manifests to Git
1. Structure Your Repository:
Organize your repository as follows:
.
├── apps/
│ ├── namespace.yaml
│ ├── deployment.yaml
│ └── service.yaml
├── clusters/
│ └── my-cluster/
│ ├── kustomization.yaml
│ └── flux-system/
└── README.md
2. Add a Sample Application:
Namespace.yaml:
apiVersion: v1
kind: Namespace
metadata:
name: my-app
deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
namespace: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:1.19
ports:
- containerPort: 80
service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-app
namespace: my-app
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
3. Commit and Push Changes:
git add .
git commit -m "Add initial app manifests"
git push origin main
Step 4: Create a Flux Kustomization
1. Create a kustomization.yaml:
Inside clusters/my-cluster/ directory, create a kustomization.yaml to define what Flux should apply:
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
name: my-app
namespace: flux-system
spec:
interval: 1m
path: ./apps
prune: true
sourceRef:
kind: GitRepository
name: flux-system
namespace: flux-system
2. Apply the Kustomization:
Push the kustomization.yaml to your Git repository. Flux will detect the new configuration and apply the changes to the cluster.
Step 5: Verify Deployment
1. Check Flux Logs:
flux logs
2. Verify Resources in Kubernetes:
kubectl get all -n my-app
You should see the deployment and service running in the my-app namespace.
Step 6: Automate Updates
To update your application or infrastructure, make changes to the Git repository. Flux will detect these changes and automatically apply them to your Kubernetes cluster.
For example:
1. Update the image tag in deployment.yaml.
2. Commit and push the changes.
3. Verify the updated application in Kubernetes.
Conclusion
By using Flux, you can achieve a fully automated, Git-driven deployment process for your Kubernetes environment. With the power of GitOps, managing applications and infrastructure becomes more reliable, auditable, and collaborative. This approach ensures consistency and reduces manual errors, enabling you to focus on delivering value to your users.
Комментарии