Kubernetes: How do I know what node I'm on?

11,749

Solution 1

There are lots of different IDs and names tied to kubernetes nodes, it depends on which you are looking for. If you want to query the API server for node info, you're most likely looking for the node name. The node name is often the same as the hostname, but if not the easiest way to find it is to query the kubelet for running pods, and see what node they're running on:

$ curl -Gs http://localhost:10255/pods/ | grep -o '"nodeName":"[^"]*"' | head -n 1
"nodeName":"e2e-test-stclair-minion-8o3b"

Other IDs can be found by querying the node spec:

$ curl -Gs http://localhost:10255/spec/ | grep -oE '(machine_|system_uu|boot_)id":.*'
machine_id": "",
system_uuid": "CB7FAAA0-3A53-5FE4-4285-D33D03FEBA6C",
boot_id": "8b89b8f5-5fbb-4cc0-82e4-7c57ec11f656",

Finally, externalID and providerID can be queried from the API server:

$ kubectl get nodes e2e-test-stclair-minion-8o3b -o=jsonpath="externalID:{.spec.externalID}; providerID:{.spec.providerID}"

EDIT:

If the above fails and you have access to the api server, you can just look for the node that matches the hostname of the desired node:

$ NODEHOST="your-host"
$ kubectl get nodes | grep "hostname=$NODEHOST"

Solution 2

I found current kubernetes node (I check minikube, gke node pool) has file /etc/machine-id, which cat /etc/machine-id is unique for each kubernetes nodes and match with correspond kubectl get nodes -o json | jq -r .items[].status.nodeInfo.machineID.

because it does not need API invocation, I think this way is easier to combine with shell or container.

Solution 3

You can obtain the node name adding wide output option to kubectl :

// List all pods in plain-text output format and includes additional information (such as node name). $ kubectl get pods -o wide

more options : https://kubernetes.io/docs/user-guide/kubectl-overview/

Solution 4

Now you would better expose them during the deployment if you intended to use them later through env-variables.

Example:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-envars-fieldref
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "sh", "-c"]
      args:
      - while true; do
          echo -en '\n';
          printenv MY_NODE_NAME MY_POD_NAME MY_POD_NAMESPACE;
          printenv MY_POD_IP MY_POD_SERVICE_ACCOUNT;
          sleep 10;
        done;
      env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName
  restartPolicy: Never

refer to

https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/

Share:
11,749

Related videos on Youtube

Author by

adrian

Read my medium.com blog for more in depth tech answers!

Updated on June 22, 2022

Comments

  • adrian 9 months

    If I ssh into a Kubernetes node, how do I figure out the UUID for the node so I can query the master API for information specific to the node?

    Tried this so far

    root     13020  2.5  1.0 410112 41660 ?        Ssl  Jan25  26:04 /usr/bin/kubelet --logtostderr=true --v=0 --api_servers=http://10.32.140.181:8080 --address=0.0.0.0 --port=10250 --allow_privileged=false --maximum-dead-containers=1 --max-pods=14
    [[email protected] ~]$ curl -Gs http://localhost:10255/pods/
    404 page not found
    
  • adrian about 7 years
    Hey I tried your suggestion, but getting a 404 not found?
  • Tim Allclair
    Tim Allclair about 7 years
    Can you post the exact command entered? Some of the paths are finicky about the trailing slash.
  • adrian about 7 years
    curl -Gs localhost:10255/pods -> is empty, I know I'm not running any pods but 404 not found is odd
  • Tim Allclair
    Tim Allclair about 7 years
    Which version of kubernetes are you running? You should at least have a kube-proxy pod (in the kube-system namespace)
  • Tim Allclair
    Tim Allclair about 7 years
    (added another option for getting the node name)
  • Tim Allclair
    Tim Allclair about 7 years
  • elia
    elia almost 5 years
    the best answer
  • Scott Kausler
    Scott Kausler almost 4 years
    No idea why this answer doesn't have more votes. First, it actually solve the problem with no guess work. Second, it doesn't require networking or any special tools like kubectl or jq. I can map the file into my pod and I'm good to go.

Related