Blog | CloudStakes Technology
1711088747ArgoCD and GitOps to Deploy DevOps-based Application on Kubernetes.webp

Use ArgoCD and GitOps to Deploy DevOps-based Application on Kubernetes

user-img

Nikita Gill

23rd March 2022

Deploying DevOps-based applications or any other application on Kubernetes can benefit in terms of infrastructural advantages, such as scaling, flexibility, distributed component management, and application versioning. In the DevOps development process, continuous integration and deployment (CI/CD) mechanisms often work at a high level of abstraction, offering functionalities like version controlling, logging amendments, and rollback. To deal with this abstraction layer, a popular approach called GitOps comes to the rescue. In this blog, we will use ArgoCD to synchronize and deploy a DevOps-based application stored in the GitHub repository on Kubernetes clusters.

Prerequisites:
  • An SSH key pair on your operating system (Linux, macOS, BSD) and for Windows OS, work with Windows subsystem for Linux.
  • A Kubernetes cluster with one worker node
  • kubectl setup with connection to Kubernetes cluster
Step 1: Install ArgoCD on Kubernetes Cluster
  • Use the following command to validate the Kubernetes configuration setup with kubectl to be able to ping worker nodes.
$ kubectl get nodes

This command then will return the list of worker nodes with Ready status to proceed.

  • Create the argocd namespace in the Kubernetes cluster, having ArgoCD and associated services:
$ kubectl create namespace argocd
  • Then project maintainers will provide the Argo CD installation script as mentioned below, so run it.
$ kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
  • Run the below-mentioned command to check the progress of the ArgoCD installation process: 
$ watch kubectl get pods -n argocd

By default, it shows five pods that have gained the Running state.

  • Press Ctrl+C to exit the installation Watch interface.

Now, you’re all set with ArgoCD in the Kubernetes cluster!

Step 2: Forward Ports to Access ArgoCD

As Kubernetes deploys services by default to arbitrary networks in the cluster, there is a requirement to forward associated ports to grant them access to be able to use them from the respective OS. ArgoCD configures a service called argocd-server on port number 443 internally, but as this port is the default HTTPS one, it is recommended to change it to another port, like 8080.

  • Use the following command to forward ports:
$ kubectl port-forward svc/argocd-server -n argocd 8080:443
  • To quit the blocking process of the port forwarding, you can press the Ctrl+C combination, as post forwarding will block the terminal till its active time.
  • Navigate to the localhost:8080 to access the ArgoCD from the web browser, in which you’ll have to take the help of the command line to get to the next step.

Note: During step 3, you may need to surpass the security warning as ArgoCD has yet to be configured with an SSL certificate.

Step 3: Use Command Line to Work on ArgoCD
  • Ensure the installation of Command argocd on the local machine to deal with ArgoCD instance settings.
  • If argocd is not installed, it is recommended to install it using Homebrew package manager, a popular command-line tool available for macOS and Linux machines.
  • Use the following command to install the Homebrew tool on your local machine:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • Enter the password and use the brew command to install ArgoCD from the terminal:
$ brew install argocd
  • The brew command then returns the argocd command, but before using it, use kubectl to get the auto-generated admin login credentials.
  • Use the login credentials to pass a path to a JSON file, using Kubernetes secrets, to extract the associated value:
$ kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo
  • Navigate to the localhost: 8080 in your browser, log into your ArgoCD dashboard as admin:

ArgoCD dashboard

  • Check everything on the localhost operated ArgoCD dashboard, whether they seem to on their place. Then use the argocs login command to log into your ArgoCD account from the command-line interface.
$ argocd login localhost:8080
  • During the login process, you’ll surely get many certificate-related security warnings like before, thus, whenever they pop up, press the Y key.
  • You can also change the existing ArgoCD account password. For that, use command:
$ argocd account update-password

Now, you’re all set for using ArgoCD to deploy applications.

Step 4: Manage Multiple Kubernetes Clusters
  • Use the following command to list all Kubernetes clusters to decide a particular one for deploying your DevOps-based application:
$ kubectl config get-contexts -o name
  • Register test-target-cluster with ArgoCD to deploy the application into it:
$ argocd cluster add target-k8s

Now, additional cluster login credentials are added to ArgoCD, enabling it for deploying services.

Step 5: Deploy Your Application on Kubernetes Cluster

Here, you’ll know the procedures to deploy the application to various Kubernetes clusters. The ArgoCD oversees the management of applications’ source codes, written by considering GitOps fundamentals and stored in GitHub repo. Here, we’re going to deploy a helm-guestbook demo with the Helm chart. You can also consider it as one of the robust Kubernetes management solutions.

  • Provide a specific path to the Git repository, storing helm-guestbook demo. For that, you’ll have to pass location and namespace:
$ argocd app create helm-guestbook --repo https://github.com/argoproj/argocd-example-apps.git --path helm-guestbook --dest-server https://kubernetes.default.svc --dest-namespace default
  • Now check the status of the application created into the ArgoCD:
$ argocd app get helm-guestbook

If it returns Sync Status as OutOfSync, consider application status as normal. It also indicates the successful retrieval of the application helm chart from the GitHub repo.

  • Now, use Kubernetes resources to deploy the application:
$ argocd app sync helm-guestbook

Here, sync references the deployment of the application under agreement with GitOps principle, aiming to track the application using ArgoCD in the 1:1 collaboration with its upstream configuration. Now, your application is successfully deployed on the Kubernetes cluster using ArgoCD. You can also use the web interface of the ArgoCD application, but the command line makes the process faster and reproducible.

  • Once deployment is done, you can check the process via referring to the ArgoCD web dashboard opened using localhost: 8080.

ArgoCD web dashboard view showing application deployment data

  • Run the application deployment on the browser. For this, you’ll need to forward the helm guestbook app port to HTTP port 80 or 9090:
$ kubectl port-forward svc/helm-guestbook 9090:80
  • Use the Ctrl+C shortcut key to stop the terminal blocking pop-ups, and then forward the port to localhost: 9090 to see the web view of the guestbook application demo:

Application Deployment on Kubernetes using ArgoCD and GitOps (GitHub Repo+DevOps rules) At last, it’ll reflect all new pushes onto the GitHub repo in the ArgoCD, which will continuously resync the application deployment alongside ensuring its high availability.

Conclusion:

As Kubernetes contains many abstraction layers, it is necessary to ensure the maintainability of deployments. And in that case, GitOps principles provide feasible solutions. Have queries related to our DevOps solutions and services? Book your first free 60 mins of DevOps consultation with us TODAY!