⎈ A Hands-On Guide to ArgoCD on Kubernetes — PART-2 ⚙️
⇢ Understanding Projects and Applications in ArgoCD with Practical Example
In the first part of this blog series, we introduced ArgoCD
and guided you through the installation and basic usage of the tool. You learned how to deploy your first application and how to sync and monitor it through the ArgoCD dashboard.
In this second part, we’ll dive deeper into ArgoCD by focusing on its CLI
(Command Line Interface), managing projects
declaratively, creating and managing applications
and application Rollbacks and History Tracking.
Prerequisites
Before we begin, ensure that you have a Kubernetes cluster running and that ArgoCD is already installed on it. If not please follow my first blog.
Installing the ArgoCD CLI
The ArgoCD CLI is a powerful tool that enables you to manage your ArgoCD resources directly from your terminal. Here’s how to install it on different operating systems:
For macOS:
brew install argocd
For Linux:
VERSION=$(curl -L -s https://raw.githubusercontent.com/argoproj/argo-cd/stable/VERSION)
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/download/v$VERSION/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
Verifying the Installation
Once installed, verify that the CLI is correctly set up by running:
argocd version
Logging In and Connecting to the Cluster
To manage your ArgoCD instance, you need to log in using the CLI and connect to your ArgoCD server.
- Retrieve the Initial Admin Password: By default, ArgoCD uses
admin
as the username. Retrieve the password usingkubectl
:
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath='{.data.password}' | base64 -d
2. Log In to ArgoCD: Use the password retrieved from the previous step to log in:
argocd login <ARGOCD_SERVER> --username admin --password <PASSWORD>
$ argocd login 144.226.259.34:30080
WARNING: server certificate had error: tls: failed to verify certificate: x509: certificate signed by unknown authority. Proceed insecurely (y/n)? yes
Username: admin
Password:
'admin:login' logged in successfully
Context '144.226.259.34:30080' updated
Replace <ARGOCD_SERVER>
with your ArgoCD server’s address (e.g., IP Address of the argocd cd service or domain name i.e.,argocd.example.com
) and <PASSWORD>
with the retrieved password.
Understanding Projects and Applications in ArgoCD
Before we dive into managing resources, it’s important to know the basics:
- Projects: These are like folders that help you organize your applications in ArgoCD. They manage permissions and resources, making them useful in environments with multiple teams.
- Applications: An application is a group of Kubernetes resources defined in a Git repository. ArgoCD ensures these resources are always up to date with the Git repository.
Now that you know what projects and applications are, let’s explore how to create and manage them using the ArgoCD CLI
and declarative methods.
Creating and Managing Projects with ArgoCD CLI
ArgoCD allows you to organize your applications into projects. This feature is particularly useful in multi-tenant environments or when managing a large number of applications.
Creating a New Project
You can create a new project using the following command:
$ argocd proj create demo-proj \
--description "My first demo project" \
--dest https://kubernetes.default.svc,* \
--src '*' \
--allow-cluster-resource '*/*'
$ argocd proj list
NAME DESCRIPTION DESTINATIONS SOURCES CLUSTER-RESOURCE-WHITELIST NAMESPACE-RESOURCE-BLACKLIST SIGNATURE-KEYS ORPHANED-RESOURCES
default *, * */* <none> <none> disabled
demo-proj My first demo project https://kubernetes.default.svc,*. * */* <none> <none> disabled
Replace <project-name>
with your desired project name, and update the destination and source URLs as needed. This command sets up a new project with a default destination (your Kubernetes cluster) and source (Any Git repository).
Managing Existing Projects
To view all projects, run:
argocd proj list
To update a project, use:
argocd proj update <project-name> --description "Updated description"
And to delete a project:
argocd proj delete <project-name>
These commands allow you to fully manage your projects through the CLI.
Declarative Project Management
ArgoCD supports declarative management, where you define your project configurations in YAML files and apply them to your ArgoCD instance. Here’s an example of a declarative project configuration:
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: demo-proj
namespace: argocd
spec:
description: "A sample demo project"
clusterResourceWhitelist:
- group: '*'
kind: '*'
destinations:
- server: '*'
sourceRepos:
- '*'
To apply this configuration:
kubectl apply -f project.yaml
This method is particularly useful in a GitOps setup, where you want to manage your infrastructure as code.
Creating and Managing Applications with ArgoCD CLI
ArgoCD allows you to create and manage applications using either the CLI or declarative configurations. This flexibility ensures that you can integrate ArgoCD seamlessly into your existing workflows.
Creating an Application Using the CLI
You can create an application in ArgoCD using the following command:
argocd app create demo-app \
--repo <taget-repo> \
--path <path> \
--dest-server https://kubernetes.default.svc \
--dest-namespace <namespace-to-deploy> \
--revision <branch> \
--sync-policy <auto/manual> \
--project <project-name> \
--sync-option CreateNamespace=true
Replace the placeholders with your specific details
This command will create an application in ArgoCD and immediately begin tracking the specified repository path.
Managing Applications Using the CLI
Once your application is created, you can manage it using the following commands:
- Syncing the Application:
argocd app sync <app-name>
This command ensures that your application is in sync with the Git repository.
- Viewing Application Status:
argocd app get <app-name>
This provides detailed information about the application’s current state.
- Deleting an Application:
argocd app delete <app-name>
- Use this command to remove an application from ArgoCD.
Declarative Application Management
Just like projects, applications can also be managed declaratively by defining them in YAML files. Here’s an example:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
spec:
project: default
source:
repoURL: https://github.com/your-repo
targetRevision: HEAD
path: <app-path>
destination:
server: https://kubernetes.default.svc
namespace: <namespace>
syncPolicy:
automated:
prune: true
selfHeal: true
This configuration file defines an application that will automatically sync with the Git repository and prune resources that are no longer defined.
To apply this configuration:
kubectl apply -f app.yaml
This method is ideal for teams that prefer to manage their applications as code, ensuring that the application configuration is stored in version control alongside the application code.
Application Rollbacks and History Tracking
ArgoCD keeps track of all changes made to your applications, allowing you to view their history and roll back to previous revisions if needed.
Viewing Application History
For example, if you’ve created an application with a deployment and service, and later changed the service name, ArgoCD will automatically sync these changes. To view the history of your application, use:
argocd app history <app-name>
Example output:
SOURCE https://github.com/anveshmuppeda/argo-cd-demo.git
ID DATE REVISION
0 2024-08-18 19:48:19 -0500 CDT main (0a5abf9)
1 2024-08-18 19:52:18 -0500 CDT main (e43f87b)
Here, you can see that the application has two revisions: one before the change (ID 0) and one after (ID 1).
Rolling Back to a Previous Revision
To roll back to a previous revision, use the rollback command with the desired revision ID:
argocd app rollback <app-name> <revision-id>
For example, to roll back to the first revision:
argocd app rollback demo-app 0
This command reverts your application to the state it was in at revision 0.
Source Code
You’re invited to explore our GitHub repository, which houses a comprehensive collection of source code for Kubernetes.
Also, if we welcome your feedback and suggestions! If you encounter any issues or have ideas for improvements, please open an issue on our GitHub repository. 🚀
Connect With Me
If you found this blog insightful and are eager to delve deeper into topics like AWS, cloud strategies, Kubernetes, or anything related, I’m excited to connect with you on LinkedIn. Let’s spark meaningful conversations, share insights, and explore the vast realm of cloud computing together.
Feel free to reach out, share your thoughts, or ask any questions. I look forward to connecting and growing together in this dynamic field!
My LinkedIn: https://www.linkedin.com/in/anveshmuppeda/
My GitHub: https://github.com/anveshmuppeda
Happy deploying! 🚀
Happy Kubernetings! ⎈