Skip to content

Instantly share code, notes, and snippets.

@itsvinayak
Created November 1, 2023 17:13
Show Gist options
  • Save itsvinayak/5423995de96d802e58e22041a18036eb to your computer and use it in GitHub Desktop.
Save itsvinayak/5423995de96d802e58e22041a18036eb to your computer and use it in GitHub Desktop.
πŸš€ Elevate your Kubernetes game with our powerful Bash script! Introducing a seamless solution designed for efficiency and productivity. Say goodbye to manual alias setup – our script automates the process, configuring essential Kubernetes aliases in your .bashrc file effortlessly. Supercharge your workflow and boost productivity with just a few …
#!/bin/bash
# Function to add or update an alias in .bashrc
add_alias_to_bashrc() {
local alias_name="$1"
local alias_command="$2"
# Check if the alias already exists in .bashrc
if grep -q "^alias $alias_name=" ~/.bashrc; then
# Update the existing alias
sed -i "s/^alias $alias_name=.*/alias $alias_name='$alias_command'/" ~/.bashrc
else
# Add a new alias to .bashrc
echo "alias $alias_name='$alias_command'" >> ~/.bashrc
fi
}
# Add or update aliases
# General:
add_alias_to_bashrc "k" "kubectl"
add_alias_to_bashrc "kg" "kubectl get"
add_alias_to_bashrc "kgp" "kubectl get pods"
add_alias_to_bashrc "kgs" "kubectl get services"
add_alias_to_bashrc "kgc" "kubectl get deployments"
add_alias_to_bashrc "kga" "kubectl get all"
add_alias_to_bashrc "kd" "kubectl describe"
add_alias_to_bashrc "kdp" "kubectl describe pods"
add_alias_to_bashrc "kds" "kubectl describe services"
add_alias_to_bashrc "kdc" "kubectl describe deployments"
add_alias_to_bashrc "kda" "kubectl describe all"
add_alias_to_bashrc "krm" "kubectl delete"
# Editing Kubernetes Resources with Editor:
add_alias_to_bashrc "ke" "kubectl edit"
# Applying Kubernetes Configurations:
add_alias_to_bashrc "ka" "kubectl apply -f"
# Rollout Commands for Deployments:
add_alias_to_bashrc "krl" "kubectl rollout"
add_alias_to_bashrc "krlr" "kubectl rollout restart"
add_alias_to_bashrc "krlh" "kubectl rollout history"
# Debugging Pods:
add_alias_to_bashrc "kl" "kubectl logs"
add_alias_to_bashrc "klf" "kubectl logs -f"
# Context and Configuration:
add_alias_to_bashrc "kctx" "kubectl config use-context"
add_alias_to_bashrc "kconfig" "kubectl config get-contexts"
# Kubectl Autocomplete:
source <(kubectl completion bash)
# Add more aliases as needed
# Apply the changes to the current shell session
source ~/.bashrc
echo "Aliases added/updated in ~/.bashrc."
@itsvinayak
Copy link
Author

Function to scale deployment resources (CPU and Memory)

scale_resources() {
    local deployment="$1"
    local cpu="$2"
    local memory="$3"
    kubectl set resources deployment "$deployment" --limits=cpu="$cpu",memory="$memory" --requests=cpu="$cpu",memory="$memory"
}

Alias for increasing resources of a deployment

alias increase-resources='scale_resources'

Usage example: increase-resources

Example:

increase-resources my-deployment 500m 512Mi

@itsvinayak
Copy link
Author

The kubectl scale deployment command in Kubernetes is used to scale the number of replicas of a specific deployment. Here's the general syntax:

kubectl scale deployment <deployment-name> --replicas=<desired-replicas>
  • <deployment-name>: Replace this with the name of the deployment you want to scale.
  • --replicas=<desired-replicas>: Replace <desired-replicas> with the number of replicas you want to set for the deployment.

For example, if you have a deployment named my-app and you want to scale it to 3 replicas, you would run:

kubectl scale deployment my-app --replicas=3

This command tells Kubernetes to adjust the number of replicas for the specified deployment (my-app in this case) to 3, effectively creating or terminating pods to match the desired replica count.

@itsvinayak
Copy link
Author

When creating an alias for kubectl logs command with a specific pod name, you can't dynamically provide the pod name as an argument to the alias. However, you can create a function instead of an alias to achieve dynamic pod name input. Here's how you can do it:

# Function to view previous logs of a specific pod
previous_logs() {
    kubectl logs "$1" -p
}

# Usage example: Call the function with the pod name as an argument
previous_logs <pod-name>

In this example, replace <pod-name> with the actual name of the pod you want to view logs for. When you run previous_logs <pod-name>, it will substitute <pod-name> with the provided pod name and execute the kubectl logs command accordingly.

Please note that you cannot achieve this level of dynamic substitution with a simple alias; you'll need to use a function or a script for this kind of parameterized behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment