HighSkill

Pull Docker Images from GitLab to DigitalOcean Kubernetes

Pull Docker Images from GitLab to DigitalOcean Kubernetes

To pull images from the GitLab Container Registry into your DigitalOcean Kubernetes cluster, follow these steps:

1. Create a GitLab Personal Access Token

  1. Log in to GitLab: Go to your GitLab instance and log in with your credentials.
  2. Access your profile settings: Click on your profile picture in the top-right corner and select "Settings".
  3. Navigate to Access Tokens: In the left sidebar, find and click "Access Tokens".
  4. Generate a new token:
    • Name: Give your token a name.
    • Scopes: Select at least the read_registry scope.
    • Expiry date: Optionally, set an expiry date.
  5. Create token: Click the "Create personal access token" button.
  6. Copy the token: Copy the generated token and store it securely. You won’t be able to see it again.

2. Create a Kubernetes Secret for the Docker Registry

You need to create a Kubernetes secret to store your Docker registry credentials. Replace the placeholders with your actual values.

kubectl create secret docker-registry gitlab-registry \
  --docker-server=registry.gitlab.com \
  --docker-username=<your_gitlab_username> \
  --docker-password=<your_personal_access_token> \
  --docker-email=<your_email>

3. Use the Secret in Your Kubernetes Deployment

When creating your Kubernetes deployments, reference the secret you just created to authenticate with the GitLab Container Registry.

Here’s an example of a deployment YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: registry.gitlab.com/<your_gitlab_namespace>/<your_project>:<tag>
        ports:
        - containerPort: 80
      imagePullSecrets:
      - name: gitlab-registry

4. Apply the Deployment

Apply the deployment to your Kubernetes cluster:

kubectl apply -f deployment.yaml

Summary

  1. Generate a GitLab personal access token with read_registry scope.
  2. Create a Kubernetes secret to store your GitLab registry credentials.
  3. Reference the secret in your deployment YAML file.

This should enable your Kubernetes cluster to pull images from the GitLab Container Registry successfully.