kubernetes: kubectl exec does not accept pod/foobar syntax
I feel like I should be able to do:
$ P=$(kubectl get pods -l run=hairpin-test -o name)
$ kubectl exec --tty -i $P sh
but I get error: invalid resource name "pod/hairpin-test-1119317711-4qmtf": name may not contain "/"
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 16 (7 by maintainers)
Commits related to this issue
- Merge pull request #33176 from gluke77/allow-pod-prefix-for-kubectl-exec Automatic merge from submit-queue Allow 'pod/' prefix in pod name for 'kubectl exec' This PR adds ability to provide pod nam... — committed to kubernetes/kubernetes by deleted user 8 years ago
- Merge pull request #24225 from openshift-cherrypick-robot/cherry-pick-24202-to-release-4.3 Bug 1776698: Restore UPSTREAMs dropped in #24159 Origin-commit: ad22cdd903e285cd2264048002fb463596cf7362 — committed to openshift/kubernetes by k8s-publishing-bot 5 years ago
Has this been reverted again? I cannot use “pod/podname” for
kubectl exec
v1.11:^ Seconding the above. I’m getting the same ‘may not contain /’ error. Naturally it’s trivial to use unix commands to filter this, but this is such a basic feature and matches the way the rest of kubectl works, so it’s quite a shocking omission.
Had the same problem. Updating kubectl resolved the issue for me.
Just ran into this on a brand new default installation.
Local Mac Docker for Desktop Enabled k8 in preferences
error: invalid resource name “svc/broken-gorilla-postgresql”: [may not contain ‘/’]
@laurieboyes If you use selectors, you can do something like this:
kubectl exec -it "$(kubectl get po -l app=$YOUR_APP_NAME -o name | cut -d '/' -f 2)" -c $YOUR_CONTAINER_NAME -- /bin/sh
Otherwise you’ll need to manually grep, something like this:
kubectl exec -it "$(kubectl get po -o name | grep $YOUR_REGEX | cut -d '/' -f 2)" -c $YOUR_CONTAINER_NAME -- /bin/sh
$()
is the syntax in bash to run the command and use its output as part of another command. You can also use backticks to achieve the same effect but that’s considered bad form by many (myself included). For example:example_ls_result=$(ls)
If going the grep route, take care to make sure your regex will only return one result, otherwise you’ll have to pipe it to xargs or something similar.