Karan Sharma

GitOps approach to Continuous Delivery for Kubernetes

6 minutes (1535 words)

In this post, I’d like to share my experience and learnings about configuring a deployment pipeline for Kubernetes. I’ll be using Gitlab CI/CD and AWS EKS to demonstrate the concept, but the core idea remains the same: all changes must come declaratively from a single source of truth. GitOps is a relatively newer term in the town but goes back to the fundamentals of Infra as Code.

GitOps fundamentally is an operating model to perform tasks on Kubernetes related to deployments, configuration, secrets and monitoring workloads. All kind of changes must be performed via a single place, which happens to be a git repo. Benefits of that are what basically benefits of version controlling the code is. So why treat infra as any different? git happens to be the single source of truth for your infra, rollbacks are easy as reverting to last known good configuration and every change can be observed/verified.

πŸ”—Goals

A lot of tutorials/blog posts hitherto cover a very basic scenario where they do kubectl apply and voila the deployment’s live. However we all know things are very different (to say the least) in production, so this post will cover all aspects of deployment:

πŸ”—Basics

A GitOps workflow looks like:

πŸ”—Push vs Pull

There are 2 approaches to how you can handle deployments to a cluster. In a Pull based approach, the cluster runs a synch controller program which continmioously syncs the state of cluster with a Git repo. Any changes you make to the Git repo will be synced automatically in cluster. The idea is that there should be no drift in the desired state via Git repo and the actual state of cluster. Flux, Argo are good tools if you want a Pull based pipeline. The merits of Pull based pipeline is it’s more secure, since the deployment is actually happening inside cluster and no external sytstem needs to communicate to your production infra. The demerits are sometimes you’ve to wait for the changes to be synced (every controller runs these sync process in a loop with a sleep which can be configured). Also using any kind of preprocessing tools like Kustomize becomes difficult, since Flux just syncs the state and applies those changes. Handling of secrets is yet another concern, you need to look . And finally GitOps is a relatively newer tech in market so GitOps tooling is still nascent and like with any other relatively (non battle tested) software you’re gonna find bugs.

Push Approach however is a traditional CD approach, where the CD server talks to the cluster and applies changes through commands. In context of normal EC2 deployments, those commands could be SSH into server, running ansible playbook etc. In context of K8s however kubectl does the magic for us. The CD server needs to talk to the K8s API server and run kubectl commands to change the cluster state. The merits of this approach are you can run all sorts of commands inside deployment pipeline and make it fully customisable. Handling of secrets also can be handled natively (like Gitlab env variables) or encrypted in git. The demerits is that your production cluster is now exposed to your CD server.

Overall, if you have an airgapped CD server with no inbound ports open, access controll the user auth to CD, I found the Push approach to be more preferable. YMMV.

πŸ”—Writing the pipeline

I’ve created a docker image eks-gitops which I’ll be using throughout the pipeline. This container image contains popular tools like kustomize, kubeval etc and scripts to configure access to cluster using kubectl using aws-iam-authenticator. I’ve written more about how RBAC works inside EKS here.

Excerpt from .gitla-ci.yml:

# Use this as base image for all jobs unless overriden
default:
  image:
    name: mrkaran/eks-gitops:latest
    entrypoint: ["/bin/sh", "-c"]
### Pipeline
stages:
  - validate
  - deploy

πŸ”—Prepare the manifests

I use Kustomize to prepare the manifests. Advantage of Kustomize is writing template free YAMLs but still be able to customise them heavily using overlays. For different environments, you can apply certain changes like increasing resource requests, adding more storage, while keeping the base same.

Here’s a folder structure (from a real GitOps repo) I follow for manifests:

.
β”œβ”€β”€ base
β”‚Β Β  β”œβ”€β”€ deployments
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.yml
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ celery.yml
β”‚Β Β  β”‚Β Β  └── nginx.yml
β”‚Β Β  β”œβ”€β”€ ingresses
β”‚Β Β  β”‚Β Β  └── web.yml
β”‚Β Β  β”œβ”€β”€ kustomization.yaml
β”‚Β Β  β”œβ”€β”€ services
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.yml
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ nginx.yml
β”‚Β Β  β”‚Β Β  └── redis-headless.yml
β”‚Β Β  β”œβ”€β”€ statefulsets
β”‚Β Β  β”‚Β Β  └── redis.yml
β”‚Β Β  └── volumes
β”‚Β Β      └── redis.yml
β”œβ”€β”€ kubekutter.yml
β”œβ”€β”€ Makefile
β”œβ”€β”€ overlays
β”‚Β Β  β”œβ”€β”€ dev
β”‚Β Β  └── prod
β”‚Β Β      β”œβ”€β”€ configs
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ app-config.env
β”‚Β Β      β”‚Β Β  └── app-nginx.conf
β”‚Β Β      β”œβ”€β”€ kustomization.env.yml
β”‚Β Β      β”œβ”€β”€ namespace.yml
β”‚Β Β      β”œβ”€β”€ patches
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ configure-configmap-volume.yml
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ modify-alb.yml
β”‚Β Β      β”‚Β Β  └── resource-limits.yml
β”‚Β Β      └── rbac.yml
└── README.md

Shameless Plug: I created kubekutr which makes managing of these manifests using kustomize a breeze.

Some things to note here:

πŸ”—Lint yo manifest

I’m using kubeval to lint the manifests. The manifests have to prepared by kustomize. CI_ENVIRONMENT_NAME is set by Gitlab when you specify an environment for a job. Don’t sweat about this part, I’ll describe it more as we proceed.

# Validate the yaml using kubeval
.lint:
  extends: .prepare-manifest
  stage: validate
  script:
    - echo "Linting manifest for ${CI_ENVIRONMENT_NAME}"
    - kustomize build overlays/$CI_ENVIRONMENT_NAME --load_restrictor none | kubeval

πŸ”—Setup environment

You can define environment name as:

# Create an environment to record all jobs for this env
.prod: &prod
  environment:
    name: prod
    url: https://prod.site
.dev: &dev
  environment:
    name: dev
    url: https://dev.site

Any job which is to be executed in a particular environment can include this variable and CI_ENVIRONMENT_NAME will be automatically set.

A cool feature of Gitlab is that you can restrict Variables scoped to the environment they are defined in.

πŸ”—Configure Secrets

All secrets are defined as Environment Variables in Gitlab CD pipeline. While running the job, the runner has access to these variables and with the help of secretGenerator in kustomize, the Secret is created.

I use secretGenerator because any time a K8s secret changes, kustomize appends with a new suffix, which makes the Replication Controller believe that they deployment has changed. So a new deployment is automatically triggered.

πŸ”—Authenticate to cluster

EKS uses aws-iam-authenticator and uses IAM access roles to allow the cluster to perform actions. Since this is a push based pipeline, you need to allow the access from your CD server to port 443.

πŸ”—Deploy changes

This is as simple as kubectl apply which configures all the changes and diff between cluster and real world state.

Here’s a full gitops repo if you’re interested in checking it out:

Tags: #Kubernetes #CI/CD #Devops