How to install gitlab-runner on k3s with helm chart

Bring your own runner ;)

Prerequisites:

  • Have some kind of k8s cluster running (inclusing k3s)
  • have one or more gitlab repo’s

Introduction

This will learn you how to deploy a simple gitlab runner in k3s / k8s with the registration token in a k8s secret.

Token

Since gitlab 16.0 you are required to register your runners in a new fashion. Please refer to your git repo and get a new token per runner. Store this in a secret as explained underneath.

Gitlab runner

Create a gitlab namespace

kubectl create namespace gitlab

Create a directory and values.yaml in your git repository:

mkdir gitlab
touch values.yaml

Example config of values.yaml:

gitlabUrl: https://gitlab.com/
concurrent: 2
rbac:
  create: true

runners:
  namespace: gitlab
  secret: gitlab-runner-secret
  config: |
    [[runners]]
      [runners.kubernetes]
        image = "debian:11"    

  ## Specify the name for the runner.
  name: "gitlab-runner"

Kubernetes secrets:

Get the registration token from:

base64 encode your registration token:

echo -n '<unencoded-token>' | base64

Create a yaml file as follows, and paste the tokens in there:

apiVersion: v1
kind: Secret
metadata:
  name: gitlab-runner-secret
  namespace: gitlab
type: Opaque
data:
  runner-registration-token: "<base64-encoded-token>"
  runner-token: ""

Create the secret based of the yaml file:

kubectl apply -f ./gitlab-runner-secret.yaml

How to install the runner on the cli:

sudo apt install helm
helm repo add gitlab https://charts.gitlab.io
sudo helm repo update

helm install --namespace gitlab gitlab-runner -f gitlab/values.yaml gitlab/gitlab-runner
Succes:

image

useful tips and commands:

If for some vague reason your .kube dir and or config have disappeared. You can use this to create a new one:

mkdir ~/.kube
touch ~/.kube/config
sudo kubectl config view --raw > ~/.kube/config

You can export your kubeconfig with the following command:

sudo export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

or (without sudo perms)

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

Check the permissions of you k3s.yaml as such:

ls -ltrah /etc/rancher/k3s/k3s.yaml
-rw------- 1 vuurvoske vuurvoske 3.0K Feb 24 15:40 /etc/rancher/k3s/k3s.yaml

note: please note that if changed it to my own user so i do not need sudo permissions to create deployments etc. It might so that on your system root is the owner. Then use sudo.

Used documentation: