Skip to content

Instantly share code, notes, and snippets.

@chinglinwen
Last active March 15, 2019 10:50
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 chinglinwen/21f019ca25800899d1863e4851b186f5 to your computer and use it in GitHub Desktop.
Save chinglinwen/21f019ca25800899d1863e4851b186f5 to your computer and use it in GitHub Desktop.
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