Simplifying Kubernetes Deployment with Argo CD

Simplifying Kubernetes Deployment with Argo CD

·

5 min read

Introduction

In the world of handling containers and making them work together smoothly, Kubernetes stands out as a go-to choice. It's like a powerful and flexible system that helps manage applications packed in containers. Now, when it's time to set up and take care of tasks in Kubernetes, a lot of developers and DevOps teams like using tools that follow Continuous Integration and Continuous Deployment, or CI/CD for short.

Argo CD is one of these tools, and people like it because it's easy to use and gets the job done well when deploying stuff in Kubernetes. This blog post is all about how to use Argo CD to put your jobs into action on Kubernetes, also check out what makes Argo CD special compared to other tools doing the same CI/CD job.

Getting Started with Argo CD

Pre-requisites:

  • Pre-installed Kubernetes environment to install Argo CD. Here we’ll be using Minikube (minikube.sigs.k8s.io/docs/start)

  • Have an application image published on a container registry (Dockerhub)

  • Have a Git repository ready with the manifest files

Install Argo CD on Kubernetes

Use kubectl to install ArgoCD on your Kubernetes (minikube) cluster. This will create the necessary resources.

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

This will create a new namespace, argocd, where Argo CD services and application resources will live.

Access the Argo CD Web UI

Once installed, you can access the Argo CD UI using port forwarding.

kubectl port-forward svc/argocd-server -n argocd 8080:443

Open your browser and go to https://localhost:8080.

The default login credentials are admin (username) and the password can be retrieved using:

kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d

This command fetches the Argo CD admin password from the Kubernetes Secret, decodes it from base64, and displays the actual password.

For PowerShell Users

In the case where you're using PowerShell run the following commands to get the login credentials:

kubectl -n argocd get secret argocd-initial-admin-secret -n argocd -o yaml

This will provide an output like this:

apiVersion: v1
data:
  password: xxxxxxxxxxxxxxxxxxxxxxx
kind: Secret
metadata:
  creationTimestamp: "2024-01-10T11:33:59Z"
  name: argocd-initial-admin-secret
  namespace: argocd
  resourceVersion: "1234"
  uid: 12345678-1234-1234-1234-1234567890AB
type: Opaque

Then copy the password provided in the output and run the following command:

[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("xxxxxxxxxxxxxxxxxxxxxxx"))

Deploy Your First Application

Argo CD provides several ways to deploy applications, offering flexibility to accommodate different workflows and scenarios. Some common ways to deploy applications using Argo CD include the use of the WebUI, Argo CD CLI, Declarative GitOps Workflow and more. Here in this case, we’ll be focusing on deploying an application using the WebUI.

In other words, the Git repository becomes the source of truth for the application configuration. This is in line with the GitOps philosophy, where the desired state of the system is declared in the Git repository, and tools like Argo CD use this repository to ensure the actual state matches the desired state.

Creating a New Application

Add all your configuration files and manifests inside a Git repository, because this Git repository holds the declarative configuration, and Argo CD ensures the Kubernetes cluster converges to this desired state automatically, providing a robust and automated deployment pipeline.

Head to the Applications section of the Argo CD Web UI and click on "New Application." Fill in application details, including the Git repository, name of application (myapp-argo-application), target cluster, and path to manifests (hover-dev in this case). Finally, save the configuration by hitting “Create” from the top left.

Alternatively, you can save the following application.yaml file and run the kubectl apply command.

# Argo CD Configuration (application.yaml)

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: myapp-argo-application
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/aftab-s/ArgoCD-WebApp-Test.git
    targetRevision: HEAD
    path: hover-dev
  destination:
    server: https://kubernetes.default.svc
    # Specify which namespace the config files in the repo should be applied to
    namespace: myapp

  # Since by default there is no myapp namespace setup prior,
  # We have to make argo cd create a namespace "myapp" automatically while deploying the K8s cluster.
  syncPolicy:
    syncOptions:
      - CreateNamespace=true # By default, it'll be set to false
    automated: # this attribute will pull changes from (polls) git repo every 3 minute intervals
      # If that delay has to be removed, you can configure a Git Webhook
      selfHeal: true # Since by default, changes made to the live cluster manually will not trigger automated sync
      prune: true # If service.yaml is deleted in the git repo, the service must be deleted from the whole cluster also

After saving this file, run the kubectl apply command.

kubectl apply -f application.yaml

This will create the namespace, and create the deployment inside the minikube cluster.

Syncing the Application

Go to the Applications dashboard and select your application. Based on your requirements, click "Sync" and choose "Manual Sync”, where Argo CD will detect the changes in the Git repository but sync will have to be carried out manually (will have to go to the Web UI and hit the “Sync” button) or “Automated Sync” where Argo CD will detect the changes in the Git repository and will carry out the Sync automatically by itself.

Now if you’d like to access the application running on the cluster, run the kubectl port-forward command for the application service you’d like to access. Here, in this case, the command looks like this:

kubectl port-forward service/myapp-service -n myapp 8899:8080

You can access the application on http://localhost:8899

Conclusion

Argo CD stands out as a specialized and effective CI/CD tool for Kubernetes deployments. Its emphasis on declarative configuration, GitOps workflow, and Kubernetes-native approach makes it a compelling choice for teams looking to streamline their container orchestration workflows. While other CI/CD tools are versatile and widely adopted, Argo CD's focus on Kubernetes-specific deployments provides a level of simplicity and efficiency that can be advantageous for teams heavily invested in Kubernetes-based architectures. As organizations continue to embrace containerization and Kubernetes, tools like Argo CD play a crucial role in enabling seamless and reliable application deployments.

Did you find this article valuable?

Support Aftab S by becoming a sponsor. Any amount is appreciated!