To get the status of a Kubernetes node using Go, you can use the Kubernetes Client-Go library. This allows you to interact with your Kubernetes cluster programmatically, including querying node status and various node conditions such as Ready, MemoryPressure, DiskPressure, etc.

Steps to Get Kubernetes Node Status Using Go

  1. Install Client-Go: First, you need to install the client-go package to interact with Kubernetes.
  2. Set Up Kubernetes Client: Use the Kubernetes client to authenticate with the cluster.
  3. Query Node Status: Use the Kubernetes client to list nodes and get their status and conditions.

Step 1: Install Kubernetes Client-Go

You can install the client-go package using:

bash
go get k8s.io/client-go@v0.26.1 # Adjust version as needed
go get k8s.io/client-go/tools/clientcmd
go get k8s.io/apimachinery/pkg/apis/meta/v1

Step 2: Initialize the Kubernetes Client

Use either In-Cluster configuration (when the app is running inside the Kubernetes cluster) or Kubeconfig (for running locally).

  • In-Cluster Configuration: Use when your Go application is running inside a Kubernetes cluster.
  • Kubeconfig: Use when you’re running the Go program outside of Kubernetes.

Step 3: List Nodes and Fetch Status

Here’s an example that fetches the status of all nodes in the cluster:

go

package main

import (
“context”
“fmt”
“log”
“os”

v1 “k8s.io/apimachinery/pkg/apis/meta/v1”
“k8s.io/client-go/kubernetes”
“k8s.io/client-go/rest”
“k8s.io/client-go/tools/clientcmd”
)

func main() {
var config *rest.Config
var err error

// Use InClusterConfig if running inside Kubernetes, otherwise fall back to kubeconfig
if config, err = rest.InClusterConfig(); err != nil {
// Use kubeconfig file for local development
kubeconfig := os.Getenv(“KUBECONFIG”)
config, err = clientcmd.BuildConfigFromFlags(“”, kubeconfig)
if err != nil {
log.Fatalf(“Error building kubeconfig: %v”, err)
}
}

// Create a new Kubernetes client
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf(“Failed to create Kubernetes client: %v”, err)
}

// Get a list of nodes in the cluster
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
if err != nil {
log.Fatalf(“Error fetching nodes: %v”, err)
}

// Iterate over the nodes and print their status
for _, node := range nodes.Items {
fmt.Printf(“Node Name: %s\n”, node.Name)

// Print the status conditions
for _, condition := range node.Status.Conditions {
fmt.Printf(” – Condition Type: %s, Status: %s, Reason: %s\n”, condition.Type, condition.Status, condition.Reason)
}

// Print additional details such as node’s capacity, allocatable resources, etc.
fmt.Printf(” CPU Capacity: %s, Memory Capacity: %s\n”, node.Status.Capacity.Cpu().String(), node.Status.Capacity.Memory().String())
fmt.Printf(” CPU Allocatable: %s, Memory Allocatable: %s\n”, node.Status.Allocatable.Cpu().String(), node.Status.Allocatable.Memory().String())

fmt.Println(“—————————-“)
}
}

Explanation:

  1. Cluster Configuration: The program first tries to use the In-Cluster configuration if the Go application is running within the Kubernetes cluster. If not, it uses the local kubeconfig file to authenticate with the cluster.
  2. Client Initialization: A Kubernetes client is initialized using the kubernetes.NewForConfig() function, which allows interaction with the cluster.
  3. Fetch Node Information: The program lists all nodes in the cluster using clientset.CoreV1().Nodes().List() and iterates over them.
  4. Print Node Status: For each node, the code checks its conditions (Ready, MemoryPressure, DiskPressure, etc.) and prints them.

Node Status Fields:

  • Node.Status.Conditions: This contains various conditions about the node, such as:
    • Ready: Indicates whether the node is ready to accept pods.
    • MemoryPressure: Indicates if the node is experiencing memory pressure.
    • DiskPressure: Indicates if the node is low on disk space.
    • PIDPressure: Indicates if the node is under pressure due to running too many processes.
  • Node.Status.Capacity: Shows the total resource capacity of the node (CPU, memory, etc.).
  • Node.Status.Allocatable: Shows the amount of resources available for scheduling pods after reserving system overhead.

Example Output:

bash
Node Name: gke-cluster-1-default-pool-xyz
- Condition Type: Ready, Status: True, Reason: KubeletReady
- Condition Type: MemoryPressure, Status: False, Reason: KubeletHasSufficientMemory
- Condition Type: DiskPressure, Status: False, Reason: KubeletHasNoDiskPressure
- Condition Type: PIDPressure, Status: False, Reason: KubeletHasSufficientPID
CPU Capacity: 4, Memory Capacity: 16Gi
CPU Allocatable: 3.9, Memory Allocatable: 15Gi
----------------------------

This Go program will monitor the status of the nodes in your Kubernetes cluster and print their current conditions, allowing you to programmatically monitor the health of the nodes.

Sign In

Sign Up