9

I am trying to get the POST and GET requests working (via Postman) but this is what I'm getting,

GET Request

curl -X GET http://localhost:8080/api/v1/namespaces/default/pods/mypod/exec

GET Response

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "the server could not find the requested resource",
  "reason": "NotFound",
  "details": {

  },
  "code": 404
}

POST Request

curl -X POST 'http://localhost:8080/api/v1/namespaces/default/pods/mypod/exec?command=ls&container=my-container&stderr=true&stdout=true'

POST Response

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "Upgrade request required",
  "reason": "BadRequest",
  "code": 400
}

Any idea on how to get these requests working? What parameters need to be changed?

Thanks

Mujeeb
  • 995
  • 1
  • 8
  • 18

2 Answers2

11

I think you are trying to exec into a pod. A websocket connection is required if you want to exec into a pod.

For a websocket connection, an http(s) call is made first, followed by an upgrade request to websocket using the HTTP Upgrade header.

curl does not support upgrade from http to websocket. Hence the error.

kubectl exec would be handy for this use case.

You can also try other cli tools which support websockets, like

Nizar M
  • 361
  • 2
  • 5
  • 2
    Yes, it was not possible via curl, so we used _kubectl_ instead. – Mujeeb Mar 21 '18 at 10:37
  • This person claims they were able to get it working with curl: https://github.com/kubernetes-client/python/issues/409#issuecomment-1241425302 – Venryx Jan 02 '23 at 14:34
  • Oh, found a second person that claims to have gotten `curl` to work with `exec`: https://stackoverflow.com/a/37396806 – Venryx Jan 02 '23 at 14:36
  • For reference, I eventually found out how to implement the https->websocket upgrade in Rust code: https://github.com/debate-map/app/blob/c2f62f7aabc603abf3f4c23c8357f1323537137d/Packages/rust-shared/src/utils/k8s/k8s_client.rs#L14 – Venryx May 27 '23 at 06:40
2
wscat -n --header "Authorization: Bearer $TOKEN"   -c 'https://200.200.200.160:6443/api/v1/namespaces/default/pods/mysql/exec?command=bash&container=mysql&stdin=true&stdout=true&tty=true'

wscat: https://github.com/websockets/wscat

enter image description here

jamlee
  • 1,234
  • 1
  • 13
  • 26