Day 23 of my 90-day Devops journey: Embracing GitOps Principles with Argo CD with Hello world project
Introduction
As you are building a website and need to deploy it to a Kubernetes cluster, it would be a good idea if you could manage everything from your Git repository. This would ensure consistency in your deployments and make it easy to roll back changes if needed. This is the power of GitOps.
GitOps uses Git as the major source of truth for your infrastructure and applications. You define your desired state using configuration files, called manifests, and store them in your Git repository. Then, when you make changes to these manifests and push them to Git, the GitOps tool will automatically apply those changes to your Kubernetes cluster. This way, your deployments are always consistent, you have a complete history of changes, and rollbacks are as easy as reverting a Git commit.
GitOps tools like ArgoCD monitor your repository and automatically update your Kubernetes cluster whenever changes are detected.
What is Kubernetes?
Kubernetes is an open-source container orchestration platform that helps you deploy, scale, and manage your applications. It provides a consistent way to run your applications across different environments, from your local machine to the cloud.
What are YAML and Manifests?
YAML is a human-readable data serialization language often used for defining Kubernetes configurations. Manifests are YAML files that describe the desired state of your application within Kubernetes, such as the number of replicas, the container image to use, and the network configuration.
How Does GitOps Work?
Define your application: Write Kubernetes manifests (YAML files) that describe how your application should be deployed.
Store in Git: Put these manifests in a Git repository.
Install ArgoCD: Set up ArgoCD to monitor your repository.
Push changes: Make changes to your manifests and push them to Git.
ArgoCD deploys: ArgoCD automatically updates your Kubernetes cluster based on the new manifests.
This way, your deployments are always consistent, you have a complete history of changes, and rollbacks are as easy as reverting a Git commit.
Why Use GitOps?
GitOps offers numerous benefits for DevOps teams:
Improved Consistency: Declarative configurations ensure that your deployments are consistent across environments, eliminating the "works on my machine" problem.
Enhanced Security: Git's version control provides a secure audit trail of all changes, making it easy to track who made what modifications and when.
Simplified Rollbacks: Rolling back to a previous version is as simple as reverting a Git commit, making troubleshooting and recovery much faster.
Getting Started with ArgoCD
ArgoCD is a Kubernetes-native continuous delivery tool that implements GitOps principles. It's like a "smart" bridge between your Git repository and your Kubernetes cluster.
Let's set up a simple "Hello World" application using ArgoCD.
Here's how ArgoCD works:
You define your application: You write Kubernetes manifests (YAML files) that tell ArgoCD how your application should be deployed.
ArgoCD monitors your Git repository: It constantly checks for changes in your manifests.
When changes are detected: ArgoCD automatically updates your Kubernetes cluster to match the new configuration.
You get a clear overview: ArgoCD provides a user interface to see the status of your deployments, track changes, and even roll back to previous versions easily.
Resources
ArgoCD Documentation: ArgoCD Documentation
GitOps: Operations by Pull Request: Weaveworks GitOps
eBook: GitOps: The Complete Guide: GitOps Guide
Prerequisites
A Kubernetes cluster (Minikube, kind, or a cloud provider like Google Kubernetes Engine (GKE) or Amazon Elastic Kubernetes Service (EKS))
kubectl
installed and configured to interact with your Kubernetes clusterA Git repository for your application manifests
Step 1: Install ArgoCD
Create a namespace for ArgoCD:
kubectl create namespace argocd
Apply the ArgoCD installation manifests:
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
Expose the ArgoCD server:
kubectl port-forward svc/argocd-server -n argocd 8080:443
You can now access the ArgoCD UI at http://localhost:8080
.
Step 2: Configure ArgoCD
Obtain the initial password for the admin
user:
kubectl get secret argocd-initial-admin-secret -n argocd -o jsonpath="{.data.password}" | base64 -d
Log in to the ArgoCD UI using admin
and the password.
Create a new application:
In the ArgoCD UI, click "New App" and provide the following details:
Application Name: A descriptive name for your application
Project: Default (unless you have custom projects configured)
Sync Policy: Choose "Automatic" for continuous deployments or "Manual" for more control
Repository URL: The URL of your Git repository (e.g.,
https://github.com/your-username/your-repo.git
)Path: The path to the directory containing your Kubernetes manifests (e.g.,
manifests
)Cluster URL: The URL of your Kubernetes cluster. For in-cluster deployments, use
https://kubernetes.default.svc
.Namespace: The namespace where the application will be deployed in your Kubernetes cluster
Step 3: Deploy Your Application
In your Git repository, create a directory (e.g., manifests
) and add the Kubernetes manifests for your "Hello World" application.
Push the changes to your Git repository:
git add .
git commit -m "Add initial application manifests"
git push
ArgoCD will automatically detect the changes and update your Kubernetes cluster. You can verify the deployment by checking the status in the ArgoCD UI or using kubectl
to see the running pods.
Troubleshooting
I encountered some errors I encountered some errors during the project. If you encounter any of these issues during the installation or deployment process, here are some common errors and their solutions:
Error: "Unable to connect to the server: EOF"
Verify that Minikube (or your Kubernetes cluster) is running and properly configured.
Check your Docker Desktop settings to ensure it's using Linux containers (I had to go to the kubernetes tab to enable it and click apply & restart).
Ensure your firewall and network settings are not blocking the connection to the Kubernetes API server.
Error: "Unauthorized"
Make sure you're using the correct admin password obtained in the previous steps.
Check your kubectl configuration and ensure it's pointing to the correct Kubernetes context.
Additional Resources
Articles and Websites
ArgoCD Official Documentation
Description: The official ArgoCD documentation provides comprehensive guides and tutorials on setting up and using ArgoCD. It's an essential resource for both beginners and intermediates.
Kubernetes Documentation
Description: This is the official documentation for Kubernetes. It includes detailed explanations of Kubernetes concepts, tutorials, and best practices for deploying and managing applications.
Weaveworks GitOps
Description: Weaveworks offers a detailed introduction to GitOps principles and practices. It includes tutorials and use cases that help understand how to implement GitOps with various tools, including ArgoCD.
eBooks
"GitOps and Kubernetes" by O'Reilly
Description: This eBook provides an in-depth look at GitOps practices and how to implement them with Kubernetes. It's suitable for readers who have some familiarity with Kubernetes and are looking to adopt GitOps.
"The DevOps Handbook" by Gene Kim, Jez Humble, Patrick Debois, and John Willis
Description: While not exclusively about GitOps, this book provides a comprehensive overview of DevOps practices, including continuous delivery, infrastructure as code, and monitoring.
By exploring these resources, I believe you'll gain a comprehensive understanding of GitOps, ArgoCD, and Kubernetes.