Helmを使ってGitLab RunnerをKubernetesに配置する

目次

概要

Kubernetes上に、GitLab Runnerを配置することがあったので、その時の方法を書いておきます。

環境

  • Kubernetes 1.9
  • GitLab Runner 10.6

方法

Kubernetes上で、GitLab Runnerを動かす方法はいくつかあります。

今回は、Helmを使った方法を紹介します。

Helmを使うことで、Kubernetesにコンテナを楽にデプロイできるようになるので、Helmを使いました。

Helmのインストール

まず、Helmのインストールをします。

Helmのインストールは、kubectlコマンドなどが動作するクライアントと、Kubernetesの2箇所にインストールをする必要があります。

私の環境では、macOS上でkubectlコマンドを使っていたので、macOSでソフトウェアを管理するHomebrewコマンドを使ってクライアントにHelmをインストールしました。

brew install kubernetes-helm

もし、WindowsやLinuxなどの場合は、下記の情報を元にバイナリをインストールしましょう。

kubernetes/helm#install

クライアントにHelmをインストールしたらクライアントからKubernetesにHelmのインストールをします。

helmコマンドが使えるようになっているはずなので、次のコマンドを実行します。

helm init

これで、Helmのインストールは終わりです。

GitLab Runnerのインストール

では、GitLab Runnerのインストールをします。

GitLab Runnerをインストールする前に設定ファイルを用意しましょう。

## GitLab Runner Image
## ref: https://hub.docker.com/r/gitlab/gitlab-runner/tags/
##
image: gitlab/gitlab-runner:alpine-v10.6.0

## The GitLab Server URL (with protocol) that want to register the runner against
## ref: https://docs.gitlab.com/runner/commands/README.html#gitlab-runner-register
##
gitlabUrl: https://gitlab.example.com/

## The Registration Token for adding new Runners to the GitLab Server. This must
## be retreived from your GitLab Instance.
## ref: https://docs.gitlab.com/ce/ci/runners/README.html#creating-and-registering-a-runner
##
runnerRegistrationToken: "*****"

## Set the certsSecretName in order to pass custom certficates for GitLab Runner to use
## Provide resource name for a Kubernetes Secret Object in the same namespace,
## this is used to populate the /etc/gitlab-runner/certs directory
## ref: https://docs.gitlab.com/runner/configuration/tls-self-signed.html#supported-options-for-self-signed-certificates
##
#certsSecretName:

## Configure the maximum number of concurrent jobs
## ref: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section
##
concurrent: 6

## Defines in seconds how often to check GitLab for a new builds
## ref: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section
##
checkInterval: 30

## Configuration for the Pods that that the runner launches for each new job
##
runners:
  ## Default container image to use for builds when none is specified
  ##
  image: ubuntu:16.04

  ## Run all containers with the privileged flag enabled
  ## This will allow the docker:dind image to run if you need to run Docker
  ## commands. Please read the docs before turning this on:
  ## ref: https://docs.gitlab.com/runner/executors/kubernetes.html#using-docker-dind
  ##
  privileged: true

  ## Namespace to run Kubernetes jobs in (defaults to 'default')
  ##
  # namespace:

  ## Build Container specific configuration
  ##
  builds:
    # cpuLimit: 200m
    # memoryLimit: 256Mi
    cpuRequests: 100m
    memoryRequests: 128Mi

  ## Service Container specific configuration
  ##
  services:
    # cpuLimit: 200m
    # memoryLimit: 256Mi
    cpuRequests: 100m
    memoryRequests: 128Mi

  ## Helper Container specific configuration
  ##
  helpers:
    # cpuLimit: 200m
    # memoryLimit: 256Mi
    cpuRequests: 100m
    memoryRequests: 128Mi

上記の設定ファイルを参考に、適当に書き換えてください。

必ず書き換えるところとしては、gitlabUrlrunnerRegistrationTokenのふたつです。

それぞれ、GitLabのURLとランナーの登録の際に必要なトークンを指定します。

仮に、この設定ファイルをvalues.ymlという名前で保存したとします。

設定ファイルを用意したら、次のコマンドでHelmを使ってKubernetesにGitLab Runnerを配置します。

helm repo add gitlab https://charts.gitlab.io
helm install --name gitlab-runner -f values.yml gitlab/gitlab-runner

helm repo add gitlab https://charts.gitlab.ioは、GitLab Runnerのチャートの登録するためのものです。HelmにコンテナをデプロイするためのDeploymentなどの設定ファイルが書かれたチャートファイルを登録します。一回だけ実行すればいいのであとは実行する必要はありません。

次のhelm installでGitLab Runnerを配置します。-fオプションで、用意しておいた設定ファイルを指定します。--nameは、適当名前をつけることができ、複数のGitLab Runnerを配置する場合に重ならない名前を指定する必要があります。

これで、GitLab Runnerの配置ができました。

GitLab Runnerの更新

values.ymlを変更して、GitLab Runnerの設定を更新したいとなった場合は、次のコマンドを実行します。

helm upgrade -f values.yml gitlab-runner gitlab/gitlab-runner

gitlab-runnerの部分は、helm installの時に--nameで指定したものを指定します。

GitLab Runnerの削除

Helmで配置したGitLab Runnerを削除したい場合は、次のコマンドを実行します。

helm delete gitlab-runner

gitlab-runnerの部分は、helm installの時に--nameで指定したものを指定します。

さいごに

誰かの参考になれば幸いです。