GitOps Explanation using ArgoCD
Table of contents
In this Blog, we will try to see what is GitOps and how we will use it in real time applications using a sample EKS Cluster deploying a simple tetris-game.
Gitops:
GitOps as its name suggests combines both Git and Operations making Git as a single source of truth for deploying, managing infrastructure, and configuring applications.
GitOps automates deployment and management, reduces manual intervention, minimizes errors, and allows for easy version control.
So when you write Kubernetes manifest files, you should always apply them using kubectl command which requires manual work and delay in applying the manifest files. So, GitOps comes as a rescue here by taking that task and when there is any change in Kubernetes Manifest files, it will automatically sync them and will deploy those changes directly to the Kubernetes Cluster.
ArgoCD is a open source tool we will use to implement GitOps Practices.
Benefits of GitOps:
- Automation: GitOps automates the entire CICD Pipelines flow, so we do not need to execute any kubectl commands at the end of pipeline.
2)Version Control: Since we are using Git as a single source of Truth, everything will be available in Git and if you want to go back to earlier versions, it will be easy.
3)Consistency: Consistency is achieved across different environments, improving configuration reliability. You can have multiple environments managed by a single ArgoCD cluster having multiple branches in a GitHub repo or separate Github repos for different environments.
4)Security: GitOps enhances security by controlling access to Git repositories, ensuring authorized users make changes.
- Faster deployments: Efficiency and faster deployment times are achieved through automation.
Pre-requisites:
1)An AWS account
2)Kubectl installed in your command Line
Hands-on:
Now let's try GitOps by doing a hands-on example of deploying a Tetris game on Elastic Kubernetes Service (EKS) using Argo CD.
1)Create a New Roles in AWS IAM with EKS-Cluster and EKS-NodeGroup use case.
Go to AWS EKS page and click on create a new Cluster.
Give a name and attach the service role which you have created in First step.
- Now attach the default VPC and all the public subnets, make sure you are not attaching us-east-1e subnet as it is not supporting. Also attach the security Group.
Now wait for 5-10 minutes as it will take time for creating the cluster.
When the cluster is up, you will see the status changing to Active from Pending.
As the Cluster is ready, Now go to Compute section of it and click on Add NodeGroup.
5)Give a Node name and attach a service role.
6)In next tab, select all default values for AMI, disk size and choose only t3.medium for instance type as ArgoCD requires instance types greater than that. Also select subnets and create NodeGroup.
After NodeGroup is created, go to your command line interface and install kubectl on it. Once kubectl is installed, you need to connect kubectl with this cluster by using below command:
aws eks update-kubeconfig --name GitOps --region us-east-1
Once the connection is done, check whether connection is established properly by knowing pods, As nothing is configured on cluster, you will see No resources exists.
Now, go to this link and enter all cli commands necessary to install ArgoCD.
https://archive.eksworkshop.com/intermediate/290_argocd/install/
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/v2.4.7/manifests/install.yaml
sudo curl --silent --location -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/download/v2.4.7/argocd-linux-amd64
sudo chmod +x /usr/local/bin/argocd
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
export ARGOCD_SERVER=`kubectl get svc argocd-server -n argocd -o json | jq --raw-output '.status.loadBalancer.ingress[0].hostname'`
export ARGO_PWD=`kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d`
The last 2 commands are for getting the Load Balancer DNS Name & storing that into ArgoCD Server and the password we will use for logging into ArgoCD Server.
LB DNS and password will be stored in ARGOCD_SERVER & ARGOCD_PWD variables respectively.
Now open LB DNS in a browser and you can see ArgoCD application running in that browser.
Login with Username as admin and password in ARGO_PWD variable and you will be able to login to the ArgoCD server.
Now ArgoCd setup is done and we need to connect our GitHub repo with manifest files with ArgoCD so that ArgoCd will take care of managing any changes further.
Go to Manage your Repositories -> Repositories -> Connect your repo using HTTPS.
Give Details like Git Repo name, repo url, username and password in case of private Repo and click on Connect.
Once we connected our repo details, then create a new application with application name, git URL and cluster details such as cluster URL, namespace.. as mentioned in below pic.
Once application is created, you can see after few seconds that the application is healthy and synced.
You can check your pods using kubectl get pods command and you can see pods running as application is synced with ArgoCD.
Click on three dots on tetris-service and you can see Hostname and this is the server where your tetris-game application has been deployed.
Now comes the magic of ArgoCD, now try to replace the docker image to other version. Go to GitHub and change the image version from v1 to v2.
Now in ArgoCD, you can see that the new version has been synced and when you open endpoint in a new tab. You can find the new commit in the last sync result.
Now refresh the DNS and check whether you are getting the old version or newer one.
That's it, you have now used GitOps for automatically syncing with Kubernetes without the need to apply kubectl command everytime there is a change in deployment or other manifest files.
Dont forget to delete the Node Groups and EKS Cluster as it might cost you some money.
Thanks for reading. If you have any suggestions and if I miss anything please let me know.
References:
https://www.youtube.com/watch?v=1hF-HRq5Mww&t=61s (Nasiullha explained very well)
GitHub Link: https://github.com/AnilKumar-Noolu/tetris-game.git
Subscribe to my newsletter
Read articles from Anil Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by