Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
k8s shortcuts to simplify kubectl command for daily use
alias k8sonline="export KUBECONFIG=~/.kube/config; export PS1=\"[\u@\h k8sonline \W]\$ \""
alias k8stest="export KUBECONFIG=~/.kube/config-test; export PS1=\"[\u@\h k8stest \W]\$ \""
getns(){
pod="$1"
kubectl get pod --all-namespaces | grep $pod |head -1 | awk '{ print $1 }'
}
getpod(){
pod="$1"
ns="$( getns $pod )"
kubectl get pod -n $ns |grep $pod | awk '{ print $1 }'
}
8log () {
pod=$1
ns="$( getns $pod )"
p="$( getpod $pod )"
shift 1
kubectl logs --tail=100 -f -n $ns $p $@
}
8logf () {
pod=$1
ns="$( getns $pod )"
if [ "x$ns" = "x" ]; then
echo "ns not found for $p"
return 1
fi
p="$( getpod $pod )"
if [ "x$p" = "x" ]; then
echo "pod not found for $p"
return 1
fi
shift
#echo "pod: $pod, ns: $ns, @: $@"
kubectl logs -n $ns $p $@
}
8del () {
pod=$1
ns=$2
if [ "x$ns" = "x" ]; then
ns="$( getns $pod )"
fi
shift 2
p="$( getpod $pod )"
kubectl delete pod -n $ns $p $@
}
8delf () {
pod=$1
ns=$2
if [ "x$ns" = "x" ]; then
ns="$( getns $pod )"
fi
shift 2
p="$( getpod $pod )"
kubectl delete pod -n $ns $p --force --grace-period=0 $@
}
8sh () {
pod=$1
ns=$2
if [ "x$ns" = "x" ]; then
ns="$( getns $pod )"
fi
shift 2
p="$( getpod $pod | head -1 )"
kubectl exec -it -n $ns $p sh
}
8s () {
pod=$1
ns=$2
if [ "x$ns" = "x" ]; then
ns="$( getns $pod )"
fi
shift 2
p="$( getpod $pod | head -1 )"
kubectl exec -it -n $ns $p bash
}
8dec () {
pod=$1
ns=$2
if [ "x$ns" = "x" ]; then
ns="$( getns $pod )"
fi
shift 2
p="$( getpod $pod )"
kubectl describe pod -n $ns $p
}
8pods () {
pod=$1
kubectl get pods -o wide --all-namespaces | grep $pod
}
8jobs () {
pod=$1
kubectl get job -o wide --all-namespaces | grep $pod
}
8rerun () {
pod=$1
ns=$2
if [ "x$ns" = "x" ]; then
ns="$( getns $pod )"
fi
shift 2
which jq >/dev/null
if [ $? -ne 0 ]; then
echo "no jq command found, exit"
return
fi
p="$( getpod $pod | awk 'NF{NF-=1};1' FS="-" OFS="-" )"
kubectl get job -n $ns "$p" -o json | jq 'del(.spec.selector)' | jq 'del(.spec.template.metadata.labels)' | kubectl replace --force -f -
}
8info () {
kubectl get pod,deploy,ep,svc,ing -o wide --all-namespaces | grep $@
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment