Error from server (NotFound): pods "\nmongo-client-79667cc85d-tsg72" not found

9/24/2019

I'm trying to make a backup from Mongo / K8S with this script

export POD=$(kubectl get pods -l app=mongo-client -o custom-columns=:metadata.name -n espace-client)
kubectl exec "$POD" sh -c 'mongodump --archive' > ~/backup/mongo/$(date +%F).db.dump

I get this error:

Error from server (NotFound): pods "\nmongo-client-79667cc85d-tsg72" not found

When I check the pods, I can see mongo-client-79667cc85d-tsg72

When I put the name without variable, it works well, so it might be because of initial \n. How can I avoid it ? How can I remove it from name ?

-- Juliatzin
bash
kubectl
kubernetes

2 Answers

9/24/2019

The output of your kubectl get pods command has a newline before the pod name because the first line of the output is the column header (which is empty in your case).

To prevent this and get only the name as output, you can suppress the column headers with the --no-headers flag:

kubectl get pods -l app=mongo-client -o custom-columns=:metadata.name -n espace-client --no-headers
-- weibeld
Source: StackOverflow

9/24/2019

Your kubectl get pods command is constrained with a namespace selector -n espace-client. Your kubectl exec command also needs the namespace flag.

-- erk
Source: StackOverflow