Beginner's Guide to Argo CD: Streamlining Kubernetes Deployments with GitOps
Tags: argocd, devops, kubernetes
Published: 2024-02-12 13:37:58

## The Traditional CI/CD Pipeline

Before Argo CD, deploying applications in Kubernetes usually followed a traditional CI/CD pipeline approach.

Developers pushed code changes to Git, triggering automated tests for quality assurance. Upon successful testing, code was built into Docker images and stored in a registry. These images were then linked to deployment manifests, which were manually updated and applied to the Kubernetes cluster using kubectl. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90yf94sck20slhmh3njh.png)
Let's break it down. Imagine you've developed an application and need it to run on a cluster with specific configurations, such as Pods and Services with external IPs. This setup represents the desired state of your application.

Now, traditionally, whenever there's a change in your application's repository or if the desired state isn't met in the cluster, manual intervention was required. This involved checking the cluster and making adjustments to ensure it matched the desired state, which could lead to mistakes and errors.

With Argo CD, it's designed to simplify application deployment with its features, streamlining the entire process using GitOps principles. With Git as the source of truth for both your application, you can also keep track of the health and sync status of application deployment and get notifications for any changes. If anything goes wrong with new updates, you can easily undo changes with the rollback feature.

## Introduction to Argo CD:

- Argo CD is a declarative, GitOps continuous delivery tool for Kubernetes.
- It's designed to simplify and automate the deployment and management of applications, ensuring continuous synchronization with the latest code changes.
- Argo CD is built with the GitOps model at its core, where the repository becomes the primary source of truth for your application's state. your repository stores essential components needed for your application, such as Kubernetes manifests, Kustomize templates, Helm charts, and configuration files. The components stored in the repository act as a roadmap, showing the steps needed to deploy your app successfully.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4g4a7fjsnwseklebptbj.png)

## How Argo CD Works

- Argo CD operates on a GitOps-based continuous delivery (CD) model with a pull-based design.
- Argo CD operates with two repositories: one for your application and another for defining the desired state of the cluster. It's like having a blueprint for how your cluster should be configured.
- When Argo CD notices any differences between the current state of your cluster and the defined blueprint (Git manifest), it works to make them match.
- If everything matches the blueprint, it shows "Synced." If not, it shows "OutOfSync," indicating there are differences to resolve.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgnjhog70aqt64gurjok.png)

## Prerequisites

- Basic understanding of managing a Kubernetes cluster, including concepts like pods, services, and namespaces.
- Familiarize with YAML, as Argo CD configuration files and Kubernetes manifests are commonly expressed in YAML format.
- You need a running kubernetes cluster.
- Installed kubectl command-line tool.

## Install Argo in Your Cluster
Begin by creating a dedicated namespace for Argo CDusing the following command:
```
PS C:\Users\hp> kubectl create namespace argocd
```
Next, apply Argo CD YAML manifest to your cluster using kubectl. This will install the Argo CD API, controller, and Custom Resource Definitions (CRDs):

```
PS C:\Users\hp> kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
```
Monitor the deployment progress by checking the status of Argo components:

```
PS C:\Users\hp> kubectl get deployments -n argocd
NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
argocd-applicationset-controller   1/1     1            1           26h
argocd-dex-server                  1/1     1            1           26h
argocd-notifications-controller    1/1     1            1           26h
argocd-redis                       1/1     1            1           26h
argocd-repo-server                 1/1     1            1           26h
argocd-server                      0/1     1            0           26h
guestbook-ui                       1/1     1            1           25h
```
### Connecting to Argo:
Argo CD does not automatically expose its API server externally. You can connect to it using one of the following methods:

1. Service Type: LoadBalancer                                                                         If you prefer to use a LoadBalancer, you can change the service type of the argocd-server to LoadBalancer.

2. Ingress
                                                                                            If you have an ingress controller set up, you can configure an ingress resource to direct traffic to the argocd-server service.

3. Port Forwarding                                                                                  Use port forwarding to access Argo CD locally on your machine:

To access Argo CD locally on your machine, you can use port forwarding. Run the following command in your terminal:

```
PS C:\Users\hp> kubectl port-forward svc/argocd-server -n argocd 8080:443
```
This command redirects your local port 8080 to port 443 of Argo CD service. Now you can access your ArgoCD dashboard at 127.0.0.1:8080.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ox9lqef8zk7z6kk62ao.png)

#### Obtaining the Initial Password:
Before you can login, you need to retrieve the initial password for the default admin user. This is generated automatically during Argo’s installation process. 
You can access it by running the following argocd command:
```
PS C:\Users\hp> argocd admin initial-password -n argocd
V0l6LBKZXESYk6Gh
```
Use these credentials to login to Argo.

