Kubernetes pod exec API - Upgrade request required
Asked Answered
H

3

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

Hamer answered 13/3, 2018 at 7:20 Comment(0)
P
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

Plicate answered 13/3, 2018 at 11:9 Comment(4)
Yes, it was not possible via curl, so we used kubectl instead.Hamer
This person claims they were able to get it working with curl: github.com/kubernetes-client/python/issues/…Horning
Oh, found a second person that claims to have gotten curl to work with exec: stackoverflow.com/a/37396806Horning
For reference, I eventually found out how to implement the https->websocket upgrade in Rust code: github.com/debate-map/app/blob/…Horning
F
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

Facia answered 12/10, 2020 at 11:11 Comment(0)
B
0

below code will work

from kubernetes import client, config, utils
from kubernetes.stream import stream


def execute_command_in_pod(namespace, pod_name, command):
# Load Kubernetes configuration from default location
config.load_kube_config()

# Create an instance of the Kubernetes API client
api_instance = client.CoreV1Api()

# Specify the container name if there are multiple containers in the pod
container_name = None

# Construct the command payload
exec_command = [
    "/bin/sh",
    "-c",
    command
]

# Execute the command in the pod
try:
    response = stream(api_instance.connect_get_namespaced_pod_exec,
        pod_name,
        namespace,
        command=exec_command,
        stderr=True,
        stdin=False,
        stdout=True,
        tty=False
    )
    print("Command executed successfully. Output:")
    print(response.output)
    return True
except Exception as e:
    print(f"Error executing command: {e}")
    return False

it from kubernetes python git

Bequeath answered 14/2 at 6:3 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Unearth

© 2022 - 2024 — McMap. All rights reserved.