Fabrice Jammes
Fabrice Jammes

Reputation: 3205

How to retrieve the api server URL with kubeadm?

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.

Upvotes: 0

Views: 837

Answers (3)

Aldemir
Aldemir

Reputation: 11

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://||"

Upvotes: 1

Fabrice Jammes
Fabrice Jammes

Reputation: 3205

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

Upvotes: 0

Ralle Mc Black
Ralle Mc Black

Reputation: 1203

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"
      ],
.....

Upvotes: 2

Related Questions