069-k8s 集群基于节点内存水位线通过打污点方式来干预Pod调度

511 阅读1分钟

这是坚持技术写作计划(含翻译)的第69篇,定个小目标999,每周最少2篇。


本文主要介绍如何最小化改造的情况下,根据中小型k8s集群的node节点的内存,通过打污点(taints)的形式,柔性干预Pod调度


简单介绍


其实关于k8s调度方面的资料很多

比如 k8s 的 Node Allocatable,对我来说,有点硬了,可能导致短暂的高峰把pod给驱逐了

比如大佬 智博老师 提到的 github.com/kubernetes-…github.com/kubernetes-… 管用是管用,但是有点复杂了。
image.png

可用脚本


花了几分钟,写了个bash脚本,脚本放到了我的gist 上,防止过国内没法访问,下面也粘贴一份

#!/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

核心部分就最后五六行,用法也很简单

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 : ./a.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"
node/192.168.1.139 modified
node/192.168.1.137 modified
node/192.168.1.126 modified
node/192.168.1.140 untainted
node/192.168.1.141 untainted
node/192.168.1.142 untainted
error: taint "mem:NoSchedule" not found
error: taint "mem:NoSchedule" not found


招聘小广告


山东济南的小伙伴欢迎投简历啊 加入我们 , 一起搞事情。
长期招聘,Java程序员,大数据工程师,运维工程师,前端工程师。

参考资料