Connect kubernetes to GitLab Container Registry
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To pull container images from GitLab's Container Registry in Kubernetes, you need to create a Kubernetes Secret containing your GitLab credentials and reference it as an imagePullSecrets in your pod or deployment spec. The secret stores the registry URL (registry.gitlab.com), username, and either a personal access token or a deploy token as the password. Without this secret, Kubernetes cannot authenticate with the private registry and pod creation fails with ErrImagePull or ImagePullBackOff.
Step 1: Create a GitLab Access Token
Deploy tokens are preferred over personal access tokens because they are scoped to a project or group and can be revoked without affecting the user's account.
Step 2: Create Kubernetes Secret
The secret type docker-registry stores credentials in the format Docker expects for registry authentication.
Step 3: Reference in Pod/Deployment
The imagePullSecrets field tells Kubernetes which secret to use when pulling the image. Without it, the kubelet attempts anonymous access and fails for private registries.
Creating the Secret from YAML
Setting Default imagePullSecrets per Namespace
Patching the default service account eliminates the need to add imagePullSecrets to every pod spec in the namespace.
GitLab CI/CD Integration
GitLab CI provides built-in variables ($CI_REGISTRY, $CI_REGISTRY_USER, $CI_REGISTRY_PASSWORD) for registry authentication. The Kubernetes secret must already exist in the cluster for the deployed pods to pull the image.
Troubleshooting
Multiple Registries
Multiple imagePullSecrets can be specified. Kubernetes tries each secret against the corresponding registry URL.
Common Pitfalls
- Token without
read_registryscope: Deploy tokens or personal access tokens must have theread_registryscope. A token with onlyapiorread_repositoryscope cannot pull images. Check the token permissions in GitLab's UI. - Secret in wrong namespace: Kubernetes secrets are namespace-scoped. If the deployment is in namespace
productionbut the secret is indefault, the pod cannot find the secret. Create the secret in the same namespace as the deployment. - Expired deploy token: Deploy tokens can expire. When they do, pods start failing with
ImagePullBackOff. Set up monitoring for token expiration and rotate tokens before they expire. Consider using tokens without expiration for long-running clusters. - Forgetting
imagePullSecretsin the pod spec: Creating the secret is not enough — the pod spec must reference it viaimagePullSecrets. Alternatively, patch the default service account to include the secret automatically. - Using the wrong registry URL for self-hosted GitLab: The default registry URL is
registry.gitlab.comfor GitLab.com. Self-hosted GitLab instances use a custom URL (oftenregistry.your-domain.com). The--docker-servervalue must match the registry URL in the image reference.
Summary
- Create a
docker-registrysecret withkubectl create secret docker-registryusing GitLab credentials - Reference the secret in
imagePullSecretsin your deployment spec - Use deploy tokens with
read_registryscope (preferred over personal access tokens) - Patch the default service account to avoid adding
imagePullSecretsto every pod spec - The secret must be in the same namespace as the deployment
- Use
kubectl describe podto diagnoseErrImagePullandImagePullBackOfferrors
Related reading
- Connect to Kubernetes mongo db in different namespace
- Connecting to many kubernetes services from local machine
- connection refused error in main application until istio-sidecar starts
- Container is not running
- Connect to Docker MySQL container from localhost?
- Connect to Docker MySQL container from localhost?
- Connect to mysql in a docker container from the host
- Connect to SQL Server database from a docker container

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.