Create a registry credential secret and reference in pod and deployment specs so you can pull container images without errors.

Docker Hub requires an account to pull images from. You can create a Kubernetes secret with your Docker Hub credentials and reference this secret in pods and deployment specs.

This approach can also be used for other private registries.

Prerequisites

Install Docker on an Ubuntu client machine. See https://docs.docker.com/engine/install/ubuntu/.

Procedure

  1. Run docker version and verify that Docker is installed.
  2. Run docker login -u USERNAME.
  3. Enter your Docker Hub password.
  4. Run cat ~/.docker/config.json.
  5. Run the following command to create a generic secret named regcred that includes your Docker Hub access credentials.
    kubectl create secret generic regcred \
        --from-file=.dockerconfigjson=/home/ubuntu/.docker/config.json \
        --type=kubernetes.io/dockerconfigjson
    
    You should see secret/regcred created.
  6. Verify the secret.
    kubectl get secrets
    
    NAME                  TYPE                                  DATA   AGE
    default-token-w7wqk   kubernetes.io/service-account-token   3      6h28m
    regcred               kubernetes.io/dockerconfigjson        1      3h22m
    
    Reference the regcred secret in the YAML.
  7. Reference the regcred secret in the imagePullSecrets section of pod or deployment spec for container images.
    For example:
    apiVersion: v1
    kind: Pod
    metadata:
      name: ping-pod
      namespace: default
    spec:
      containers:
      - image: busybox:1.34
        name: busybox
        command: ["ping", "-c"]
        args: ["1", "8.8.8.8"]
      imagePullSecrets:
      - name: regcred
      restartPolicy: Never