0

Is there a kubectl command which returns the API server URL?

What I want is the code I need to put instead of below ... :

API_SERVER_URL=$(kubectl ...)
echo $API_SERVER_URL
http://<API_SERVER_IP>:<API_SERVER_PORT>

API_SERVER_IP should be the very same that the one in my .kube/config.

0

3 Answers 3

2

Try this:

kubectl proxy --port=8090 &
curl http://localhost:8090/api/

This returns something like this:

{
  "kind": "APIVersions",
  "versions": [
    "v1"
  ],
  "serverAddressByClientCIDRs": [
    {
      "clientCIDR": "0.0.0.0/0",
      "serverAddress": "10.165.39.165:16443"
    }
  ]
}

Without proxying you can use:

https://10.165.39.165:16443/api

but you need to pass authorization in the request.

In the response you see the array with the versions.

From here you can call and inspect the versions or get what is available on that version.

curl http://localhost:8090/api/v1

{
  "kind": "APIResourceList",
  "groupVersion": "v1",
  "resources": [
    ....
      "shortNames": [
        "cs"
      ]
    },
    {
      "name": "configmaps",
      "singularName": "",
      "namespaced": true,
      "kind": "ConfigMap",
      "verbs": [
        "create",
        "delete",
        "deletecollection",
        "get",
        "list",
        "patch",
        "update",
        "watch"
      ],
      "shortNames": [
        "cm"
      ],
.....
1

This could be refined for programmatical access to the url is:

egrep  -B1 -e "name: .*CONTEXT_NAME" /PATH_TO/.kube/config | grep server | sed -e "s| \+server: https://||"
1
  • Thanks, here is an example for our command which works with kind: egrep -B1 -e "name: .*kind-kind" $HOME/.kube/config | grep server | sed -e "s| \+server: https://||" Commented Jul 17, 2024 at 14:59
0

This one works fine:

PROTOCOL=$(kubectl get endpoints -n default kubernetes -o yaml -o=jsonpath="{.subsets[0].ports[0].name}")
IP=$(kubectl get endpoints -n default kubernetes -o yaml -o=jsonpath="{.subsets[0].addresses[0].ip}")
PORT=$(kubectl get endpoints -n default kubernetes -o yaml -o=jsonpath="{.subsets[0].ports[0].port}")
API_SERVER_URL="${PROTOCOL}://${IP}:${PORT}"

and its result is something like that:

echo $API_SERVER_URL
https://172.18.0.3:6443

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.