DEV Community

Cover image for Building a Kubernetes CI/CD Pipeline with GitLab and Helm
Daniel Slapelis
Daniel Slapelis

Posted on

Building a Kubernetes CI/CD Pipeline with GitLab and Helm

Everyone loves GitLab CI and Kubernetes.

GitLab CI (Continuous Integration) is a popular tool for building and testing software developers write for applications. GitLab CI helps developers build code faster, more confidently, and detect errors quickly.

Kubernetes, popularly shortened to K8s, is a portable, extensible, open-source platform for managing containerization workloads and services. K8s is used by companies of all sizes everyday to automate deployment, scaling, and managing applications in containers.

The purpose of this post is to show how you can bolt on the Continuous Delivery (CD) piece of the puzzle to build a CI/CD pipeline so you can deploy your applications to Kubernetes. But before we get too far, we're going to need to talk about Helm, which is an important part of the puzzle.

What the Helm?

Helm calls itself "the package manager for Kubernetes". That's a pretty accurate description. Helm is a versatile, sturdy tool DevOps engineers can use to define configuration files in, and perform variable substitution to create consistent deployments to our clusters, and have different variables for different environments.

It's certainly the right solution to the problem we're covering here.

How do we do it?

First off, a few prerequisites. You’re going to have to have this all hammered out before you started with the project. There’s links to helpful documentation below if you need help.

  1. You already have an Amazon EKS cluster.
  2. You already know how to use GitLab CI.
  3. You have a GitLab CI runner configured in your Kubernetes cluster.
  4. You have the AWS Load Balancer Controller running in your cluster.

With those boxes checked, we can get started. You'll want to create a new repository in GitLab first for us to use in this example. Once you've done that we can get started with creating our files.

File tree

Basically, at the end our folder/file structure is going to look like this:

├── chart/
|   ├── Chart.yaml
|   ├── values.yaml
|   └── templates/
|      ├── deployment.yaml
|      ├── service.yaml
|      ├── ingress.yaml
|      └── configmap.yaml
└── gitlab-ci.yml
Enter fullscreen mode Exit fullscreen mode


applicationName: my-first-app
certArn: your-certificate-arn
domain: your domain name
subnets: your subnets
securityGroups: your security groups
Enter fullscreen mode Exit fullscreen mode


apiVersion: apps/v1
kind: Deployment
  name: {{ .Values.applicationName }}
  namespace: {{ .Values.applicationName  }}
  replicas: 2
  revisionHistoryLimit: 2
      app: {{ .Values.applicationName }}
        app: {{ .Values.applicationName  }}
        - name: {{ .Values.applicationName }}
          imagePullPolicy: Always
          image: nginx:1.19.4
            - containerPort: 80
            - mountPath: /usr/share/nginx/html/index.html
              name: nginx-conf
              subPath: index.html
        - name: nginx-conf
            name: {{ .Values.applicationName  }}-configmap
Enter fullscreen mode Exit fullscreen mode

This is the configuration file that defines our deployment. You can see there are a few lines with {{ some text }}. This is how we use a variable we define in our values file within our chart.


apiVersion: v1
kind: ConfigMap
  name: {{ .Values.applicationName }}-configmap
  namespace: {{ .Values.applicationName }}
  index.html: |
      <h1>My first Helm deployment!</h1>
      <p>Thanks for checking out my first Helm deployment.</p>
Enter fullscreen mode Exit fullscreen mode

This config map just defines a simple index page that we'll display for our app.


apiVersion: v1
kind: Service
  name: {{ .Values.applicationName }}
  namespace: {{ .Values.applicationName }}
    - port: 80
      targetPort: 80
      protocol: TCP
      name: {{ .Values.applicationName }}
    - port: 80
      targetPort: 80
      protocol: TCP
      name: {{ .Values.applicationName }}
  type: NodePort
    app: {{ .Values.applicationName }}
Enter fullscreen mode Exit fullscreen mode


apiVersion: extensions/v1beta1
kind: Ingress
  name: {{ .Values.applicationName }}
  namespace: {{ .Values.applicationName }}
  annotations: alb {{ .Values.subnets }} / {{ .Values.securityGroups }} internet-facing  {{ .Values.certArn }} '[{"HTTP": 80}, {"HTTPS":443}]' '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
    - host: {{ .Values.applicationName }}.{{ .Values.domain }}
        - path: /*
            serviceName: ssl-redirect
            servicePort: use-annotation
        - path: /*
            serviceName: {{ .Values.applicationName }}
            servicePort: 80
Enter fullscreen mode Exit fullscreen mode


  - deploy

  DOCKER_HOST: tcp://localhost:2375/
  DOCKER_DRIVER: overlay2
  APP_NAME: my-first-app

  stage: deploy
  image: alpine/helm:3.2.1
    - helm upgrade ${APP_NAME} ./charts --install --values=./charts/values.yaml --namespace ${APP_NAME}
    - if: $CI_COMMIT_BRANCH == 'master'
      when: always
Enter fullscreen mode Exit fullscreen mode

Okay we have all the files. Now what?

Well, after you have all the files defined and your infrastructure follows our prerequisites, there's not much left to do.

If you commit these files, GitLab will interpet your .gitlab-ci.yml file and initiate a pipeline. Our pipeline only has one stage and one job (deploy). It'll spin up a container in the cluster for the deployment using the helm:3.2.1 image and run our script command. This does all of the heavy lifting for us with creating all of the files required in our namespace and starting our application.

If you configure in Route53 a DNS record like with an A record to the load balancer that the ingress controller created, you'll see the index page we defined in the configmap!

This post first appeared on our blog where we write about devops and devops consulting services.

Top comments (1)

feritaly profile image

Hello, how is everything?

Congratulations on the document.

I was applying some concepts with a helm ...

By chance, this doc is missing the helm file , Chart.yaml?
can you share it with us?