<p>I am trying to resolve this error from Kubernetes (here is the 'describe pod' output, redacted some parts to keep sensitive data out of this post):</p>
<pre><code> ~ $ kubectl describe pod service-xyz-68c5f4f99-mn7jl -n development
Name:         service-xyz-68c5f4f99-mn7jl
Namespace:    development
Priority:     0
Node:         autogw.snancakp/&lt;REDACTED&gt;
Start Time:   Thu, 24 Aug 2023 09:55:21 -0400
Labels:       app=service-xyz
              pod-template-hash=68c5f4f99
Annotations:  cni.projectcalico.org/containerID: 7c93d46d14e9101887d58a7b4627fd1679b8435a439bbe46a96ec11b36d44981
              cni.projectcalico.org/podIP: &lt;REDACTED&gt;/32
              cni.projectcalico.org/podIPs: &lt;REDACTED&gt;/32
Status:       Running
IP:           &lt;REDACTED&gt;
IPs:
  IP:           &lt;REDACTED&gt;
Controlled By:  ReplicaSet/service-xyz-68c5f4f99
Containers:
  service-xyz:
    Container ID:   containerd://&lt;REDACTED&gt;
    Image:          gitlab.personal.local:5050/teamproject/service-xyz:latest
    Image ID:       gitlab.personal.local:5050/teamproject/service-xyz@sha256:&lt;REDACTED&gt;
    Port:           &lt;none&gt;
    Host Port:      &lt;none&gt;
    State:          Waiting
      Reason:       ImagePullBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Thu, 24 Aug 2023 09:55:27 -0400
      Finished:     Thu, 24 Aug 2023 09:55:27 -0400
    Ready:          False
    Restart Count:  0
    Environment:
      QUEUE:     service-xyz
      EXCHANGE:  &lt;set to the key 'environment' in secret 'teamproject-secrets'&gt;  Optional: false
      RMQ_URL:   &lt;set to the key 'rmq_url' in secret 'teamproject-secrets'&gt;      Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-rtm67 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-rtm67:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       &lt;nil&gt;
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              &lt;none&gt;
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                    From               Message
  ----     ------     ----                   ----               -------
  Normal   Scheduled  7m                     default-scheduler  Successfully assigned development/service-xyz-68c5f4f99-mn7jl to machine.goodmachine
  Normal   Pulled     6m55s                  kubelet            Successfully pulled image &quot;gitlab.personal.local:5050/teamproject/service-xyz:latest&quot; in 5.137384512s (5.137391155s including waiting)
  Normal   Created    6m54s                  kubelet            Created container service-xyz
  Normal   Started    6m54s                  kubelet            Started container service-xyz
  Normal   Pulling    6m11s (x4 over 7m)     kubelet            Pulling image &quot;gitlab.personal.local:5050/teamproject/service-xyz:latest&quot;
  Warning  Failed     6m11s (x3 over 6m52s)  kubelet            Failed to pull image &quot;gitlab.personal.local:5050/teamproject/service-xyz:latest&quot;: rpc error: code = Unknown desc = failed to pull and unpack image &quot;gitlab.personal.local:5050/teamproject/service-xyz:latest&quot;: failed to resolve reference &quot;gitlab.personal.local:5050/teamproject/service-xyz:latest&quot;: failed to authorize: failed to fetch oauth token: unexpected status: 401 Unauthorized
  Warning  Failed     6m11s (x3 over 6m52s)  kubelet            Error: ErrImagePull
  Normal   BackOff    5m34s (x2 over 6m26s)  kubelet            Back-off pulling image &quot;gitlab.personal.local:5050/teamproject/service-xyz:latest&quot;
  Warning  Failed     5m34s (x2 over 6m26s)  kubelet            Error: ImagePullBackOff
  Warning  BackOff    119s (x18 over 6m52s)  kubelet            Back-off restarting failed container service-xyz in pod service-xyz-68c5f4f99-mn7jl_development(561cbfc0-addd-4da3-ae6b-dccc2dfa68eb)
</code></pre>
<p>So the error is an ImagePullBackOff, so I figured I didn't set up my gitlab-ci, secrets, or pod/deployment yaml correct.</p>
<p>Here is <strong><code>gitlab-ci.yml</code></strong>:</p>
<pre class="lang-yaml prettyprint-override"><code>stages:
  - build
  - deploy

