adrian
adrian

Reputation: 2376

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

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

[achang@p3dlwsbkn50d51 ~]$ curl -Gs http://localhost:10255/pods/
404 page not found

Upvotes: 7

Views: 31037

Answers (6)

COvayurt
COvayurt

Reputation: 895

The following comes more handy to me :

kubectl get node -o yaml |grep host |awk '{print $2}' |grep $(hostname)

Note that the hostnames should have been arranged properly

Upvotes: 0

ThaSami
ThaSami

Reputation: 141

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/

Upvotes: 6

takehiro iyatomi
takehiro iyatomi

Reputation: 823

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.

Upvotes: 15

pr-pal
pr-pal

Reputation: 3558

kubelet is the entity running in each node and is like the "host agent" with which kube-api-server interacts to get the node specific information and also to do the node specific tasks.

Kubelet has the information about the node name. Typically, the node name is given with "--hostname-override" option when starting the kubelet.

So, the following command would give the node name if --hostname-override is set.

ps -eaf | grep kubelet | tr ' ' '\n' | grep "--hostname-override" | awk -F= ' { print $2 } '

Else, there must be kubelet APIs to give this information, but kubelet APIs are undocumented and hence it is better not to suggest them in stackoverflow

Upvotes: 0

Nicolas Pepinster
Nicolas Pepinster

Reputation: 6239

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/

Upvotes: 5

Tim Allclair
Tim Allclair

Reputation: 7817

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"

Upvotes: 2

Related Questions