Kubernetes – Menu Command line – Common Tasks

Please find one of my favourite menu driven scripts that I use with developers if they keen to use the command line vs tools like Rancher to get to know Kubernetes e.g. Scaling Services, View Nginx Log files for suspicious activity etc

#!/bin/bash

# Function to display menu
display_menu() {
    clear
    echo "=== Kubernetes Administrator Menu ==="
    echo "1. Get Pods"
    echo "2. Get Services"
    echo "3. Describe Pod"
    echo "4. Describe Service"
    echo "5. View Log Files"
    echo "6. View High-Count Ingress HTTP Requests"
    echo "7. Scale Up Deployment"
    echo "8. Scale Down Deployment"
    echo "9. Exit"
    echo
    read -p "Enter your choice: " choice
    echo
}

# Function to get pods
get_pods() {
    kubectl get pods
    echo
    read -p "Press enter to continue..."
}

# Function to get services
get_services() {
    kubectl get services
    echo
    read -p "Press enter to continue..."
}

# Function to describe a pod
describe_pod() {
    read -p "Enter the pod name: " pod_name
    kubectl describe pod $pod_name
    echo
    read -p "Press enter to continue..."
}

# Function to describe a service
describe_service() {
    read -p "Enter the service name: " service_name
    kubectl describe service $service_name
    echo
    read -p "Press enter to continue..."
}

# Function to view log files
view_logs() {
    read -p "Enter the pod name: " pod_name
    read -p "Enter the container name (press enter for all containers): " container_name

    if [ -z "$container_name" ]; then
        kubectl logs $pod_name
    else
        kubectl logs $pod_name -c $container_name
    fi

    echo
    read -p "Press enter to continue..."
}

# Function to view high-count ingress HTTP requests
view_high_count_requests() {
    read -p "Enter the log file name: " log_file
    read -p "Enter the high count threshold: " threshold

    awk -v threshold=$threshold '/ingress/ { count[$NF]++ } END { for (ip in count) { if (count[ip] > threshold) print count[ip], ip } }' $log_file

    echo
    read -p "Press enter to continue..."
}

# Function to scale up a deployment
scale_up_deployment() {
    read -p "Enter the deployment name: " deployment_name
    read -p "Enter the number of replicas to scale up: " replicas

    kubectl scale deployment $deployment_name --replicas=+$replicas
    echo "Deployment scaled up successfully!"
    echo
    read -p "Press enter to continue..."
}

# Function to scale down a deployment
scale_down_deployment() {
    read -p "Enter the deployment name: " deployment_name
    read -p "Enter the number of replicas to scale down: " replicas

    kubectl scale deployment $deployment_name --replicas=-$replicas
    echo "Deployment scaled down successfully!"
    echo
    read -p "Press enter to continue..."
}

# Main script
while true; do
    display_menu

    case $choice in
        1) get_pods;;
        2) get_services;;
        3) describe_pod;;
        4) describe_service;;
        5) view_logs;;
        6) view_high_count_requests;;
        7) scale_up_deployment;;
        8) scale_down_deployment;;
        9) exit;;
        *) echo "Invalid choice. Please try again.";;
    esac
done

  • Uncategorized

Leave a comment