To interact with Argo CD from the command line interface (CLI), you'll need to install the Argo CD CLI tool. Refer to the official documentation for detailed instructions on installing the CLI: [ArgoCD CLI Installation.](https://argo-cd.readthedocs.io/en/stable/cli_installation/)

Login to Argo CD CLI:                                                                                       Use the initial password obtained earlier to log in to Argo CD.
```
PS C:\Users\hp> argocd login localhost:8080
```
Once logged in, you can update the password using the following command:

```
PS C:\Users\hp> argocd account update-password
```

## Practical Example: Deploying an Application with Argo CD 



### Create Application using CLI



#### Step 1: Create Kubernetes Manifest for Application
Begin by creating a Kubernetes manifest for your application. For this demonstration, we'll utilize the guestbook example from the Argo CD official example apps repository available at https://github.com/argoproj/argocd-example-apps.

#### Step 2: Create Argo CD Application Manifest
Before deploying your application with Argo CD, you need to create an application.yaml configuration file. This file contains essential settings such as the repository URL, path to manifests, namespace, and other configurations.
Next, create an Argo CD Application manifest for the guestbook example:

guestbook-app.yaml:

```
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: guestbook
spec:
  destination:
    server: 'https://kubernetes.default.svc'
    namespace: default
  source:
    repoURL: 'https://github.com/argoproj/argocd-example-apps'
    path: guestbook
    targetRevision: HEAD
  project: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
```

#### Step 3: Apply Argo CD Application Manifest
Apply the Argo CD Application manifest using the following command:
```
PS C:\Users\hp> kubectl apply -f guestbook-app.yaml
```
After deploying the application, review its status and configuration details in the Argo CD dashboard.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ad7tz4eq5dtu751sfki5.png)

### Create Application using UI

#### Step 1: Log into Argo CD

Use the admin user and its password, log into Argo CD
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o066zcsjarte57bk9i9z.png)

#### Step 2: Create Application

Let's create an Argo CD application using the guestbook example from the Argo CD official example apps repository.
https://github.com/argoproj/argocd-example-apps
Click the “+ New App” button

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qsxyblnxjtv5h6zdfyaw.png)

Enter the following information:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgxvprtkc6bb4qrda4gr.png)

Git Repo (Source/desired state):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpez2ioysv12k3n8zvk9.png)


Destination:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71dk2v98bovluig5dpjp.png)


Then click “CREATE” button at the top.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pe9bi4l9v71uzxcqkw80.png)


Wait for a few minutes until the app “guestbook” is deployed successfully.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozwn8yswzw98wrt2s0di.png)

After deployment, verify that the service is running by executing:

```
PS C:\Users\hp> kubectl get svc -n argocd
```
Additionally, you can access the guestbook app through port forwarding:

```
PS C:\Users\hp> kubectl port-forward svc/guestbook-ui -n argocd 8081:80
```
Access it from a web browser on http://localhost:8081

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fds3ep2nbrlgdpza0tqn.png)
Or verify using the Argo CD command-line tool:
```
PS C:\Users\hp> argocd app get guestbook
Name:               argocd/guestbook
Project:            default
Server:             https://kubernetes.default.svc
Namespace:          argocd
URL:                https://localhost:8080/applications/guestbook
Repo:               https://github.com/argoproj/argocd-example-apps.git
Target:             HEAD
Path:               guestbook
SyncWindow:         Sync Allowed
Sync Policy:        Automated
Sync Status:        Synced to HEAD (d7927a2)
Health Status:      Healthy

GROUP  KIND        NAMESPACE  NAME          STATUS  HEALTH   HOOK  MESSAGE
       Service     argocd     guestbook-ui  Synced  Healthy        service/guestbook-ui created
apps   Deployment  argocd     guestbook-ui  Synced  Healthy        deployment.apps/guestbook-ui created
```
After defining your application in the Argo CD Application Manifest, any updates or changes to your Kubernetes manifests, such as deployments or services, can be made directly in your code. Once you've made your changes, simply push the updated files to your synced GitHub repository. Argo CD will automatically detect these changes and handle the deployment process, ensuring that your application stays up to date with the latest configurations.

## Wrap-Up

With this guide, we've explored the basics of Argo CD. There is an advanced features to explore within Argo CD. Advanced features like integration notifications, ApplicationSets, RBAC, and more.
For more information refer the official - [Argo CD Documentation](https://argo-cd.readthedocs.io/en/stable/)

Additional Resources:
[Argo CD: The De Facto Continuous Delivery Tool for Kubernetes](https://spacelift.io/blog/argocd)