While using Kubernetes API
, I came across this issue where I am not able to pod exec
into another pod using the API.
I made sure I am running a pod
with a serviceAccount
that has correct RBAC
permissions (and both pods
are in the same namespace). I tried with curl
(7.74.0
) but I stumbled upon this SO answer that curl
may not be the right utility to do this. I have already tried other possible options (this and this) mentioned in the comments in the same post, but no luck.
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)
curl -k -H "Upgrade: SPDY/3.1" -H "Authorization: Bearer $TOKEN" -H "Connection: Upgrade" -H "Sec-WebSocket-Version: 13" -H "Accept: application/json" "https://kuberne
tes.default.svc/api/v1/namespaces/$NAMESPACE/pods/nginx-deployment-5c547569b4-jgm85/exec?command=ls"
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": "Upgrade request required",
"reason": "BadRequest",
"code": 400
}
Have already tried adding/removing few of those headers.
The next thing I tried was wscat
(5.2.0
). Tried a couple of variations, but needless to say, "no luck" again.
wscat -n -H "Authorization: Bearer $TOKEN" -c wss://kubernetes.default.svc/api/v1/namespaces/$NAMESPACE/pods/nginx-deployment-5c547569b4-jgm85/exec?command=ls
error: Unexpected server response: 403
This command outputs nothing (at all):
wscat -c wss://kubernetes.default.svc/api/v1/namespaces/$NAMESPACE/pods/nginx-deployment-5c547569b4-jgm85/exec?command=ls&stdin=true&stdout=true&tty=true&token=$TOKEN&namespace=$NAMESPACE
I looked at this SO post, but it doesn't work for me.
wscat -n -H "Authorization: Bearer $TOKEN" -c 'https://kubernetes.default.svc/api/v1/namespaces/$NAMESPACE/pods/nginx-deployment-5c547569b4-jgm85/exec?command=ls&stdin=true&stdout=true&tty=true'
...
SyntaxError: The URL's protocol must be one of "ws:", "wss:", or "ws+unix:"
...
Not sure how the fella even managed to use http(s)
in the first place.
Can someone kindly advise if there's something wrong with what I doing or an alternative (can't use kubectl
) to do the same? TIA