Skip to content

Deploy to k8s with gitlab pipeline

Kubernetes GitLab

GitLab kubernetes integration

For this guide to work, you will need to integrate Gitlab with your EKS cluster

Gitlab has great documentation for that here:

Adding and removing Kubernetes clusters

Integrating with GitLab

You can follow the full guide os setting up EKS with eksctl.

This part mentions GitLab Integration

Now that Gitlab is configured with our EKS cluster details,
It allows you to deploy your apps to k8s directly from gitlab CI.

To demonstrate this we will deploy an nginx app, only this time the container image is hosted in our private gitlab registry.

We will use the namespace lab.

This namespace should be allowed to pull images from your private gitlab registry.

So first Create a Token and make sure you can login to gitlab registry and pull images with it.

This token should have readonly permissions for your registry.

Gitlab Personal Token

Once it the Gitlab Token is working, we should configure it in kubernetes.

For that you should create imagePullSecrets in your name namespace, as described here:

Create K8S Secret

Let's go back to our nginx project.

(open the project in your IDE or Gitlab UI)

You can recall what we did here:

Create Release stage

Create our deployment

Creating the deployment definition:

Create the file /manifests/deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
    app: nginx
spec:
replicas: 3
selector:
    matchLabels:
    app: nginx
template:
    metadata:
    labels:
        app: nginx
    spec:
    containers:
    - name: nginx
        image: __IMAGE__
        ports:
        - containerPort: 80
    imagePullSecrets:
    - name: regcred

In this file we set the place holder __IMAGE__ so we can later on replace it with our released image tag.

We also set imagePullSecret. These are credentials required to access gitlab registry.

Note about secret

As mentioned it must be created for each namespace.

CI/CD Pipeline

Creating the GitLab Pipeline:

Edit the .gitlab-ci.yml, clear all lines and add the following instead:

stages:
- build
- release
- build-release
- deploy

variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
NGINX_VER: "1.17.8"

build:
stage: build
script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build --build-arg NGINX_VER=${NGINX_VER} -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE

release:
stage: release
image: registry.gitlab.com/juhani/go-semrel-gitlab:v0.20.0
script:
    - release next-version --allow-current
    - release changelog
    - release commit-and-tag --create-tag-pipeline CHANGELOG.md
only:
    - master
except:
    - tags
when: manual
dependencies: []

build-release:
stage: build-release
script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build --build-arg NGINX_VER=${NGINX_VER} -t $IMAGE_TAG .
    - docker tag $IMAGE_TAG $CI_REGISTRY_IMAGE
    - docker push $IMAGE_TAG $CI_REGISTRY_IMAGE
environment:
    name: master
only:
    - tags
dependencies: []

deploy:
image: lachlanevenson/k8s-kubectl:latest
stage: deploy
script:
    - cd manifests/
    - sed -i "s/__IMAGE__/${IMAGE_TAG}/" deployment.yml
    - kubectl apply -f deployment.yml -n lab
    - kubectl rollout status -f deployment.yml -n lab
    - kubectl get pods,svc -n lab -o wide
tags:
    - kubernetes
only:
    - tags

Commit & Push

Commit and push the changes with commit message:

feat(CI): deploy to k8s

The git push to the repository will trigger the pipeline.

Deploy to k8s

Once the build stage is complete, click on the created release stage and it should start.

Then, the deploy stage will be created, and you should see the output at the pipeline.

Summary

Here is what we did so far in this tutorial:

We built, tagged, pushed released and now also → deployed our app to k8s using gitlab CI.

Comments