Flux CD:
Flux CD is a Continuous Delivery (CD) and GitOps tool for Kubernetes.
Flux is a Kubernetes-based application deployment and management solution. It works by
monitoring your source code repository for changes and automatically generating
deployments to keep your applications up to date.
Flux manages deployments using the GitOps concept, which implies that your application’s
desired state is stated in source code and version-controlled using Git. This method makes it
simple to track deployment modifications and collaborate with other team members.
GitOps:
GitOps is a method of managing your infrastructure and apps in such a way that the entire
system is described declaratively and version controlled (most likely in a Git repository), with
an automated mechanism ensuring that the deployed environment matches the state
indicated in the repository.
Pre-Requisites:
1. EC2 instance with terraform, docker, AWS cli, Kubectl installed.
2. Running EKS cluster
Tools installation:
Install AWS CLI and Configure
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install
Okay now after installing the AWS CLI, let's configure the AWS CLI so that it can
authenticate and communicate with the AWS environment.
aws configure
Install and Setup Kubectl
Moving forward now we need to set up the kubectl also onto the EC2 instance.
curl -LO "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
sudo mv ./kubectl /usr/local/bin
kubectl version
Follow below steps to install terraform on AmazonLinux.
sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo
https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
Creating an Amazon EKS cluster using terraform
Code available in https://github.com/ksnithya/blue-green.git
git clone https://github.com/ksnithya/blue-green.git
cd blue-green
terraform init
terraform plan
terraform apply
aws eks --region ap-south-1 update-kubeconfig --name eks_cluster_demo
Installing the Kubernetes Metrics Server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-
server/releases/latest/download/components.yaml
kubectl get deployment metrics-server -n kube-system
Install Flux
https://www.webagesolutions.com/blog/deploy-an-application-using-flux
We will now install Flux.
1. Run this command to install Flux:
curl -s https://fluxcd.io/install.sh | sudo bash
Bootstrap Flux
Verify Flux pre-check for Kubernetes cluster:
Then, run the following command to have Flux check your cluster to see if it meets the
requirements for Flux to function properly.
flux check --pre
GitHub Credentials:
To begin, export your GitHub credentials to your terminal so that Flux may use them
to bootstrap your GitHub repository.
# Your GitHub PAT (Personal Access Token)
export GITHUB_TOKEN=****************************************
# Your GitHub username
export GITHUB_USER=***********
How to create Github PAT(Personal Access token):
Login to github.
1. In the upper-right corner of any page, click your profile photo, then click Settings.
2. In the left sidebar, click Developer settings.
3. In the left sidebar, click Personal access tokens -> Token(Classic) ->Generate
New token.
4. Click on “Generate new token(classic).
5. Then give name, select expiration date, Select scope as per your
requirement and generate token
6. Copy the token and save in secure place. We cant get the same key
again.
Run the bootstrap for a repository on your personal GitHub account:
flux bootstrap github \
--owner=$GITHUB_USER \
--repository=flux-documents \
--branch=master \
--path=./clusters/my-cluster \
--personal
For public repo:
flux bootstrap github --owner=$GITHUB_USER --repository=flux-doc --
branch=master --private=false --personal=true --path=clusters/my-
cluster
Parameter Explanation
–
The name of the Git repo that will be created.
repository
–branch The branch where the files will be committed.
A folder in the repo where the config files will be added. This
–path option lets you store the files for multiple clusters in the
same repo.
–personal A private repo will be created.
If all goes well, you should see all components as healthy.
We can check the installation
Kubectl get ns
kubectl get all -n flux-system
We can see new repository created in our Github
Prepare the Application for Flux Based Deployment
We will now add the yaml file that defines the deployment and service for our demo
app.
We will then tell Flux to monitor these files and look for any updates. Flux will detect
any changes to these files and automatically apply them to K8s. The process of
defining what deployment, service etc. you need for an application is called
customization.
Clone the repo where our configuration file exists.
git clone https://github.com/ksnithya/blue-green.git
cd blue-green/blue
create a file called kustomization.yaml. Note: The file must be called
kustomization.yaml. You need to add all the yaml file we are using to deploy the
application. If we make any modification to this yaml file then Flux CD will
automatically identify it and redeploy the application automatically.
Configure Flux for Our Application
Add new resources via GitOps
Now, We intend to use the GitOps technique by only committing new definitions to
the GitHub repository and leaving the rest to Flux CD. Flux CD should automatically
retrieve and apply updated resource definitions from the repository to the cluster.
Let’s have a look at the repository and add some more resource definitions to it.
We will now tell where our application’s “kustomization” files are
located.
In the flux-doc repo, go inside the clusters/my-cluster folder:
[ec2-user@ip-172-31-9-225 flux-main]$ git clone
https://github.com/ksnithya/flux-doc.git
cd flux-doc/clusters/my-cluster/flux-system
Generate YAML via CLI
If you wish to use the CLI, run the two commands below to generate the YAML
declarations for the two required resources.
In the flux-doc/clusters/my-cluster folder, create a file
cd flux-doc/clusters/my-cluster
# Generate GitRepository Resource YAML
flux create source git blue-green \
--url= https://github.com/ksnithya/blue-green \
--branch=master \
--interval=30s \
--export > ./source.yaml
# Generate Kustomization Resource YAML
flux create kustomization blue-green \
--target-namespace=default \
--source=blue-green \
--path="./blue" \
--prune=true \
--interval=5m \
--export > ./kustomiz.yaml
git add -A && git commit -m "Added application"
git push
While you wait for your resources to be applied, execute the following command to
observe Flux CD at work.
flux get kustomizations –watch
Now I have build new docker image and pushed to docker hub and used
that new image in deployment file in our app repository. Automatically
application deployed.
To uninstall flux:
flux uninstall -n flux-system