Skip to content

Instantly share code, notes, and snippets.

@anjia0532
Last active February 3, 2022 11:10
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 anjia0532/71e5a706ea01b79f98d8b1e27e9459a7 to your computer and use it in GitHub Desktop.
Save anjia0532/71e5a706ea01b79f98d8b1e27e9459a7 to your computer and use it in GitHub Desktop.
k8s 集群基于节点内存水位线通过打污点方式来干预Pod调度/k8s cluster base on node memory water level add taint to node
#!/usr/bin/env bash
# author anjia0532@gmail.com
# blog https://anjia0532.github.io/
# github https://github.com/anjia0532
RED='\033[0;31m'
NC='\033[0m' # No Color
usage () {
echo -e "${RED} Auto add/remove mem=poor:NoSchedule taint to your nodes of k8s cluster by threshold value(Low water level/High water level)
Usage : $0 {OPTIONS}
-k < kubectl command e.g. -k sudo kubectl --kubeconfig /path/to/config.yaml>
-l < Low water level range (0,100) e.g. -l 50 >
-h < high water level range (0,100) e.g. -h 80>
-f < Selector (label query) to filter on, supports '=', '==', and '!='. e.g. -f key1=value1,key2=value2 > ${NC}";
}
# parse args
while getopts "k:l:h:f:" opts; do
case ${opts} in
k) KUBECTL_CMD=${OPTARG} ;;
l) LOW=${OPTARG} ;;
h) HIGH=${OPTARG} ;;
f) FILTER=${OPTARG} ;;
*) usage; exit;;
esac
done
# those args must be not null
if [ ! "$KUBECTL_CMD" ] || [ ! "$LOW" ] || [ ! "$HIGH" ]
then
usage
exit 1
fi
# low and hign must between 0 and 100
if [ "$LOW" -ge 100 ] || [ "$HIGH" -ge 100 ] || [ "$LOW" -le 0 ] || [ "$HIGH" -le 0 ]
then
usage
exit 1
fi
echo -e "View top node(order by memory) by this command \n
${RED} ${KUBECTL_CMD} top node --selector=\"${FILTER}\" --use-protocol-buffers --sort-by='memory' \n ${NC}"
nodes=()
while IFS='' read -r line; do nodes+=("$line"); done < <(${KUBECTL_CMD} top node --selector="${FILTER}" --no-headers --use-protocol-buffers --sort-by='memory' | awk '{print $1","$5}'| sed "s/%//g")
for node in "${nodes[@]}" ; do
IFS="," read -r node_name mem_useage <<< "${node}"
[[ $mem_useage -ge ${HIGH} ]] && ( echo -e "\n${KUBECTL_CMD} taint nodes ${node_name} mem=poor:NoSchedule --overwrite" && ${KUBECTL_CMD} taint nodes ${node_name} mem=poor:NoSchedule --overwrite)
[[ $mem_useage -le ${LOW} ]] && ( echo -e "\n${KUBECTL_CMD} taint nodes ${node_name} mem=poor:NoSchedule-" && ${KUBECTL_CMD} taint nodes ${node_name} mem=poor:NoSchedule- )
done
@anjia0532
Copy link
Author

anjia0532 commented Jan 28, 2022

useage/用法

k8s_node_flexible_memory_scheduling.sh 
 Auto add/remove mem=poor:NoSchedule taint to your nodes of k8s cluster by threshold value(Low water level/High water level)
    Usage : ./k8s_node_flexible_memory_scheduling.sh {OPTIONS}
        -k < kubectl command e.g. -k sudo kubectl --kubeconfig /path/to/config.yaml>
        -l < Low water level range (0,100) e.g. -l 50 >
        -h < high water level range (0,100) e.g. -h 80>
        -f < Selector (label query) to filter on, supports '=', '==', and '!='. e.g. -f key1=value1,key2=value2 >
k8s_node_flexible_memory_scheduling.sh -k "kubectl" -l 60 -h 80 -f "biz=demo"
View top node(order by memory) by this command 

 /var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml top node --selector="biz!=log,biz!=mall-search" --use-protocol-buffers --sort-by='memory' 
 


/var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml taint nodes 192.168.1.139 mem=poor:NoSchedule-
error: taint "mem:NoSchedule" not found

/var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml taint nodes 192.168.1.130 mem=poor:NoSchedule-
error: taint "mem:NoSchedule" not found

/var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml taint nodes 192.168.1.126 mem=poor:NoSchedule-
error: taint "mem:NoSchedule" not found

/var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml taint nodes 192.168.1.140 mem=poor:NoSchedule-
error: taint "mem:NoSchedule" not found

/var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml taint nodes 192.168.1.129 mem=poor:NoSchedule-
error: taint "mem:NoSchedule" not found

/var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml taint nodes 192.168.1.138 mem=poor:NoSchedule-
error: taint "mem:NoSchedule" not found

/var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml taint nodes 192.168.1.132 mem=poor:NoSchedule-
error: taint "mem:NoSchedule" not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment