I am trying to use kubectl run command to pull an image from private registry and run a command from that. But I don't see an option to specify image pull secret. It looks like it is not possible to pass image secret as part for run command.
Is there any alternate option to pull a container and run a command using kubectl? The command output should be seen on the console. Also once the command finishes the pod should die.
As far as I know you cannot, but you can use
kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
, but this is not very different from what you can do withkubectl create -f mypod.json
What I think you're after is not a
Pod
but aJob
, for example, if you need to populate a database, you can create a container that does that, and run it as a job instead of a pod or replica set.Kubectl run ...
creates deploymentor
job` objects. Jobs finish when the pod execution terminates and you can check the logs.Take a look here and here for the termination
You could create the
docker-registry
secret as described at @MarkO'Connor's link, then add it to the default ServiceAccount. It's the SA that acts on the behalf of pods, including pulling their images.From Adding ImagePullSecrets to a service account:
Now, any new pods created in the current namespace will have this added to their spec:
On Windows, you can do
patch
, but as it shows a JSON error, you have to do this trick (using PowerShell):Also , if you want to update/ append imagePullSecret , then you should be using something like this :
.
Usually when you need kubectl 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
):The edit will show something similar to this:
Just append an
imagePullSecrets
:so it will look like this:
Say name is
YOUR_PWD_SECRET
, then this secret must exist in the kubectl context's namespace: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, thekubectl run
will work.You can use the overrides if you specify it right, it's an array in the end, that took me a bit to figure out, the below works on Kubernetes of at least 1.6:
--overrides='{ "apiVersion": "v1", "spec": { "imagePullSecrets": [{"name": "your-secret"}] } }'
for example
kubectl run -i -t hello-world --restart=Never --rm=true \ --image=eu.gcr.io/your-registry/hello-world \ --overrides='{ "apiVersion": "v1", "spec": { "imagePullSecrets": [{"name": "your-registry-secret"}] } }'