From Kubernetes to Gitlab-ci in less than 10min

From Kubernetes to Gitlab-ci in less than 10min

I have been playing a lot with Kubernetes lately, from creating a cluster from scratch on bare metal, setting up a ELK stack, Prometheus metrics to setting up a CI/CD using Gitlab CI.

I found the last one so easy that I made a video for it.

I have been using a DigitalOcean Kubernetes cluster for this video, it was super easy and I got $100 to play with it for 60 days.

If you have never tried it, I encourage you to try it out. Here is a link you can use to get the $100 credits: DigitalOcean

The video shows how I create a cluster, setup a service account on it for Gitlab ci, then setup a gitlab runner on the cluster.

The easiest way to do it is to use the button in Gitlab UI to create a runner on your cluster, or to use Helm from the console, but in my case I wanted to customize a bit more the runner to be able to use Docker DinD to build Docker images from Gitlab ci.


Using Helm from the console

You can take a look at the doc here: https://docs.gitlab.com/runner/install/kubernetes.html#installing-gitlab-runner-using-the-helm-chart

Here are the values I have been using when I tried with Helm

You can then install the runner using this command:

# Add the repo
helm repo add gitlab https://charts.gitlab.io

# Install
helm install --namespace gitlab-managed-apps gitlab-runner gitlab/gitlab-runner -f values.yaml

The issue with this is that for now, gitlab chart does not use the volumes when creating it's config file (`config.toml`). So one way to fix it if you still want to use Helm is to change the ConfigMap manually in Kubernetes. You'll find a lot of issues here that are talking about it.

To fix that you can run kubectl edit -n gitlab-managed-apps ConfigMap gitlab-runner-name then add this before # Start the runner:

    cat >> /home/gitlab-runner/.gitlab-runner/config.toml <<-EOF
          [[runners.kubernetes.volumes.host_path]]
            name = "docker"
            mount_path = "/var/run/docker.sock"
            read_only = true
            host_path = "/var/run/docker.sock"
          [[runners.kubernetes.volumes.host_path]]
            name = "certs"
            mount_path = "/certs/client"
            read_only = true
            host_path = "/certs/client"
          [[runners.kubernetes.volumes.host_path]]
            name = "cache"
            mount_path = "/cache"
            read_only = true
            host_path = "/cache"
    EOF

Using Kubectl

The way I did it was to extract the templates from the chart and modify them to add my changes in the ConfigMap

To extract the yaml files you can use helm template gitlab/gitlab-runner --output-dir '.' -f values.yaml

You will get a bunch of files depending on your values.

Then edit the configmap.yaml to add the previous block and get something like this:

Once it's done you can run kubectl apply -f <file> for your files

kubectl apply -f service-account.yaml
kubectl apply -f secrets.yaml
kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml