2

I'm looking to get the information available in the command kubectl cluster-info

Kubernetes control plane is running at https://api.joseph-rancher-cluster-2.rancher.aveshalabs.io
CoreDNS is running at https://api.joseph-rancher-cluster-2.rancher.aveshalabs.io/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

How can I get the api server endpoint URL using that function?

I only know of two ways to get the end point, but both of them only return the private ip:

  1. kubectl describe svc kubernetes -n default
  2. kubectl config view -o jsonpath="{.clusters[?(@.name==\"joseph-rancher-cluster-2\")].cluster.server}"
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • 2
    You shouldn't usually need `lookup`. You also shouldn't normally need to look up the Kubernetes API server location; inside the cluster it's at a well-known location (you show a service with a DNS name `kubernetes.default`), and from outside the cluster you need to know the API server location before you can connect to the cluster at all (it needs to be passed out-of-band somehow). – David Maze Jan 02 '23 at 12:52
  • i agree. The requirement is actually to spin up a custom worker operator which can post it's own api server URL to another custom controller operator, running on a different cluster. So that it can access the worker metrics. Currently we are providing the api server URL manually via `values.yaml`. We are searching for a way to fetch endpoint while building the worker operator template. – Mridul Gain Jan 04 '23 at 09:21
  • 1
    Unless you have some sort of complex networking setup, the ClusterIP Service address from one cluster won't be reachable in the other. In general, you don't usually have ways in Kubernetes to find externally-accessible names of things from inside the cluster. I don't think you can do better than your current `values.yaml` approach. – David Maze Jan 04 '23 at 11:36
  • Would `kubectl config view -o jsonpath='{.contexts[?(@.name == "'"$(kubectl config current-context)"'")].context.cluster}'` work? Using the current context. ` – VonC May 05 '23 at 18:13
  • Since Helm does not have a built-in command to directly retrieve the API server endpoint URL, you might have to use (assuming `helm list --kubeconfig=/path/to/my-kubeconfig.yaml`) a `kubectl config view --kubeconfig=/path/to/my-kubeconfig.yaml -o jsonpath='{.contexts[?(@.name == "'"$(kubectl config current-context --kubeconfig=/path/to/my-kubeconfig.yaml)"'")].context.cluster}'`. – VonC May 05 '23 at 18:16
  • Do you want to get this information from within the pod I.e code deployed in a pod? – bashxx May 08 '23 at 15:53

2 Answers2

0

To retrieve the Kubernetes API server endpoint URL from the output of kubectl cluster-info, you can use the following command:

kubectl cluster-info | grep 'Kubernetes control plane is running at' | awk '{print $NF}'

This command will extract the API server endpoint URL from the output of kubectl cluster-info by searching for the line that starts with "Kubernetes control plane is running at", and then printing the last field of that line (which contains the URL).

Alternatively, you can also use the --context option with kubectl config view to specify the context for your Kubernetes cluster, and then extract the API server endpoint URL from the resulting JSON output:

kubectl config view --context=joseph-rancher-cluster-2 | jq -r '.clusters[].cluster.server'

This command uses the jq command-line JSON processor to extract the API server endpoint URL from the JSON output of kubectl config view. The --context option is used to specify the context for your Kubernetes cluster, and jq -r '.clusters[].cluster.server' is used to print the server field of all the clusters defined in the configuration file.

0

You can get the CoreDNS url from:

kubectl cluster-info | awk '{print $NF}' | head -1

and the control plane URL with:

kubectl cluster-info | awk '{print $NF}' | head -1 | tail -2 
Alez
  • 1,913
  • 3
  • 18
  • 22