首页 > 其他分享 >【K8s】scale缩容时删除指定pod

【K8s】scale缩容时删除指定pod

时间:2024-03-26 15:22:23浏览次数:21  
标签:scale err 缩容时 io pod pods comes before

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!")
}

标签:scale,err,缩容时,io,pod,pods,comes,before
From: https://www.cnblogs.com/ayf-devops/p/18096732

相关文章

  • Kubernetes之Pod调度策略
    一、NodeSelector:定向调度1.基本原理在Kubernetes上部署应用时,有时候可能需要限制Pod的调度范围,将Pod调度到指定的一些Node上。此时,可以为需要调度的这些Node打上标签,同时设置Pod的nodeSelector属性,使二者相匹配来达到定向调度的目的。2.简单实践(1)为目标......
  • Podman能够替代Docker吗
    导读:参考:ExploringPodman:AMoreSecureDockerAlternative作者:MarinBezhanov网址:https://betterstack.com/community/guides/scaling-docker/podman-vs-docker/该随笔为文章部分摘要和学习笔记架构区别Docker属于CS架构(client-server),Podman利用了无守护架构(daemonless......
  • Kubernetes之Pod工作负载
    Pod工作负载,亦称Pod控制器。在Kubernetes平台上,我们很少会直接创建一个Pod,因为直接管理单个Pod的工作量将会非常繁琐。我们可以使用KubernetesAPI创建工作负载对象,这些对象所表达的是比Pod更高级别的抽象概念,Kubernetes 控制平面根据我们定义的工作负载对象规......
  • 【机器学习】详细解析Sklearn中的StandardScaler---原理、应用、源码与注意事项
    【机器学习】详细解析Sklearn中的StandardScaler—原理、应用、源码与注意事项......
  • Kubernetes之Pod基本原理与实践
    一、Pod的定义与基本用法1.Pod是什么Pod是可以在Kubernetes中创建和管理的、最小的可部署的计算单元。Pod不是进程,而是容器运行的环境。Pod所建模的是特定于应用的“逻辑主机”,其中包含一个或多个应用容器。当Pod包含多个应用容器时,这些容器的应用之间应该是......
  • kubeshark查看k8s中pod的流量
    kubeshark的介绍在底层实现当中,Kubeshark主要使用到了Linux内核中的各种内置方法和API,隐藏了对流量数据的加解密实现,可以直接收集到K8s集群中的加密和未加密流量。对网络数据的收集主要使用了直接抓包法和基于拓展伯克利包过滤(eBPF)的数据包获取。直接抓包法涉及libpcap、AF_PACKE......
  • CentOS 7 安装 DNF 包管理工具和 Podman
    安装软件包:bashdnfinstall<package_name>用于安装指定的软件包。更新软件包:bashdnfupdate用于更新系统中已安装的所有软件包到最新版本。搜索软件包:bashdnfsearch<keyword>用于搜索具有指定关键字的软件包。移除软件包:bashdnfremove<package_name>......
  • RHCE(podman容器)
    一:容器的基础1:为什么会出现容器? 开发和运维的矛盾:就是开发人员有个环境来专门进行开发使用的,运维人员的服务器上面没有开发的环境,因为,不是开发人员,如果添加了,服务器的占用的内存就大了起来,所以就出现了一个问题,当开发的东西放到运维的上面时,因为没有环境,这个就部署不上去,就会报......
  • P9077 [PA2018] Poddrzewo 题解
    思考感觉题目有点迷惑的意思,要最小化操作\(1\)使用的次数,也就是要节约修改操作,让我们认为操作\(1\)是最有用的,其实只要稍微动动脑子想一想,删除操作才是最有用的,而交换操作根本没用。当将序列删除到只剩两个点时,就把两个点连上,度都为\(1\)。所以如果序列中\(1\)的数量超......
  • CocoaPod 如何创建私有库
    一github新建仓库点击Newrepository,然后配置仓库属性   Createanewrepository       创建完成自己的github远程项目。打开终端,cd~/.cocoapods/repos目录下执行podrepoaddNAME(自定义项目名称,可以和远端不一致)https://自己生成的token@gith......