GitOps for Seamless Software Deployment
This article explores GitOps, focusing on using ArgoCD and Jenkins CI/CD for seamless software deployment. It guides you through setting up a GitOps workflow.
Join the DZone community and get the full member experience.
Join For FreeIn 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:
- Automated Deployments: Changes in the Git repository automatically trigger deployments.
- Improved Traceability: Every change is traceable through Git commits.
- Enhanced Security: Git's inherent security features bolster the deployment process.
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:
- A Kubernetes cluster.
- Access to the specified Git repositories.
- ArgoCD is installed on your Kubernetes cluster.
Step 1: Install ArgoCD
Install ArgoCD on your Kubernetes cluster. You can use the following command:
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:
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:
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>
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:
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
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
- Jenkins Installation and Setup: First, ensure Jenkins is installed and properly configured. You should have a Jenkins server with the necessary plugins for Git and Kubernetes.
- Creating a Jenkins Pipeline: Create a new Jenkins pipeline that is triggered by changes in the
gitops-apps-hello
repository. This pipeline will handle the build and test processes for the application.
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'
}
}
}
}
}
- Handling Repository Credentials: Ensure Jenkins has the necessary credentials to access both Git repositories. This can be set up in the Jenkins credentials store.
- Webhooks for Triggering Builds: Set up webhooks in the
gitops-apps-hello
repository to trigger the Jenkins pipeline automatically whenever there's a push to the repository. - Pipeline Execution: When changes are pushed to the
gitops-apps-hello
repository, the Jenkins pipeline triggers. It builds and tests the application, and if these stages succeed, it proceeds to update thegitops-k8s-apps
repository with the new application version. - ArgoCD Deployment: Once Jenkins pushes the updated Kubernetes manifests to the
gitops-k8s-apps
repository, ArgoCD detects these changes. ArgoCD then synchronizes the changes, deploying the updated application version to the Kubernetes cluster.
Benefits of Using Jenkins in the GitOps Workflow
- Automated Testing and Building: Jenkins automates the build and test phases, ensuring that only thoroughly tested applications are deployed.
- Traceability: Every change is logged and can be traced back through Jenkins builds, providing an audit trail.
- Flexibility: Jenkins pipelines can be customized to include additional stages like security scanning or integration testing.
- Efficiency: This integration streamlines the process from code change to deployment, reducing manual intervention and speeding up the release process.
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
- Immutable Infrastructure: Treat infrastructure as code; all changes should be through Git.
- Review and Approval Processes: Use pull requests and code reviews for changes in the Git repositories.
- Regular Monitoring: Keep an eye on the ArgoCD dashboard for the status of applications.
- 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.
Published at DZone with permission of Rajesh Gheware. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments