Skip to content
Icon

recipes Pod Resources

Profile Avatar

Icon 1 5 Troubleshooting Commands

Icon 1 Last updated 13 weeks ago

Icon 1 Contributed by jon-funk



Troubleshooting Commands

Show Pods Without Resource Limit or Resource Requests Set in Namespace recipes

What does it do?

This command is used to retrieve information about running pods in a Kubernetes cluster, specifically targeting pods in a specific namespace with certain labels and filtering for those without resource limits set. The output is then formatted as JSON using the jq tool.

Command
kubectl get pods --context=gke_runwhen-nonprod-sandbox_us-central1_sandbox-cluster-1-cluster -n recipes  --field-selector=status.phase=Running -ojson | jq -r '[.items[] as $pod | ($pod.spec.containers // [][])[] | select(.resources.limits == null) | {pod: $pod.metadata.name, container_without_limits: .name}]'
IconCopy to clipboard Copied to clipboard

Learn more

This multi-line content is auto-generated and used for educational purposes. Copying and pasting the multi-line text might not function as expected.

# Get the list of pods in a Kubernetes cluster
kubectl get pods \
  # Use specified context for the command
  --context=${CONTEXT} \
  # Specify the namespace where the pods are located
  -n ${NAMESPACE} \
  # Filter pods by specific labels
  ${LABELS} \
  # Select only pods that are in the "Running" status
  --field-selector=status.phase=Running \
  # Output the result in JSON format
  -ojson | jq -r '[.items[] as $pod |
    ($pod.spec.containers // [][])[] |
      select(.resources.limits == null) |
        {pod: $pod.metadata.name, container_without_limits: .name}]'

This multi-line command provides clear and detailed explanations of each section, making it easier to understand and modify for newer or less experienced devops engineers. It also includes comments to explain each option used in the `kubectl` and `jq` commands.
Helpful Links

Show Pods Without Resource Limit or Resource Requests Set in Namespace recipes

What does it do?

This command is used to retrieve information about running pods in a Kubernetes cluster, specifically targeting pods in a specific namespace with certain labels and filtering for those without resource limits set. The output is then formatted as JSON using the jq tool.

Command
kubectl get pods --context=gke_runwhen-nonprod-sandbox_us-central1_sandbox-cluster-1-cluster -n recipes  --field-selector=status.phase=Running -ojson | jq -r '[.items[] as $pod | ($pod.spec.containers // [][])[] | select(.resources.requests == null) | {pod: $pod.metadata.name, container_without_requests: .name}]'
IconCopy to clipboard Copied to clipboard

Learn more

This multi-line content is auto-generated and used for educational purposes. Copying and pasting the multi-line text might not function as expected.

# Get the list of pods in a Kubernetes cluster
kubectl get pods \
  # Use specified context for the command
  --context=${CONTEXT} \
  # Specify the namespace where the pods are located
  -n ${NAMESPACE} \
  # Filter pods by specific labels
  ${LABELS} \
  # Select only pods that are in the "Running" status
  --field-selector=status.phase=Running \
  # Output the result in JSON format
  -ojson | jq -r '[.items[] as $pod |
    ($pod.spec.containers // [][])[] |
      select(.resources.limits == null) |
        {pod: $pod.metadata.name, container_without_limits: .name}]'

This multi-line command provides clear and detailed explanations of each section, making it easier to understand and modify for newer or less experienced devops engineers. It also includes comments to explain each option used in the `kubectl` and `jq` commands.
Helpful Links

Get Pod Resource Utilization with Top in Namespace recipes

What does it do?

This command uses kubectl to retrieve a list of running pods in a specific namespace and context, then uses the kubectl top command to display CPU and memory usage for each pod and its containers.

Command
for pod in $(kubectl get pods  -n recipes --context gke_runwhen-nonprod-sandbox_us-central1_sandbox-cluster-1-cluster -o custom-columns=":metadata.name" --field-selector=status.phase=Running); do kubectl top pod $pod -n recipes --context gke_runwhen-nonprod-sandbox_us-central1_sandbox-cluster-1-cluster --containers; done
IconCopy to clipboard Copied to clipboard

Learn more

This multi-line content is auto-generated and used for educational purposes. Copying and pasting the multi-line text might not function as expected.

# Set up variables for easy modification
LABELS="app=myapp" # replace with the appropriate label for your pods
NAMESPACE="my-namespace" # replace with the namespace where your pods are located
CONTEXT="my-context" # replace with the appropriate context for your Kubernetes cluster

# Use kubectl to get a list of running pods based on specified labels, namespace, and context
running_pods=$(kubectl get pods -l $LABELS -n $NAMESPACE --context $CONTEXT --field-selector=status.phase=Running -o custom-columns=":metadata.name")

# Iterate through the list of running pods and use kubectl top to display resource usage for each pod and its containers
for pod in $running_pods; do
    kubectl top pod $pod -n $NAMESPACE --context $CONTEXT --containers
done

This multi-line command is broken down into several steps and includes helpful comments to explain the purpose of each section. It also provides clear instructions for newer or less experienced devops engineers on how to modify the command to suit their specific environment.
Helpful Links

Identify VPA Pod Resource Recommendations in Namespace recipes

What does it do?

This script is a bash script that fetches VPA (Vertical Pod Autoscaler) recommendations from the Kubernetes cluster for a specific namespace and context, analyzes the current CPU and Memory requests in the cluster, and generates recommendations to adjust pod resources such as CPU and Memory based on the VPA recommendations. The final output is provided in JSON format.

Command
KUBERNETES_DISTRIBUTION_BINARY="kubectl" NAMESPACE="recipes" CONTEXT="gke_runwhen-nonprod-sandbox_us-central1_sandbox-cluster-1-cluster" LABELS="" UTILIZATION_THRESHOLD="95" DEFAULT_INCREASE="25" RESTART_AGE="10"  bash -c "$(curl -s https://raw.githubusercontent.com/runwhen-contrib/rw-cli-codecollection/main/codebundles/k8s-podresources-health/vpa_recommendations.sh)" _
IconCopy to clipboard Copied to clipboard

Learn more

This multi-line content is auto-generated and used for educational purposes. Copying and pasting the multi-line text might not function as expected.

# Start a bash script

# Initialize recommendations array
declare -a recommendations

# Function to convert memory to Mi
convert_memory_to_mib() {
    local memory=$1

    # Extract the number and unit separately
    local number=${memory//[!0-9]/}
    local unit=${memory//[0-9]/}

    case $unit in
        Gi)
            echo $(( number * 1024 ))  # Convert Gi to Mi
            ;;
        Mi)
            echo $number  # Already in Mi
            ;;
        Ki)
            echo $(( number / 1024 ))  # Convert Ki to Mi
            ;;
        *)
            echo $(( number / (1024 * 1024) ))  # Convert bytes to Mi
            ;;
    esac
}

# Function to convert CPU to millicores
convert_cpu_to_millicores() {
    local cpu=$1
    if [[ $cpu =~ ^[0-9]+m$ ]]; then
        echo ${cpu%m}
    else
        echo $(($cpu * 1000))  # Convert CPU cores to millicores
    fi
}

# ...
# (Remaining script left for comment...)
Helpful Links

Identify Resource Constrained Pods In Namespace recipes

What does it do?

It seems like the command you're trying to run isn't able to be processed or executed properly. It could be due to a variety of factors, such as incorrect syntax, missing software dependencies, or insufficient permissions.

Command
Could not render command
IconCopy to clipboard Copied to clipboard

Learn more

This multi-line content is auto-generated and used for educational purposes. Copying and pasting the multi-line text might not function as expected.

# This command is used to create a multi-line command with helpful comments for newer or less experienced devops engineers
# First, let's start by setting up a variable for the directory path 
directory_path="/path/to/directory"

# Next, change into the specified directory using the cd command
cd $directory_path

# Then, list all the files in the directory using the ls command
ls -l

# Finally, check the disk usage of the files in the directory using the du command
du -sh *
Helpful Links