Search code examples
bashkuberneteskubectl

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


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 ?


Solution

  • 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