# Deploying Your First Application with Argo CD

Now that you have a good understanding of Argo CD, let's dive into deploying your first application using this powerful tool. We’ll walk you through the entire process, from creating a sample application to monitoring your deployment. By the end, you should feel confident in using Argo CD to manage your Kubernetes applications.

## Prerequisites

Before we get started, make sure you have:

1. **Access to a Kubernetes Cluster**: You can use a local cluster with Minikube, KinD, or a cloud provider such as GKE, AKS, or EKS.

2. **Argo CD Installed**: If you haven’t installed Argo CD yet, you can follow these commands:

   ```bash
   kubectl create namespace argocd
   kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
  1. kubectl CLI: Ensure your kubectl command-line tool is configured to communicate with your Kubernetes cluster.

  2. Git Repository: A git repository (could be on GitHub, GitLab, etc.) containing the Kubernetes manifests for your application.

Step 1: Create Sample Application

For this example, let’s create a simple "Hello World" web application using a basic Nginx deployment. Start by creating a directory for your application manifests locally.

mkdir my-hello-world-app
cd my-hello-world-app

Create the following directory structure:

my-hello-world-app/
│
├── deployment.yaml
├── service.yaml
└── kustomization.yaml

deployment.yaml

This file will define your application deployment.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
  labels:
    app: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: nginx:alpine
        ports:
        - containerPort: 80

service.yaml

This file defines a service to expose your application.

apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: hello-world

kustomization.yaml

This file combines your deployment and service for ease of use.

resources:
  - deployment.yaml
  - service.yaml

Now, you can push this directory to your git repository to make it available for Argo CD.

Step 2: Create a New Application in Argo CD

After pushing your code to your git repository, navigate to the Argo CD dashboard (http://:). You can also forward the Argo CD server port for access using the command:

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

You’ll need to log in to Argo CD. The username is admin, and the password can be retrieved with the following command:

kubectl get pods -n argocd
kubectl describe secret argocd-initial-admin-secret -n argocd

Navigate to the "Applications" section, and click "New Application".

Application Configuration

Fill out the form with the following configurations:

  1. Application Name: hello-world

  2. Project: default

  3. Source:

    • Repository URL: https://github.com/<your-username>/my-hello-world-app.git (replace with your actual repository URL)
    • Revision: HEAD
    • Path: . or the specific path if you change it.
  4. Destination:

    • Cluster: https://kubernetes.default.svc
    • Namespace: default (or any other namespace where you want to deploy)
  5. Sync Policy: Use manual for starters; this keeps you in control.

Click Create.

Step 3: Sync Your Application

Your application should now be listed in the Argo CD dashboard in an "OutOfSync" state. This means that while Argo CD can see your repository, it hasn't deployed anything yet.

To sync your application, click on the application name, then click the Refresh button, and select Sync from the dropdown:

Sync Options

  • You can select Automatic or Manual. For your first deployment, use Manual.
  • After making your selection, click Synchronize.

A new dialog box will pop up displaying a summary of changes that will be made. Confirm by clicking Synchronize once more.

Step 4: Verify Your Deployment

After a successful sync, you can check the application status in the dashboard. You should see it moving from OutOfSync to Synced.

To verify that your application is running, you can either:

  1. Visit the external IP (if you used a LoadBalancer type service):

    kubectl get svc
    
  2. Or access the application using port-forwarding:

    kubectl port-forward svc/hello-world 8080:80
    

    Open your browser and navigate to http://localhost:8080.

Step 5: Workflow Validation

Make a change in your application code or manifests. For example, you can edit the deployment.yaml file to change the image tag from nginx:alpine to nginx:latest and push the changes to your git repository:

image: nginx:latest

Return to the Argo CD dashboard where you should see the application becoming OutOfSync again. You can resync the application as you did earlier to apply the updates.

Monitoring and Managing the Application

In addition to syncing changes, Argo CD provides powerful monitoring tools. You can view logs, health status, and metrics directly in the UI.

Rollback a Deployment

If something goes wrong or you want to revert back to a previous version, you can use the history and rollback features in Argo CD. Click on the "History" tab in your application view, and choose the desired previous revision to roll back.

Conclusion

Congratulations! You've successfully deployed your first application using Argo CD. With this powerful continuous deployment tool, you can be assured of a smooth experience managing your Kubernetes applications.

As you continue exploring Argo CD, consider diving deeper into advanced topics like automated health checks, self-healing applications, and integrating with CI/CD pipelines. Happy deploying!