Skip to content

Instantly share code, notes, and snippets.

@ngrinkevich
Forked from tamas-molnar/kubectl-shortcuts.sh
Created January 25, 2018 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngrinkevich/72d519eeaa57dfeca8d002ec804a56ea to your computer and use it in GitHub Desktop.
Save ngrinkevich/72d519eeaa57dfeca8d002ec804a56ea to your computer and use it in GitHub Desktop.
aliases and shortcuts for kubectl
alias kc='kubectl'
alias kclf='kubectl logs --tail=200 -f'
alias kcgs='kubectl get service -o wide'
alias kcgd='kubectl get deployment -o wide'
alias kcgp='kubectl get pod -o wide'
alias kcgn='kubectl get node -o wide'
alias kcdp='kubectl describe pod'
alias kcds='kubectl describe service'
alias kcdd='kubectl describe deployment'
alias kcdf='kubectl delete -f'
alias kcaf='kubectl apply -f'
alias kcci='kubectl cluster-info'
function kcpp() {
kcgp | grep $@ | head -1 | awk '{ print $1}' | xargs -i kubectl port-forward {} 8080:8080 > /dev/null 2>&1 &
sleep 2;
xdg-open http://localhost:8080/__health > /dev/null 2>&1;
}
function kcfp() { kubectl get pod -o wide| grep $@; }
function kcfs() { kubectl get service -o wide| grep $@; }
function kcfd() { kubectl get deployment -o wide | grep $@; }
function kcssh() { ssh -A core@$(kubectl get node -o wide | grep $@ | awk '{ print $4}'); }
function kcxsh() { kubectl exec -ti $@ sh; }
function kcxbash() { kubectl exec -ti $@ bash; }
function kcres() {
echo "Scaling $1"
desired_replicas=$(kubectl get deployments --selector=app=$1 -o jsonpath='{$.items[0].spec.replicas}');
echo "Desired nf or replicas: $desired_replicas";
echo "Scaling deployment $1 down to 0 replicas...";
kubectl scale --replicas=0 deployments/$1;
current_replicas=$(kubectl get deployments --selector=app=$1 -o jsonpath='{$.items[0].status.availableReplicas}')
while [ ! -z "$current_replicas" ]; do
sleep 1;
current_replicas=$(kubectl get deployments --selector=app=$1 -o jsonpath='{$.items[0].status.availableReplicas}')
done;
echo "Scaling deployment $1 up to $desired_replicas replicas...";
kubectl scale --replicas=$desired_replicas deployments/$1;
if [ "$2" == "skipCheck" ]; then
echo "Skipping healthchecks"
return
fi
default_sleep=10
initial_sleep=$(kubectl get deployments --selector=app=$1 -o jsonpath='{$.items[0].spec.template.spec.containers[0].readinessProbe.initialDelaySeconds}')
initial_sleep=${initial_sleep:-10}
echo "Waiting $initial_sleep seconds for the first readiness probe check..."
sleep $initial_sleep
period_sleep=$(kubectl get deployments --selector=app=$1 -o jsonpath='{$.items[0].spec.template.spec.containers[0].readinessProbe.periodSeconds}')
period_sleep=${period_sleep:-10}
while [ "$current_replicas" != "$desired_replicas" ]; do
echo "Waiting $period_sleep seconds until checking the node count"
sleep $period_sleep
current_replicas=$(kubectl get deployments --selector=app=$1 -o jsonpath='{$.items[0].status.availableReplicas}')
current_replicas=${current_replicas:-0}
echo "Current nr of replicas: $current_replicas"
done;
echo "$1 restarted"
}
function kcgnt() { for machine in $(kcgn | tail -n +2 | awk '{ print $1 }' ); do echo -n "${machine}: "; echo $(kc describe node $machine | grep -i taints); done | sort -k 2; }
function kcstat() {
for node in $(kubectl get nodes | tail -n +2 | awk '{print $1}'); do
echo $node
echo -e "$(kubectl describe node $node | grep -A 4 "Allocated resources")\n";
done
}
function kcreach(){
for public_ip in $(kubectl get nodes -o wide | tail -n +2 | awk '{print $4}'); do
echo $public_ip
echo -e "$(ssh core@$public_ip date)\n"
done
}
function kcready() {
for node in $(kubectl get nodes | tail -n +2 | awk '{print $1}'); do
echo $node
echo -e "$(kubectl describe node $node | grep "Ready")\n";
done
}
export PS1="[\W]\[\033[0;33m\][\t]\[\033[0;31m\][\${FLEETCTL_TUNNEL%-tunnel-up.ft.com}][\${KUBE_ENV}]\[\033[0m\]\$ "
. ~/services_autocomplete.sh
source <(kubectl completion bash)
export FT_K8S_CONFIG_FOLDER=your-folder-here
alias k8s-del='export KUBECONFIG=$FT_K8S_CONFIG_FOLDER/kubeconfig; export KUBE_ENV=k8s-delivery'
alias k8s-pub='export KUBECONFIG=$FT_K8S_CONFIG_FOLDER/kubeconfig-pub; export KUBE_ENV=k8s-pub'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment