Home > Blockchain >  how to display nodes information with a JSON request?
how to display nodes information with a JSON request?

Time:01-11

I know how to use the API to perform simple request such as display node information selecting node by labels value.

For example : curl http://localhost:8080/api/v1/nodes?labelSelector=kubernetes.io/role=worker3 Display informations about node whose role is worker3.

Is there a way to perform the same request using a JSON query ?

looked on the web to find a such example but did not find one.

CodePudding user response:

You can query with kubectl by label. The Roles of the node are just labels.

To return in yaml format

 kubectl get nodes -l node-role.kubernetes.io/worker -o yaml

To return in json format

 kubectl get nodes -l node-role.kubernetes.io/worker -o json

Update
Querying the api with json you can do like so:

curl http://localhost:8080/api/v1/nodes?{"node.kubernetes.io/worker01":"worker01"}

This in my case returns this:

{
  "kind": "NodeList",
  "apiVersion": "v1",
  "metadata": {
    "resourceVersion": "317238"
  },
  "items": [
    {
      "metadata": {
        "name": "worker01",
        "uid": "a2bec224-361f-49e9-8bba-b3b172816d6e",
        "resourceVersion": "316653",
        "creationTimestamp": "2022-12-24T11:04:43Z",
        "labels": {
          "beta.kubernetes.io/arch": "amd64",
          "beta.kubernetes.io/os": "linux",
          "kubernetes.io/arch": "amd64",
          "kubernetes.io/hostname": "worker01",
          "kubernetes.io/os": "linux",
          "microk8s.io/cluster": "true",
          "node.kubernetes.io/microk8s-worker": "microk8s-worker"
        },

............

As you can see it works, but you must analyse 2 things generally.

  1. the api version (can be different to v1, depends on the kubernetes version)
  2. the labels and property name.

The example above comes from microk8s, here i havent even Roles defined.

kubectl get node
NAME       STATUS   ROLES    AGE   VERSION
master     Ready    <none>   17d   v1.25.4
worker01   Ready    <none>   17d   v1.25.4

So i looked for some label that could extract the required data.

  • Related