variables:
  SERVICE_NAME: &quot;service-xyz&quot;

services:
   - name: docker:dind
     alias: dockerservice
build_image:
  tags:
    - self.hosted
    - linux
  stage: build
  image: docker:latest
  variables:
    DOCKER_HOST: tcp://dockerservice:2375/
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: &quot;&quot;
  before_script:
    - docker login -u &quot;$CI_REGISTRY_USER&quot; -p &quot;$CI_REGISTRY_PASSWORD&quot; &quot;$CI_REGISTRY&quot;
  script:
    - docker build -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE
  only:
    - main

deploy:
  tags:
    - self.hosted
    - linux
  stage: deploy
  image:
    name: bitnami/kubectl:latest
    entrypoint: [&quot;&quot;]
  variables:
    NAMESPACE: development
    SECRET_NAME: regcred
  before_script:
    - export KUBECONFIG=$KUBECONFIG_DEVELOPMENT
  script:
    - kubectl delete secret -n ${NAMESPACE} ${SECRET_NAME} --ignore-not-found
    - kubectl create secret -n ${NAMESPACE} docker-registry ${SECRET_NAME} --docker-server=${CI_REGISTRY} --docker-username=${CI_REGISTRY_USER} --docker-password=${CI_REGISTRY_PASSWORD} --docker-email=${GITLAB_USER_EMAIL} 
    - kubectl patch serviceaccount default -p '{&quot;imagePullSecrets&quot;:[{&quot;name&quot;:&quot;'$SECRET_NAME'&quot;}]}' -n $NAMESPACE
    - kubectl create namespace $NAMESPACE --dry-run=client -o yaml | kubectl apply -f -
    - kubectl apply -f deployment.yaml -n $NAMESPACE
  only:
    - main
  when: manual
</code></pre>
<p>Here is the <strong>deployment</strong> file (deployment.yaml) (which references <code>regcred</code>):</p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-xyz
spec:
  replicas: 1
  selector:
    matchLabels:
      app: service-xyz
  template:
    metadata:
      labels:
        app: service-xyz
    spec:
      containers:
      - name: service-xyz
        image: gitlab.personal.local:5050/teamproject/service-xyz:latest
        imagePullPolicy: Always
        env:
        - name: QUEUE
          value: service-xyz
        - name: EXCHANGE
          valueFrom:
            secretKeyRef:
              name: teamproject-secrets
              key: environment
        - name: RMQ_URL
          valueFrom:
            secretKeyRef:
              name: teamproject-secrets
              key: rmq_url
      imagePullSecrets:
      - name: regcred
</code></pre>
<p>When I look at the secrets in Kuberenetes (through Rancher), I see the <code>regcred</code> in the correct place:
<a href="https://i.stack.imgur.com/sPsgT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sPsgT.png" alt="RancherSecretsPage" /></a></p>
<p>I believe I've set up everything correctly, and I don't know why the deployment won't work. The deployment correctly references <code>regcred</code>, but I'm still getting the ImagePullBackOff error.</p>
<p>Can anyone help me out here?</p>
<p>Regards and thanks</p>

Nulla
<p>As per Gitlab documentation</p>
<p><strong>CI_REGISTRY_PASSWORD</strong></p>
<p>The password to push containers to the project’s GitLab Container Registry. Only available if the Container Registry is enabled for the project. This password value is the same as the CI_JOB_TOKEN and <strong>is valid only as long as the job is running</strong>.</p>
<p><strong>Use the CI_DEPLOY_PASSWORD for long-lived access to the registry.</strong></p>
<p>So based on this it looks like your secret would no longer be valid once your job finishes(which it does immediately after trying to creating your kubernetes deployment object) and in the meantime your Pod is trying to pull in the image from registry using the invalid token/password(CI_REGISTRY_PASSWORD)</p>
<p>Try creating and using a deploy token instead</p>
<p><a href="https://docs.gitlab.com/ee/user/project/deploy_tokens/index.html#gitlab-deploy-token" rel="nofollow noreferrer">https://docs.gitlab.com/ee/user/project/deploy_tokens/index.html#gitlab-deploy-token</a></p>
<p><a href="https://i.stack.imgur.com/csrry.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/csrry.png" alt="enter image description here" /></a></p>
