PodDeletionCost是k8s1.22后默认开启的新特性,以annotation的方式作用于Pod,表示这个pod的“删除代价”,代价越小的pod删除优先级相对越高。因此在scale前给需要删除的pod加上annotation即可。
// ActivePodsWithRanks is a sortable list of pods and a list of corresponding
// ranks which will be considered during sorting. The two lists must have equal
// length. After sorting, the pods will be ordered as follows, applying each
// rule in turn until one matches:
//
// 1. If only one of the pods is assigned to a node, the pod that is not
// assigned comes before the pod that is.
// 2. If the pods' phases differ, a pending pod comes before a pod whose phase
// is unknown, and a pod whose phase is unknown comes before a running pod.
// 3. If exactly one of the pods is ready, the pod that is not ready comes
// before the ready pod.
// 4. If controller.kubernetes.io/pod-deletion-cost annotation is set, then
// the pod with the lower value will come first.
// 5. If the pods' ranks differ, the pod with greater rank comes before the pod
// with lower rank.
// 6. If both pods are ready but have not been ready for the same amount of
// time, the pod that has been ready for a shorter amount of time comes
// before the pod that has been ready for longer.
// 7. If one pod has a container that has restarted more than any container in
// the other pod, the pod with the container with more restarts comes
// before the other pod.
// 8. If the pods' creation times differ, the pod that was created more recently
// comes before the older pod.
//
// In 6 and 8, times are compared in a logarithmic scale. This allows a level
// of randomness among equivalent Pods when sorting. If two pods have the same
// logarithmic rank, they are sorted by UUID to provide a pseudorandom order.
//
// If none of these rules matches, the second pod comes before the first pod.
//
// The intention of this ordering is to put pods that should be preferred for
// deletion first in the list.
点击查看代码
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
// 1. 加载 kubeconfig 文件
config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
if err!= nil {
fmt.Println(err)
os.Exit(1)
}
// 2. 创建 Kubernetes 客户端
clientset, err := kubernetes.NewForConfig(config)
if err!= nil {
fmt.Println(err)
os.Exit(1)
}
// 3. 获取 Pod 对象
pod, err := clientset.CoreV1().Pods("ai").Get(context.TODO(), "ai-tts-g2-grpc-55b59f567d-nf65x", metav1.GetOptions{})
if err!= nil {
fmt.Println(err)
os.Exit(1)
}
// 4. 添加注解
annotationKey := "controller.kubernetes.io/pod-deletion-cost"
annotationValue := "-100"
if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}
pod.Annotations[annotationKey] = annotationValue
// 5. 更新 Pod 对象
_, err = clientset.CoreV1().Pods("ai").Update(context.TODO(), pod, metav1.UpdateOptions{})
if err!= nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Annotation added successfully!")
}