Created
November 1, 2023 17:13
-
-
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 β¦
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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." |
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
The
kubectl scale deployment
command in Kubernetes is used to scale the number of replicas of a specific deployment. Here's the general syntax:<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: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.