Kubernetes GitOps: A Beginner's Guide with a Hands-On Tutorial
Tags: gitops, tutorial, kubernetes, git
Published: 2023-04-08 05:49:34

Kubernetes has become the talk of the town for many years now. 
It has revolutionized the orchestration of containers and has become a leader in this field. Many developers love working with Kubernetes. While organizations prefer deploying applications on Kubernetes to cope with the scale and demanding resources, a new approach is trending regarding Kubernetes-related deployments - **GitOps**.

Yes, GitOps provides a smooth way and a set of practices to leverage simple tools like Git to manage infrastructure and application deployments. By combining Kubernetes and GitOps, organizations can reap the enormous benefits of flexibility, agility, scale, performance, efficiency and faster feature delivery. 

In this guide to Kubernetes and GitOps, I have a step-by-step beginner-level tutorial to start implementing GitOps with Kubernetes. It is time to take your application deployment to the next level. Let's get started!

## Simple GitOps Workflow
GitOps basically works on the principle of making Git the source of truth, including moving everything to code and storing and maintaining everything in Git. When it comes to deployment, make use of an operator to deploy what is configured in Git and Yaml in a declarative fashion. Since all the developers are primarily friendly with Git, GitOps simplifies their complex workflow.

![gitops workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xolzvw5562ewy0a26emj.png)

So when it comes to Kubernetes, the app code, container images, and all related manifest files will be stored in Git, and any changes are made through Git as a single source of truth. 

_Note_: The above content is taken from my other GitOps article written on the Dev community. [Read](https://dev.to/pavanbelagatti/getting-started-with-gitops-for-developers-4aej)! 

Today, we will show you how to get started with the GitOps approach for your application in the below tutorial. 

## Prerequisites
- In this tutorial, we will use a continuous delivery tool such as Harness to do GitOps. Hence, create a free [Harness account](https://app.harness.io/auth/#/signup/?module=cd&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_gitops_article&utm_term=get-started). It uses Argo CD-as-a-Service.
- GitHub account and a sample repo to experiment with. I have a [sample repo](https://github.com/pavanbelagatti/argocd-example-apps) you can fork it and use. 

## Tutorial
Signup to Harness account, verify your email and sign back in again to set up the GitOps pipeline.

You will be presented with the below deployment options - **Kubernetes** and **Kubernetes with GitOps**. 

Select '**Kubernetes with GitOps**' and continue.
![Kubernetes with GitOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/354716di5f5c27n1rn0m.png)

You don't have to worry about any set up and installation. Harness takes care of everything. It connects to Harness by provisioning hosted agent.
![hosted agent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmq2gg8ex8fhn567rz83.png)

You can click 'provision' and wait for some time to get green checkbox for your agent. 
![provision](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ij7zkohxdhfgplrsqcqr.png)

Once the agent gets installed successfully, get to the next configuration.

This is where we provide simple details like source, authentication and Git details.

Select Git and own source code from the options. Add our Git repo link - https://github.com/pavanbelagatti/argocd-example-apps

**Note:** Above shown is my GitHub repo link, you should add your repo link, the one you forked at the beginning of this tutorial. 

Once you add these details, make sure to add 'Anonymous' to authenticate your details. [BTW, its a public repo and hence no authentication is needed with username and password]
![details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/daw25b7gf5p9r3kosz3s.png)

Make sure the authentication is successful.
![authentication is successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2n8o0dx6p9i7zqab3fb.png)

Next, let's add the other required Git details as shown below.
![git details add](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwwgv8htrqa3ha1jzpps.png)

Select Target revision as 'master' and Path as 'helm-guestbook'
![git repo details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gzqloilmt7wm5g41ofd.png)

Next, select 'Harness Hosted' to test the GitOps workflow. You can choose self-managed too. But for this tutorial, we will select 'Harness Hosted'. 
![harness hosted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtym7hmxdkddygtte2d0.png)

Click the 'Connect to Cluster' button and make sure the connection is successful.
![connect to cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ayjw2is97g71ixke0z4.png)

Next, you will see the last step where we are all set to deploy the application.
![deploy app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3h6lxak4rtqo0zqux6r.png)

Click on 'Create and Sync Application'. 

This is the GitOps dashboard you will enter where all your GitOps details will be present.
![gitops dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj9e344qn7krdls7et8e.png)

You will see the 'Sync' status running. 
![sync status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmvs1047n43zhwvk01da.png)

In a minute or two, we can see the successful message.
![sync successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0adbjoh22ox9o8og4ja.png)

You can check each tab starting from the 'Resource View'
![resource view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9t18anv3il18804y68h7.png)

These are the 'App Details' tab features shown below.
![app details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5g52zdvj6oazo6m0a10h.png)

In the same tab, you can see the sync policy, it is set as 'manual', change it to 'Automated'.
![automated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csxbwmi6akuqowir60r3.png)

![auto-sync](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/naoiebgaz13t2x3kfc9s.png)

This means, when any changes happen to your GitHub repo, the GitOps agent will pick those changes automatically and syncing will happen in an automated fashion.

The next tab shows the sync status of the application.
![sync status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2k2jqfak2fqems3c7k3.png)

The manifest tab shows the manifest details of the application. 
![manifest details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imw88905zi1neu8f9sjb.png)

In the main dashboard, go to the 'GitOps' tab is the entry to your GitOps workflows. 
![gitops workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uk5s67armp3zv6a7f7gg.png)

Once you click on the 'GitOps' tab, you should see the overview of your GitOps dashboard. 
![gitops dashboard new](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jnoeiklvbu6or3ng7sf.png)

If you click on the application, you should see our application. 
![application tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhn8to2573r1xzesww8g.png)

You click on it and go to the Resource view. You will see 4 replicas running as mentioned in our `values.yaml` file. 
![values file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsy71d0eee5gxk3spahy.png)

This is our values.yaml file from the example repo.

```
# Default values for helm-guestbook.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 4

image:
  repository: gcr.io/heptio-images/ks-guestbook-demo
  tag: 0.1
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - chart-example.local
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #  cpu: 100m
  #  memory: 128Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}
```

Now, change the `replicaCount: 4` from 4 to 5. 
![replicas changed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cil27c7smh2xstbp2m4h.png)

You should see that the GitOps agent will see this difference and picks it up. Suddenly the application configuration goes out of sync. Takes some time to match up and gets back to the healthy and successful sync state. 
![app config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/raxnsfgjccbr14q2t4ma.png)

You can see the second deployment as 'Automated' since we changed the sync policy to automated. 
![automated sync](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b18d2bp6ds4trg6w7g5h.png)

Well, after some time, the application configuration state shows a healthy and successful sync state. 
![healthy and success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxigyqovclx2z1gur1ad.png)

In the above resource view, you can see that the replicas have been matched to 5 as we wanted 5 replicas. 

As GitOps uses Git as the source of truth, it keeps a record of who committed what code and if someone changed something in the repository. You can see this from our GitOps dashboard.
![git chanaged](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9m7qwg48hr21orvg9s41.png)

Once you click on that id number shown in red box above, you can see what changed.
![replica count changed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55zrkgqb1cc5orqci3p8.png)

This is how intuitive and simple it is to deploy applications via the GitOps way using Harness CD.

Kubernetes GitOps is a fantastic approach to handling complex cloud-native application deployments. Since it uses a simple tool like Git that developers already know, it becomes easy to learn and streamline your application deployment approach. Make your applications more stable, agile and efficient with Kubernetes GitOps. _Get started with GitOps today!_