How to Continuously Deliver Kubernetes Applications With Flux CD
By: Gaurav Agarwal
Tags: Programming, Software Engineering, Kubernetes, DevOps, Technology

Authorise Flux CD to Connect to Your Git Repository

We now need to allow the Flux CD operator to interact with the Git repository, and therefore, we need to add its public SSH key to the repo.

Get the public SSH key using fluxctl .

$ fluxctl identity --k8s-fwd-ns flux

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCryxSADyA+GIxtyCwpO3R9EuRcjZCqScKbYO246LZknyeluxKz0SlHYZHrlqxvla+k5GpPqnbImLLhuAD+YLzn0DbI58hUZLsrvxPWKiku--REDACTED--MKoPyEtQ+JiR3ZiADx6Iq8tYRRR+WBs1k5Hc8KNpg+FSRP8I8+CJRkCG4JQacPwK8FESP4qr1dxVv1tE8ZXyb8CdiToKpK7Mkc= root@flux-b9b4cc4f9-p9w88

Add the SSH key to your repository so that Flux CD can access it.

Go to https://github.com/<YOUR_GITHUB_USER>/nginx-kubernetes/settings/keys

Add a name to the Key in the Title section.

Paste the SSH Key in the Key section.

Check “Allow write access.”

Flux CD synchronises automatically with the configured Git repository every five minutes. However, if you want to synchronise Flux with the Git repo immediately, you can use fluxctl sync , as below.



Synchronizing with ssh://

Revision of master to apply is 8db9163

Waiting for 8db9163 to be applied ...

Done. $ fluxctl sync --k8s-fwd-ns fluxSynchronizing with ssh:// git@github.com /bharatmicrosystems/nginx-kubernetesRevision of master to apply is 8db9163Waiting for 8db9163 to be applied ...Done.

Now let’s get the pods to see if we have two replicas of nginx.

$ kubectl get pod -n web

NAME READY STATUS RESTARTS AGE

nginx-deployment-7fd6966748-lj8zd 1/1 Running 0 20s

nginx-deployment-7fd6966748-rbxqs 1/1 Running 0 20s

Get the service, and you should see an nginx load balancer service running on port 80. If your Kubernetes cluster can spin load balancers, you should see an external IP appear.

$ kubectl get svc -n web

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

nginx-service LoadBalancer 10.8.10.33 35.222.174.212 80:30609/TCP 94s

Test the service using the external IP. If your cluster does not allow you to spin load balancers, you can use the NodeIP:NodePort combination.



This is version 1 $ curl http://35.222.174.212/ This is version 1

Update the image to bharamicrosystems/nginx:v2 on workloads/nginx-deployment.yaml

$ sed -i "s/nginx:v1/nginx:v2/g" workloads/nginx-deployment.yaml

$ git add --all

$ git commit -m 'Updated version to v2'

$ git push origin master

Let’s now wait for five mins for the auto-sync, and meanwhile, watch the pods updating.

$ watch -n 30 'kubectl get pod -n web' NAME READY STATUS RESTARTS AGE

nginx-deployment-5db4d6cb84-8lbsk 1/1 Running 0 11s

nginx-deployment-5db4d6cb84-qc6jp 1/1 Running 0 10s

nginx-deployment-6784c95fc7-zqptk 0/1 Terminating 0 6m43s

And as you see, the old pods are terminating, and new ones are spinning up. Check the pod status to ensure all pods are running.

$ kubectl get pod -n web

NAME READY STATUS RESTARTS AGE

nginx-deployment-5db4d6cb84-8lbsk 1/1 Running 0 1m

nginx-deployment-5db4d6cb84-qc6jp 1/1 Running 0 1m

Now let’s call the service again.



This is version 2 $ curl http://35.222.174.212/ This is version 2

And as you see, the version is now updated to v2.

Congratulations! You have successfully set up Flux CD on your Kubernetes cluster.