Last active
February 3, 2022 11:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
useage/用法