GitOps for Seamless Software Deployment

In the ever-evolving landscape of software deployment, GitOps has emerged as a game-changer, streamlining the journey from code to cloud. This article will explore GitOps using ArgoCD, a prominent GitOps operator, focusing on two repositories: the application repository gitops-apps-hello and the source of truth repository gitops-k8s-apps. We'll delve into setting up a workflow that integrates these repositories with ArgoCD for seamless deployment. Fork these repos and replace the references in the below article to experiment on your own.

Understanding GitOps With ArgoCD

GitOps is more than just a buzzword; it's a paradigm that leverages Git as the single source of truth for infrastructure and application configurations. Integrating GitOps with ArgoCD enhances the deployment process offering a robust solution for managing Kubernetes clusters.

Key Benefits:

Setting Up a GitOps Workflow With ArgoCD

To exploit the full potential of GitOps, let's set up a workflow using the specified repositories and ArgoCD.

Pre-Requisites:

Step 1: Install ArgoCD

Install ArgoCD on your Kubernetes cluster. You can use the following command:

Shell
 
kubectl create namespace argocd

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


Step 2: Access ArgoCD UI

Once installed, access the ArgoCD UI by port-forwarding:

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


Then, visit http://localhost:8080 in your browser.

Step 3: Connect Repositories

Connect the application repo and the source of the truth repo to ArgoCD:

Shell
 
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64

sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd

argocd login localhost:8080 --username admin --password <password>


Shell
 
argocd repo add https://github.com/brainupgrade-in/gitops-apps-hello
argocd repo add https://github.com/brainupgrade-in/gitops-k8s-apps


Step 4: Define an Application in ArgoCD

Define an application in ArgoCD that points to the gitops-k8s-apps repository. This can be done either via the UI or the CLI. Here's an example using the CLI:

Shell
 
argocd app create argocd --repo https://github.com/brainupgrade-in/gitops-k8s-apps.git --path argocd/apps --dest-server https://kubernetes.default.svc --dest-namespace argocd


Shell
 
curl -o argocd-cm.yaml  https://raw.githubusercontent.com/brainupgrade-in/gitops-k8s-apps/main/argocd/overlays/argocd-cm.yaml

kubectl patch cm argocd-cm -n argocd --type strategic --patch-file argocd-cm.yaml

curl -o argocd-repo-server-deploy.yaml https://raw.githubusercontent.com/brainupgrade-in/gitops-k8s-apps/main/argocd/overlays/argocd-repo-server-deploy.yaml

kubectl patch deployment argocd-repo-server -n argocd --type strategic --patch-file argocd-repo-server-deploy.yaml


Automating Deployments

With ArgoCD, changes in the gitops-k8s-apps repository automatically triggers deployments in your Kubernetes cluster. ArgoCD continuously monitors this repository and ensures that the cluster's state matches the desired state defined in the repository.

Example: Updating an Application

Integrating Jenkins CI/CD into our GitOps workflow adds another layer of automation and control, especially when updating applications. Jenkins can be used to automate the process of building, testing, and deploying changes to our application repository, brainupgrade-in/gitops-apps-hello, and then updating our source of truth repository, brainupgrade-in/gitops-k8s-apps, which in turn triggers ArgoCD to deploy these changes to the Kubernetes cluster.

Setting up Jenkins for Application Updates

Properties files
 
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Commands to build the application
                sh 'echo Building Application...'
            }
        }
        stage('Test') {
            steps {
                // Commands to test the application
                sh 'echo Running Tests...'
            }
        }
        stage('Update Source of Truth') {
            steps {
                // Updating the gitops-k8s-apps repository with the new application version
                script {
                    // Fetch the new image tag or build number
                    def newImageTag = 'my-app-image:1.1.0' // Example tag
                    // Clone the source of truth repository
                    sh 'git clone https://github.com/brainupgrade-in/gitops-k8s-apps.git'
                    sh 'cd gitops-k8s-apps'
                    // Update the Kubernetes manifests with the new image tag
                    sh "sed -i 's|newTag: .*|newTag: ${newImageTag}|' ./hello/e2e/kustomization.yaml"
                    // Commit and push the changes
                    sh 'git commit -am "Update app version"'
                    sh 'git push origin main'
                }
            }
        }
    }
}


Benefits of Using Jenkins in the GitOps Workflow

Integrating Jenkins into our GitOps workflow for updating applications adds a robust, automated pipeline that ensures reliability and efficiency in deployments. This combination of Jenkins for CI/CD and ArgoCD for GitOps offers a powerful toolset for modern cloud-native application deployment.

Best Practices for GitOps With ArgoCD

  1. Immutable Infrastructure: Treat infrastructure as code; all changes should be through Git.
  2. Review and Approval Processes: Use pull requests and code reviews for changes in the Git repositories.
  3. Regular Monitoring: Keep an eye on the ArgoCD dashboard for the status of applications.
  4. Security Practices: Implement secure access controls and audit trails for both repositories.

Conclusion

Mastering GitOps with ArgoCD is a stepping stone towards efficient and reliable software deployment. By leveraging the robustness of Git and the automation capabilities of ArgoCD, we can achieve a seamless deployment process. This approach resonates with modern software development needs, ensuring a smoother and more controlled path from code to cloud. As we continue to innovate, embracing methodologies like GitOps will be pivotal in shaping efficient and secure software deployment landscapes.

 

 

 

 

Top