Kubectl proxy and curl

Author: Fabrice JAMMES (LinkedIn).

Objective

Learn how to use kubectl proxy and curl to list services in a specified Kubernetes namespace.

Prerequisites

  • A running Kubernetes cluster
  • kubectl installed and configured
  • curl installed

Steps

Step 1: Start kubectl proxy

Run the following command to start the kubectl proxy:

kubectl proxy &
Note

In case of port conflict use the --portoption

This command runs kubectl proxy in the background, allowing API requests to be sent to the Kubernetes API server via localhost.

Step 2: List Services in a Namespace using curl

To list the services in a given namespace, use the following curl command:

Answer
curl http://localhost:8001/api/v1/namespaces/<namespace>/services

Replace <namespace> with the actual namespace you want to query. If you want to list services in the default namespace, use:

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

Expected Output

You should see a JSON output listing the services running in the specified namespace, including their metadata, specifications, and statuses.

Cleanup

If you want to stop the kubectl proxy, find the process ID and terminate it:

ps aux | grep kubectl
kill <PID>

Alternatively, you can use:

pkill -f kubectl

Conclusion

You have successfully queried Kubernetes services using kubectl proxy and curl. This method allows direct interaction with the Kubernetes API for retrieving resource information.