How to deploy gitlab-runner on kubernetes and automatically register the runner?

6/28/2019

I'm new to gitlab ci/cd. I want to deploy gitlab-runner on kubernetes, and I use kubernetes to create two resource:

gitlab-runner-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: gitlab-runner
  namespace: gitlab
data:
  config.toml: |
    concurrent = 4

    [[runners]]
      name = "Kubernetes Runner"
      url = "http:my-gitlab.com/ci"
      token = "token...."
      executor = "kubernetes"
      tag = "my-runner"
      [runners.kubernetes]
        namespace = "gitlab"
        image = "busybox"

gitlab-runner-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gitlab-runner
  namespace: gitlab
spec:
  replicas: 1
  selector:
    matchLabels:
      name: gitlab-runner
  template:
    metadata:
      labels:
        name: gitlab-runner
    spec:
      containers:
      - args:
        - run
        image: gitlab/gitlab-runner:v11.11.3
        imagePullPolicy: Always
        name: gitlab-runner
        volumeMounts:
        - mountPath: /etc/gitlab-runner
          name: config
        - mountPath: /etc/ssl/certs
          name: cacerts
          readOnly: true
      restartPolicy: Always
      volumes:
      - configMap:
          name: gitlab-runner
        name: config
      - hostPath:
          path: /usr/share/ca-certificates/mozilla
        name: cacerts

The problem is that after creating the two resource using kubectl apply. I can't see the runner instance in http://my-gitlab.com/admin/runners. I suspect the reason is that I have not register the runner. And I enter the runner pod pod/gitlab-runner-69d894d7f8-pjrxn and register the runner manually through gitlab-runner register, after that I can see the runner instance in http://my-gitlab.com/admin/runners.

So am I do anything wrong? Or is it has to manually register the runner inside the pod?

Thanks.

-- leo
gitlab
gitlab-ce
gitlab-ci
gitlab-ci-runner
kubernetes

1 Answer

6/28/2019

Indeed you need to explicitly register runner on the GitLab server.
For example via:

gitlab-runner register --non-interactive \
    --name $RUNNER_NAME \
    --url $GITLAB_URL \
    --registration-token $GITLAB_REGISTRATION_TOKEN \
    --executor docker \
    --docker-image $DOCKER_IMAGE_BUILDER \
    --tag-list $GITLAB_RUNNER_TAG_LIST \
    --request-concurrency=$GITLAB_RUNNER_CONCURRENCY

You can pass most of its configuration as arguments.
If you didn't create config.toml, it will generate it for you, including the runner token received from the server on registration.

However,
as you use Kubernetes, there is a simpler way.
GitLab provides great integration with Kubernetes, all you need to do is attach your cluster once to your project\group: https://docs.gitlab.com/ee/user/project/clusters/#adding-an-existing-kubernetes-cluster

And then installing a runner is just few clicks in the UI, via what they call "managed apps": https://docs.gitlab.com/ee/user/clusters/applications.html#gitlab-runner

On this last page you can find links to the Helm chart that they use.
So you can even use it directly yourself.
And you can see there specifically call to register: configmap.yaml#L65

-- Ivan
Source: StackOverflow