Kubernetes - 测试环境资源压缩

2024-09-07
#!/bin/bash
# 当副本数大于1,强制变更为1
# 运行频率:每小时
k8s_cluster="k8s-v6"
envs="paas-env-fat bigdata"
echo `date`
for env in ${envs}
do
    /usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} -n ${env} get deployment | awk '$4>1' | awk '{print $1}' | grep -v NAME | while read line
    do
        /usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} -n ${env} scale deployment ${line} --replicas=1
    done
done
#!/bin/bash
# 当request cpu > 250m,强制变为 cpu 50m,内存128Mi
# 运行频率:每小时
k8s_cluster="k8s-v6"
namespaces="paas-env-fat bigdata platform"
echo `date`
for namespace in ${namespaces}
do
    /usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} -n ${namespace} get deployments.apps -o=custom-columns=NAME:.metadata.name,REPLICAS:.spec.replicas,CPU:.spec.template.spec.containers[*].resources.requests.cpu,MEMORY:.spec.template.spec.containers[*].resources.requests.memory --no-headers | awk '{
        if($3 !~ /m/) {
            $3 = $3 * 1000
        };
        if($4 !~ /Mi/) {
            $4 = $4 * 1024
        };
        if($4 !~ /Gi/) {
            $4 = $4 * 1
        };
        sub("m", "", $3);
        print $1 "\t" $2 "\t" $3 "\t" $4
}' | awk '$2>0 && ( ($3>150 || $3 == 0) || ( $4 > 256 || $4 == 0))'| while read line
    do
	   echo $line
	   d_name=$(echo ${line} | awk '{print $1}')
#	   if [[ ${d_name} = "down-gateway-kuaishou-singleisp" || ${d_name} = "down-sched-core" ]];then
#		   echo ${d_name} do nothing
#           else
        	   /usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} -n ${namespace} patch deployment ${d_name} -p '{"spec":{"template":{"spec":{"containers":[{"name":"'${d_name}'","resources":{"requests":{"cpu":"50m","memory":"128Mi"}}}]}}}}'
	           sleep 1
#	   fi
    done
done

标题:Kubernetes - 测试环境资源压缩
地址:https://blog.njqhome.com:8443/articles/2024/09/07/1725699960629.html