Usually when you need kubectl run
it's because you're testing something temporary, in a namespace that already has the docker registry secret to access the private registry. So the simplest is to edit the default service account to give it the pull secret to use when a pull secret is not present (which will be the case for kubectl run
):
kubectl edit serviceaccount default
The edit will show something similar to this:
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: "2019-04-16T14:48:17Z"
name: default
namespace: integration-testing
resourceVersion: "60516585"
selfLink: /api/v1/namespaces/integration-testing/serviceaccounts/default
uid: ab7b767d-6056-11e9-bba8-0ecf3bdac4a0
secrets:
- name: default-token-4nnk4
Just append an imagePullSecrets
:
imagePullSecrets:
- name: <name-of-your-docker-registry-password-secret>
so it will look like this:
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: "2019-04-16T14:48:17Z"
name: default
namespace: integration-testing
resourceVersion: "60516585"
selfLink: /api/v1/namespaces/integration-testing/serviceaccounts/default
uid: ab7b767d-6056-11e9-bba8-0ecf3bdac4a0
secrets:
- name: default-token-4nnk4
imagePullSecrets:
- name: <name-of-your-docker-registry-password-secret>
Say name is YOUR_PWD_SECRET
, then this secret must exist in the kubectl context's namespace:
tooluser:/host $ kubectl get secret YOUR_PWD_SECRET
NAME TYPE DATA AGE
YOUR_PWD_SECRET kubernetes.io/dockerconfigjson 1 186d
If it doesn't exist you must create it, either from scratch or copy it from another namespace (best way to do that is answer by NicoKowe at https://stackoverflow.com/a/58235551/869951).
With a secret holding your docker registry password, the secret in the same namespace where the kubectl run
will execute, and with a default service account that lists the secret as imagePullSecrets, the kubectl run
will work.