Running “kubectl proxy” for a single request

2/26/2019

Is there any way to run kubectl proxy, giving it a command as input, and shutting it down when the response is received?

I'm imagining something with the -u (unix socket) flag, like this:

kubectl proxy -u - < $(echo "GET /api/v1/namespaces/default")

I don't think it's possible, but maybe my socket fu just isn't strong enough.

-- philo
kubectl
kubernetes

2 Answers

2/28/2019

kubectl proxy won't give you any way to run a one-off request and terminate the proxy.

Generic way to start a command in the background, run a command and terminate the initially started command finally would be to write a bash script like:

#!/usr/bin/env bash
set -eu

kubectl proxy &
proxy_pid=$!
echo $proxy_pid

until curl -fsSL http://localhost:8001/ > /dev/null; do
    echo "waiting for kubectl proxy" >&2
    sleep 5
    # TODO add max retries so you can break out of this
done

curl http://localhost:8001/api/v1/namespaces/default

function cleanup {
    echo "killing kubectl proxy" >&2
    kill $proxy_pid
}
trap cleanup EXIT

If you actually want to use sockets:

  • Start the unix domain socket like kubectl proxy -u ./foo.sock
  • Make sure your cURL supports unix domain sockets and call curl --unix-socket ./foo.sock http:/api/v1/namespaces/default etc.
-- AhmetB - Google
Source: StackOverflow

2/27/2019

You don't need a long-running kubectl proxy for this.

Try this:

kubectl get --raw=/api/v1/namespaces/default
-- AhmetB - Google
Source: StackOverflow