Skip to content

Instantly share code, notes, and snippets.

@yogeek
Last active May 18, 2022 12:27
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 yogeek/cb59aab992e2271b34060c789ab30d92 to your computer and use it in GitHub Desktop.
Save yogeek/cb59aab992e2271b34060c789ab30d92 to your computer and use it in GitHub Desktop.
K8S utils
# kube-ps1
export KUBE_PS1_SYMBOL_USE_IMG=true
#---------------------------------------------------------
# Aliases
#---------------------------------------------------------
alias k='kubectl'
alias kc='kubectx'
alias kn='kubens'
alias kgp='kubectl get po'
alias kdp='kubectl describe po'
alias kgpw='kubectl get po -o wide'
alias kgpa='kubectl get po --all-namespaces'
alias kgpaw='kubectl get po -o wide --all-namespaces'
alias kgs='kubectl get svc'
alias kds='kubectl describe svc'
alias kgsa='kubectl get svc --all-namespaces'
alias kgd='kubectl get deploy'
alias kdd='kubectl describe deploy'
alias kgda='kubectl get deploy --all-namespaces'
alias kgds='kubectl get ds'
alias kdds='kubectl describe ds'
alias kgdsa='kubectl get ds --all-namespaces'
alias kgall='kubectl get all'
alias kgallall='kubectl get all -A'
alias kgn='kubectl get nodes -o wide'
alias kdn='kubectl describe nodes'
alias kgnm="kubectl get no --selector='node-role.kubernetes.io/master'"
alias kgnw="kubectl get no --selector='!node-role.kubernetes.io/master' -o wide --sort-by '.metadata.creationTimestamp'"
alias kgnlm="kubectl get no --selector='node-role.kubernetes.io/master' -o=custom-columns=MASTER_LABELS:.metadata.labels | sed 's/\s/\n/g' | sed 's/map/\n/g' | sed -e 's/\[//' -e 's/\]//'"
alias kgnlw="kubectl get no --selector='!node-role.kubernetes.io/master' -o=custom-columns=WORKER_LABELS:.metadata.labels | sed 's/\s/\n/g' | sed 's/map/\n/g' | sed -e 's/\[//' -e 's/\]//'"
alias kev='kubectl get ev --field-selector=type=Warning --sort-by=metadata.creationTimestamp'
#---------------------------------------------------------
# Events for a specific node
#---------------------------------------------------------
kubectl get events --sort-by='.metadata.creationTimestamp' --field-selector involvedObject.kind=Node | grep ip-10-20-102-139.ec2.internal
#---------------------------------------------------------
# Finalizer k8s bug which prevent naemspaces to be deleted
# (staying in "Terminating" state)
#---------------------------------------------------------
finalize() {
for i in $(kubectl get ns | grep Terminating | cut -d ' ' -f1); do
NAMESPACE=$i; kubectl proxy &
cur_pid=$!
kubectl get namespace $NAMESPACE -o json | jq '.spec = {"finalizers":[]}' > temp.json
curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize
rm -f temp.json
kill $cur_pid
done
wait
}
##########################
# KUBECONFIG
##########################
export KUBECONFIG=""
for kubecfg in $(find ~/.kube/ -name "kubeconfig*"); do
export KUBECONFIG=${KUBECONFIG}:${kubecfg}
done
# Merge kubeconfig files into ~/.kube/config for toosl like kube-forwarder to detect clusters
# kubectl config view --merge --flatten > ~/.kube/config
#---------------------------------------------------------
# kubeconfig per session
#---------------------------------------------------------
file="$(mktemp -t "kubectx.XXXXXX")"
export KUBECONFIG="${file}:${KUBECONFIG}"
cat <<EOF >"${file}"
apiVersion: v1
kind: Config
current-context: ""
EOF
# https://kapeli.com/cheat_sheets/Kubernetes.docset/Contents/Resources/Documents/index
#---------------------------------------------------------
# Function to test if a k8s node is ready
# Example : is_node_ready $NODE_ID
#---------------------------------------------------------
function is_node_ready() {
ready=$(kubectl get nodes -o jsonpath='{.items[?(@.metadata.name=="'$1'")].status.conditions[?(@.type=="Ready")].status}')
[[ "$ready" == "True" ]] && return 0 || return 1
}
#---------------------------------------------------------
# List pods' images
#
# kimg : list image in the current namespaces
# kimg -n ns1 : list images in the ns1 namespace
# kimg -A : list images in all namespaces
#---------------------------------------------------------
cat <<EOF >> $HOME/kubectl-fmt/k8s-images.fmt
NAMESPACE NAME IMAGE INIT_IMAGE
metadata.namespace metadata.name spec.containers[0].image spec.initContainers[0].image
EOF
function kimg() {
kubectl get pods -o custom-columns-file=$KFMT/k8s-images.fmt "$@"
}
#---------------------------------------------------------
# kubefwd
#---------------------------------------------------------
function kf() {
# Get current context
ctx="$(kubectl config view -o=jsonpath='{.current-context}')"
# Get namespace
ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${ctx}\")].context.namespace}")"
echo "#################################################################"
echo "Forwarding services from $ns namespace in $ctx context..."
echo "#################################################################"
# Forward services of the current namespace
sudo kubefwd services -c $KUBECONFIG -n $ns
}
#---------------------------------------------------------
# Find pod by IP
#---------------------------------------------------------
function kip(){
[[ -z "$1" ]] && echo "Need to pass pod IP as param." || (
kubectl get --all-namespaces --output json pods | jq '.items[] | select(.status.podIP=="'$1'")' | jq '.metadata.name + " --> " + .metadata.namespace'
)
}
#---------------------------------------------------------
# Kube Resource report
#---------------------------------------------------------
function kubereport() {
mkdir -p /tmp/kubereport-output
echo ""
echo "----------------------------------------------------------"
echo "Merging config files found in KUBECONFIG var..."
echo "----------------------------------------------------------"
echo $KUBECONFIG | sed 's/:/\n/g'
echo ""
echo "----------------------------------------------------------"
kubectl config view --flatten > ~/.kube/config
echo "Generating report..."
echo "----------------------------------------------------------"
docker pull hjacobs/kube-resource-report
docker run -it --net=host -v ~/.kube/:/root/.kube/ -v /tmp/output:/output hjacobs/kube-resource-report /output
echo ""
echo "----------------------------------------------------------"
echo "Opening report..."
echo "----------------------------------------------------------"
xdg-open /tmp/output/index.html &
}
#---------------------------------------------------------
# Kube Ops View
#---------------------------------------------------------
function kube-ops() {
# Merge all kubeconfig
kubectl config view --merge --flatten > ~/.kube/config
# Proxy current context
kubectl proxy &
proxy_pid=$!
# We need to mount the local ~/.kube/config file into the Docker container
docker run -it --net=host -v ${HOME}:${HOME} hjacobs/kube-ops-view --kubeconfig-path=${HOME}/.kube/config &
xdg-open http://localhost:8080 &
docker_pid=$!
# kill $docker_pid
# kill $cur_pid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment