A Deep Dive into Helpful Bash Commands for Kubernetes (Show Logs and Describe Pods)


kubernetes A Deep Dive into Helpful Bash Commands for Kubernetes (Show Logs and Describe Pods) bash script BASH Shell cloud knowledgebase Kubernetes Microsoft Azure

kubernetes

Kubernetes, a powerful and flexible platform for managing containerized workloads and services, offers a host of commands for querying and manipulating clusters.

Bash Function to Show Logs and Describe Pods of a Kubernetes Cluster

Today, we’ll examine a set of Bash commands that automate the process of retrieving and displaying information about Kubernetes Pods. The script we’ll focus on not only retrieves a list of Pods but also describes each one and shows their logs. Here’s the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
## first parameter: namespace
## second optional parameter: local kubeconfig file
show_and_describe_pods() 
{
    local namespace=$1
    local kubeconfig=$2
    local config=""
    if [[ ! -z "$kubeconfig" ]]; then
        config="--kubeconfig $kubeconfig"
    fi
    local all_pods=$(kubectl get pods --kubeconfig $config -n $namespace | awk 'NR!=1 {print $1}')
    for pod in $all_pods; do
        echo ----------------- Describing Pod $pod ----------------------------------------
        kubectl describe pod $pod $config -n apps
 
        echo ----------------- Showing Logs for Pod $pod ----------------------------------------
        kubectl logs $pod -$config -n apps
    done
}
## first parameter: namespace
## second optional parameter: local kubeconfig file
show_and_describe_pods() 
{
    local namespace=$1
    local kubeconfig=$2
    local config=""
    if [[ ! -z "$kubeconfig" ]]; then
        config="--kubeconfig $kubeconfig"
    fi
    local all_pods=$(kubectl get pods --kubeconfig $config -n $namespace | awk 'NR!=1 {print $1}')
    for pod in $all_pods; do
        echo ----------------- Describing Pod $pod ----------------------------------------
        kubectl describe pod $pod $config -n apps

        echo ----------------- Showing Logs for Pod $pod ----------------------------------------
        kubectl logs $pod -$config -n apps
    done
}

Here, we define a bash function `show_and_describe_pods` that takes two parameters. The first parameter specifies the namespace, and the second (optional) parameter specifies the local kubeconfig file if given.

Then, we use the following to get all the pods:

1
kubectl get pods --kubeconfig $config -n $namespace
kubectl get pods --kubeconfig $config -n $namespace

However, we need to use awk to print the pod names only – and also need to skip the first line, and finally store the output in a variable:

1
local all_pods=$(kubectl get pods --kubeconfig $config -n $namespace | awk 'NR!=1 {print $1}')
local all_pods=$(kubectl get pods --kubeconfig $config -n $namespace | awk 'NR!=1 {print $1}')

Now, let’s break down this script step by step:

The for Loop

At the script’s core, there’s a for loop that iterates over a list of Pods. In Bash scripting, a for loop is a control flow statement for specifying iterations, enabling a set of commands to be executed for every item in a list.

Retrieving Pods

The list of Pods is retrieved using the kubectl get pods command, which returns all Pods in a specific namespace (in this case, “apps”). kubectl is the Kubernetes command-line tool. The –kubeconfig flag specifies the path to the kubeconfig file, which contains cluster, context, and authentication information. The kubectl get pods command is often used in scripts and automation as it provides concise, human-readable output.

The output of kubectl get pods is piped to awk, a powerful text-processing command. NR!=1 {print $1} is an awk script that says “for every line that is not the first one (NR!=1), print the first field (print $1)”.

Describing and Logging Pods

Once the Pod names are fetched, the loop iterates over each one, and two operations are performed: description and logging.

The kubectl describe pod $pod command shows the detailed state of the pod, including information like the status of each container in the Pod, the Pod’s start time, and the events taking place within the Pod.

kubectl logs $pod prints out the logs for that particular Pod, which is crucial for debugging and understanding the behavior of applications running within the Pods.

The echo command is used to create visual separators in the output, indicating when the description of a Pod begins and when the logs start. It makes the output more readable, especially when dealing with multiple Pods.

Conclusion

This script is a powerful demonstration of the flexibility of command-line tools like Bash and kubectl. With a few lines of code, we can automate the process of retrieving, describing, and logging all Pods in a Kubernetes namespace. This can be highly useful for system administrators who need to monitor the status of Pods or developers who want to debug their applications.

Remember, to effectively use this script, you need to replace $LOCAL_KUBE_CONF with the path to your actual kubeconfig file. Keep exploring and using the power of these commands for managing your Kubernetes clusters. Happy scripting!

This has been tested on Azure Cloud (AKS) – see this also: Common Azure Kubernetes Services AKS Commands (Cheet Sheet)

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
879 words
Last Post: Career Guidance and Required Skills to Become a Software Engineer
Next Post: TRON Blockchain: How to Check If a Node is synchronized and Fully Functional?

The Permanent URL is: A Deep Dive into Helpful Bash Commands for Kubernetes (Show Logs and Describe Pods)

Leave a Reply