Using GitLab registry with a Kubernetes Cluster

This post gives information on how to pull private Docker images stored in the GitLab container registry into a Kubernetes deployment

Assumptions

  • kubectl is already configured to connect to the correct cluster

1. Create GitLab credentials

To pull private images from the GitLab registry you will need to create an Access Token

  1. Visit https://gitlab.com/profile/personal_access_tokens
  2. Enter a token name, and optional expiry date
  3. Ensure read_registry is selected
  4. Click “Create personal access token”
  5. Make a note of the token, it will not be shown again

2. Create a Secret

Create a secret using data from, replacing the placeholders with your GitLab username, GitLab access token and email address

kubectl create secret docker-registry gitlab-auth \
    --docker-server=https://registry.gitlab.com \
    --docker-username=USERNAME \
    --docker-password=TOKEN \
    --docker-email=EMAIL

Note: By default, Kubernetes secret data is not secure

3. Update your deployment

In your Deployment definition

spec:
    template:
        spec:
            containers:
                - image: registry.gitlab.com/user-name/image-name:tag
            imagePullSecrets:
                - name: gitlab-auth