Introduction
GitLab Runner is an open-source project that works in conjunction with GitLab CI/CD pipelines to automate the process of building, testing, and deploying software. It can run various types of jobs, including builds, tests, and deployments, based on the instructions provided in the .gitlab-ci.yml configuration file.
Why Use GitLab Runner on Kubernetes?
Scalability: Kubernetes allows for easy scaling of resources. GitLab Runner deployments can dynamically scale based on workload demands, ensuring optimal resource utilization.
Isolation: Kubernetes provides container orchestration, allowing GitLab Runner jobs to run in isolated environments (pods). This isolation ensures that jobs do not interfere with each other and provides security benefits.
Resource Efficiency: GitLab Runner on Kubernetes can efficiently utilize cluster resources by scheduling jobs on available nodes, thereby maximizing resource utilization and minimizing idle capacity.
Consistency: Running GitLab Runners on Kubernetes ensures consistency across different environments, whether it's development, testing, or production. The same Kubernetes environment can be used to run CI/CD pipelines consistently.
Key Components
GitLab Runner: The agent responsible for executing CI/CD jobs defined in .gitlab-ci.yml files. It interacts with GitLab CI/CD and Kubernetes API to schedule and run jobs in Kubernetes pods.
Kubernetes: An open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. GitLab Runner utilizes Kubernetes to manage the lifecycle of CI/CD job pods.
Helm: Helm is a package manager for Kubernetes that allows you to define, install, and manage applications on Kubernetes. GitLab Runner can be deployed on Kubernetes using Helm charts provided by GitLab.
Install Gitlab-runner on Kubernetes
1- Add the helm repo
root@master:~# helm repo add gitlab https://charts.gitlab.io
"gitlab" already exists with the same configuration, skipping
2- Update the repo
root@master:~#helm repo update gitlab
"gitlab" already exists with the same configuration, skipping
root@master:~# helm repo update gitlab
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "gitlab" chart repository
Update Complete. ⎈Happy Helming!⎈
3- Create a values.yaml file
root@master:~# cat values.yaml
env:
open:
STORAGE: local
persistence:
enabled: true
accessMode: ReadWriteOnce
size: 8Gi
storageClass: "managed-nfs-storage"
root@master:~# cat values.yaml
gitlabUrl: https://gitlab.com/
runnerRegistrationToken: "gitlab-runner-token"
concurrent: 10
checkInterval: 30
rbac:
create: true
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["list", "get", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]
- apiGroups: [""]
resources: ["pods/attach"]
verbs: ["list", "get", "create", "delete", "update"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["list", "get", "create", "delete", "update"]
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["list", "get", "create", "delete", "update"]
runners:
privileged: true
config: |
[[runners]]
[runners.kubernetes]
namespace = "gitlab-runner"
tls_verify = false
image = "docker:19"
privileged = false
4- Create the namespace and deploy the helm
root@master:~# kubectl create ns gitlab-runner
root@master:~# helm install gitlab-runner gitlab/gitlab-runner -f values.yaml --namespace gitlab-runner
NAME: gitlab-runner
LAST DEPLOYED: Mon Mar 4 22:09:02 2024
NAMESPACE: gitlab-runner
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Your GitLab Runner should now be registered against the GitLab instance reachable at: "https://gitlab.com/"
5- Verify the runner
root@master:~# kubectl -n gitlab-runner get pods
NAME READY STATUS RESTARTS AGE
gitlab-runner-7b8ff76bff-mptdc 1/1 Running 0 39s
Login to https://gitlab.com and verify the runner registration
Create a project in gitlab
Create .gitlab-ci.yml and use kubernetes as tag to run the pipeline on Kubernetes
Run the Pipeline
Run the command to verify a new runner get start in gitlab-runner
root@master:~# kubectl -n gitlab-runner get pods
NAME READY STATUS RESTARTS AGE
gitlab-runner-7b8ff76bff-mptdc 1/1 Running 0 18m
runner-kea6jzghg-project-45006412-concurrent-0-1heswzap 0/2 Init:0/1 0 56s
Verify the pipeline in Gitlab UI
We are able to run our pipeline jobs on our self-hosted runner in kubernetes