shell - k8s平滑移除node
2021-12-04
#!/bin/bash
kubectl top node | grep -v NAME | sort -nk 5
echo -n "请输入node节点名"
read NODE
node_ip=$(kubectl get node -o wide | grep ${NODE} | awk '{print $6}')
kubectl cordon ${NODE}
namespaces=$(kubectl get namespaces | awk '{print $1}' )
stime=$(date "+%s")
for namespace in ${namespaces}
do
kubectl -n ${namespace} get pods -o wide | grep ${NODE} | awk '$6!="'${node_ip}'"' | grep Running | awk '{print $1}' | while read line
do
last_one_split_name=$(echo ${line} | awk -F '-' '{print $NF}')
last_two_split_name=$(echo ${line} | awk -F '-' '{print $(NF-1)}')
if [[ $(echo ${last_one_split_name} | wc -c) -gt 2 ]];then
deployment_name=$(echo ${line} | sed "s/-${last_two_split_name}-${last_one_split_name}//g")
echo kubectl -n ${namespace} patch deployment ${deployment_name} -p '{"spec":{"template":{"metadata":{"annotations": {"sTime":'\"${stime}\"'} } }}}'
else
statefulsets_name=$(echo ${line} | sed "s/-${last_one_split_name}//g")
echo kubectl -n ${namespace} patch statefulsets ${statefulsets_name} -p '{"spec":{"template":{"metadata":{"annotations": {"sTime":'\"${stime}\"'} } }}}'
fi
done
done