GitOps w/ ArgoCD a Tutorial
Tags: devops, tutorial, git, kubernetes
Published: 2020-03-29 03:18:32

---
title: GitOps w/ ArgoCD a Tutorial
published: true
description: This is a step by step on how to get ArgoCD up and running locally on a k3s cluster, and also demonstrate a bit of what ArgoCD is capable of. This will get you up and running on a local k3s cluster, so you don't need your own existing Kubernetes cluster!
tags: devops, tutorial, git, kubernetes
cover_image: https://drive.google.com/uc?export=view&id=1hBoastp6Rr6blDHMSMHYzu-sUgpyt7Hs
---

This is a step by step on how to get ArgoCD up and running locally on a k3s cluster, and also demonstrate a bit of what ArgoCD is capable of. This will get you up and running on a local k3s cluster, so you don't need your own existing Kubernetes cluster!


#### Some info before you jump in!
> If you have a Kubernetes cluster already that you'd like to run ArgoCD in and have *kubectl* configured for it already, you can skip installing ***k3d*** and skip any ***k3d*** related steps. 

> If you are using a GKE cluster, there is one extra step that you must take. You will need grant your account the ability to create new cluster roles with the following command: 
```bash 
$ kubectl create clusterrolebinding YOURNAME-cluster-admin-binding --clusterrole=cluster-admin --user=YOUREMAIL@gmail.com
```


#### Now let's Git into it! (*I know, I know. Hilarious pun.*)

Prereqs:
>[Docker](https://www.docker.com/get-started)
[K3d](https://github.com/rancher/k3d)
[ArgoCD CLI](https://argoproj.github.io/argo-cd/cli_installation/)
[Kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
[Repo used for tutorial](https://github.com/Dizdarevic/gitops-brown-bag) (*Feel free to fork it, for your convenience.*)
[Docker image and versions used](https://hub.docker.com/repository/docker/dizdarevic/express-app)

##### Spin up k3d/k3s
```bash 
$ k3d create
```

##### Configure *kubectl* to your k3s cluster
```bash 
$ export KUBECONFIG="$(k3d get-kubeconfig --name='k3s-default')"
```

##### Deploy argocd into cluster 
```bash
$ kubectl create namespace argocd
$ kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
```

##### Access ArgoCD UI through port-forwarding
```bash 
$ kubectl port-forward svc/argocd-server -n argocd 8080:443
```
> Head over to http://localhost:8080/ where you should find the ArgoCD UI. You should see a login page. Your username is *admin* and your password is actually the name of your running server. You can get that with the following command:

```bash 
$ kubectl get pods -n argocd -l app.kubernetes.io/name=argocd-server -o name | cut -d'/' -f 2
```

> Then you'll see the following screen! Congrats, you got ArgoCD running in your cluster!

> ![ArgoCD UI](https://drive.google.com/uc?export=view&id=1WRVxu1z6hCIPkQCiSy_NgceCqYUljvUZ "ArgoCD UI")

##### Login to ArgoCD through CLI:
> You'll need to login to the running ArgoCD server through the CLI so that you can deploy applications through the CLI with ArgoCD. You could also simply do it through the UI, but we will proceed here with the CLI.

```bash 
$ argocd login localhost:8080
```

> Your password here is the same as the one from the previous step.

##### Configure app repo with ArgoCD
> Since we are using the example repo I've provided, you'd simply replace the repo in the command with your own. Also, replace the path to the deployment directory to the path specified in your own repo.

> *In true GitOps fashion, you would* **NOT** *hold both the source code and the deployment manifests in the same repo. I've only done so for the sake of a more simple demonstration.*

> We are deploying into the `default` namespace, which you typically wouldn't, but for the sake of the demo it should be fine.

```bash
$ export MY_APP_NAME=express-app
$ export MY_REPO_NAME=https://github.com/Dizdarevic/gitops-brown-bag.git
$ export MY_DEPLOYMENT_PATH=example-app/deployment-repo
$ argocd app create $MY_APP_NAME --repo $MY_REPO_NAME --path $MY_DEPLOYMENT_PATH --dest-server https://kubernetes.default.svc --dest-namespace default
```
> After which, you should see the application show up on the ArgoCD dashboard:

> ![ArgoCD UI App](https://drive.google.com/uc?export=view&id=1N2Ey_NxUWZF6Eejrzt6AITXZL4ZL5jFF "ArgoCD UI App")
> You'll see that the current state of the deployment is `out-of-sync` with the deployment configuration in your git repository.
> Click on the `Sync` button, in the UI and then click `Synchronize`.
> Now you can watch your app/server deploy!

> ![ArgoCD UI App](https://drive.google.com/uc?export=view&id=18lJ98Y9qFdQfuBuzEmJo1cZDcLMt9e3r "ArgoCD UI App")

##### Expose the **v1** docker image (stated in the prereqs) with ArgoCD
> I'm specifically exposing `3000`, because this is the port defined in the deployment configuration as well as the port that is exposed in the docker image being used. You can do so with the following command:

```bash 
$ kubectl port-forward svc/express-app -n default 3000:3000
```

> Now you can visit http://localhost:3000/ and see your first server deployed to a cluster with ArgoCD! Congrats!

##### Deploy **v2** and watch it redeploy
> Before we begin, stop port forwarding on port `3000`. It will break anyways once we redeploy a different version.

> Now we will modify the version of the docker image being used in the deployment repo.

> Change the `image tag` in the [deployment repo](https://github.com/Dizdarevic/gitops-brown-bag/blob/master/example-app/deployment-repo/express-app-deployment.yaml) from `dizdarevic/express-app:v1` to `dizdarevic/express-app:v2`

> Go back to the ArgoCD UI. Click `Refresh` on the application and you should see that it is `out-of-sync` again. Do the same synchronization steps as before. Allow the new version of the service to spin up, and for the old one to spin down.

> Follow the same steps as before to expose the service on port `3000`. Once you visit http://localhost:3000/ again, you should see a different message that shows you are using the new docker image!

##### Spin down everything after you're doing poking around!
```bash 
$ k3d delete
```