To check the status of nodes in a Kubernetes cluster, you can use various methods, depending on your preferred tool or approach. Below are detailed instructions on how to check node status:
—
### **1. Using `kubectl` (Command Line)**
The `kubectl` command-line tool is the most common way to check node status.
#### **a. View Node Status Summary**
“`bash
kubectl get nodes
“`
This outputs a table with the following columns:
– **NAME**: Node name.
– **STATUS**: The overall status of the node (e.g., `Ready`, `NotReady`).
– **ROLES**: Role of the node (e.g., `master`, `worker`).
– **AGE**: How long the node has been part of the cluster.
– **VERSION**: Kubernetes version on the node.
Example output:
“`
NAME STATUS ROLES AGE VERSION
node1 Ready master 5d v1.28.0
node2 Ready worker 5d v1.28.0
node3 NotReady worker 4d v1.28.0
“`
#### **b. View Node Conditions in Detail**
“`bash
kubectl describe node <node-name>
“`
This provides detailed information about a specific node, including its conditions:
– `Ready`: Indicates if the node is ready to accept pods.
– `DiskPressure`: Checks if the node is running out of disk space.
– `MemoryPressure`: Checks if the node is running low on memory.
– `PIDPressure`: Indicates if the node is running out of process IDs.
#### **c. Check Node Status with Wide Output**
“`bash
kubectl get nodes -o wide
“`
This adds additional information such as IP addresses, node labels, and more.
—
### **2. Using `kubectl top` for Resource Utilization**
To see the current resource usage (CPU and memory) of nodes:
“`bash
kubectl top nodes
“`
Example output:
“`
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
node1 120m 6% 500Mi 25%
node2 250m 12% 750Mi 37%
node3 0m 0% 0Mi 0%
“`
> Note: Ensure the **metrics-server** is installed in your cluster for this command to work.
—
### **3. Using Kubernetes Dashboard**
The Kubernetes Dashboard provides a visual way to check node status:
1. Install the Kubernetes Dashboard:
“`bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
“`
2. Access the dashboard:
“`bash
kubectl proxy
“`
Visit [http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/](http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/) in your browser.
3. Navigate to the **Nodes** section to view their status and resource usage.
—
### **4. Programmatic Access (Using Kubernetes Client Libraries)**
Using Go’s **client-go** library:
“`go
package main
import (
“context”
“fmt”
“log”
“os”
“path/filepath”
metav1 “k8s.io/apimachinery/pkg/apis/meta/v1”
“k8s.io/client-go/kubernetes”
“k8s.io/client-go/tools/clientcmd”
)
func main() {
// Load kubeconfig
kubeconfig := filepath.Join(os.Getenv(“HOME”), “.kube”, “config”)
config, err := clientcmd.BuildConfigFromFlags(“”, kubeconfig)
if err != nil {
log.Fatalf(“Error building kubeconfig: %v”, err)
}
// Create Kubernetes clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf(“Error creating clientset: %v”, err)
}
// Get node list
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Fatalf(“Error listing nodes: %v”, err)
}
// Print node statuses
for _, node := range nodes.Items {
for _, condition := range node.Status.Conditions {
if condition.Type == “Ready” {
fmt.Printf(“Node: %s, Status: %s\n”, node.Name, condition.Status)
}
}
}
}
“`
—
### **5. Using Monitoring Tools**
– **Prometheus/Grafana**: Provides dashboards with detailed node metrics and statuses.
– **Lens IDE**: A Kubernetes management tool with a GUI to visualize cluster resources.
—
### **6. Directly Query the Kubernetes API**
You can query the Kubernetes API to get node information:
“`bash
curl -k -H “Authorization: Bearer <TOKEN>” https://<API_SERVER>/api/v1/nodes
“`
Replace `<TOKEN>` with a valid token and `<API_SERVER>` with your Kubernetes API server’s URL. This returns detailed node data in JSON format.
—
### **Common Node Status Types**
– **Ready**: Node is healthy and ready to schedule pods.
– **NotReady**: Node is not healthy or cannot schedule pods.
– **SchedulingDisabled**: Node is cordoned (e.g., for maintenance).
– **Unknown**: The node controller cannot reach the node.
—
### **Troubleshooting Tips**
– Check `kubectl logs` for more details if a node is `NotReady`.
– Use `kubectl get events` to see recent events that might indicate issues.
– Ensure your network and cluster components (e.g., kubelet, kube-proxy) are functioning correctly.
By combining these methods, you can efficiently monitor and troubleshoot node statuses in your cluster.
