实验kubernetes的CPU绑定策略

embedded/2024/10/15 15:55:50/

CPU 管理配置

CPU 管理策略通过 kubelet 参数 --cpu-manager-policy 或 KubeletConfiguration 中的 cpuManagerPolicy 字段来指定。 支持两种策略:

  • none:默认策略。
  • static:允许为节点上具有某些资源特征的 Pod 赋予增强的 CPU 亲和性和独占性。

更改 CPU 管理器策略

以minikube为例,修改kubelet配置:

1、添加配置:

kubectl edit configmaps kubelet-config -n kube-system

    cpuManagerPolicy: static
    kubeReserved: {cpu: 100m, memory: 100Mi}

2、在节点机器删除 rm /var/lib/kubelet/cpu_manager_state

3、重启kubelet。

4、观察是否启动成功:

systemctl status kubelet

journalctl -f

5、查看 /var/lib/kubelet/cpu_manager_state 是否变化,如:

cat /var/lib/kubelet/cpu_manager_state
{"policyName":"static","defaultCpuSet":"0-3","checksum":611748604}

设置pod绑定cpu核数

1、绑定cpu的pod配置(Guaranteed Pod):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 100%
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: docker.io/nginx:1.27.0
          imagePullPolicy: Never
          resources:
            requests:
              cpu: 1
              memory: "50Mi"
            limits:
              cpu: 1
              memory: "50Mi"
          ports:
            - name: http
              containerPort: 80
      terminationGracePeriodSeconds: 6

2、没有绑定cpu的pod配置(非Guaranteed Pod):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 100%
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
        - name: myhello
          image: docker.io/library/myhello:v1.4
          imagePullPolicy: Never
          ports:
            - name: http
              containerPort: 8000
      terminationGracePeriodSeconds: 6

3、先后部署hello和nginx:

kubectl apply -f hello-deployment-rs2.yaml

kubectl apply -f nginx-deployment-rs2.yaml

观察cpu绑定效果

1、只部署hello后的绑定情况:

docker ps | grep hello

docker inspect fa0c12f34ae5 | grep -i pid

taskset -pc 94245

pid 94245's current affinity list: 0-3

2、然后继续部署nginx容器的绑定情况:

docker ps | grep nginx
docker inspect 0d42e09a0f56 | grep -i pid
taskset -pc 59820
pid 59820's current affinity list: 1

2、查看hello容器的cpu绑定情况:

taskset -pc 94245
pid 94245's current affinity list: 0,2,3

3、查看kubelet的cpu_manager_state:

cat /var/lib/kubelet/cpu_manager_state

{"policyName":"static","defaultCpuSet":"0,2-3","entries":{"9268f4b8-bf6c-4910-9f3a-a9ac879ffc60":{"nginx":"1"}},"checksum":1987962493}

4、删除nginx容器

kubectl delete -f nginx-deployment-rs2.yaml

5、再次查看kubelet的cpu_manager_state:

cat /var/lib/kubelet/cpu_manager_state

{"policyName":"static","defaultCpuSet":"0,2-3","entries":{"9268f4b8-bf6c-4910-9f3a-a9ac879ffc60":{"nginx":"1"}},"checksum":1987962493}

6、再次查看hello的cpu绑定状态:

taskset -pc 94245
pid 94245's current affinity list: 0,2,3

hello使用的cpu并没有变化,依然只能使用3个核。

7、删除hello并再次部署hello:

kubectl delete -f hello-deployment-rs2.yaml

kubectl apply -f hello-deployment-rs2.yaml

8、观察hello的cpu绑定情况:

#hello:

taskset -pc 91704
pid 91704's current affinity list: 0-3

hello恢复调度所有cpu。

9、再次查看kubelet的cpu_manager_state:

cat /var/lib/kubelet/cpu_manager_state
{"policyName":"static","defaultCpuSet":"0-3","checksum":611748604}

实验结论:

1、虽然设置了static调度模式,但是只要不部署独占cpu的pod即Guaranteed Pod,其他pod都可以在所有cpu上调度。

2、Guaranteed Pod部署后,非Guaranteed Pod会动态腾出cpu核心供Guaranteed Pod独占。

3、节点内的cpu已存在被Guaranteed Pod绑定核心后,只有在独占cpu核心的Guaranteed Pod全部退出后,然后其他pod重新部署后才能恢复其他pod在所有cpu上的调度。

--end--


http://www.ppmy.cn/embedded/127942.html

相关文章

如何有效进行主机加固?深信达MCK提供答案

在数字化时代,企业面临的网络安全威胁日益严峻,尤其是勒索病毒等恶意软件的攻击,给企业带来了巨大的挑战。为了有效应对这些威胁,企业需要采取全面的网络安全防护措施,其中主机加固成为了关键的一环。深信达的MCK主机加…

排队模型和贪心算法,贪心算法在算力共享排队系统中的应用

目录 排队模型和贪心算法 一、排队模型概述 二、贪心算法简介 三、排队模型与贪心算法的关系 四、实例说明 贪心算法在算力共享排队系统中的应用 贪心算法的局限性 排队模型和贪心算法 之间存在密切的关系,主要体现在排队问题的求解过程中。 一、排队模型概述 排队模…

How to list the environment variables in MySQL based on podman

有时候,我们期望系统的、完整的输出mysql中的环境变量,但是只是想看看,不想安装mysql,有没有什么好的办法呢? 其实,答案是有的。我们可以基于docker/podman来完成,这里推荐podman,示…

vue中关于router.beforeEach()的用法

router.beforeEach()是Vue.js中的路由守卫,用于在路由跳转前进行校验、取消、重定向等操作。 基本使用: const router new VueRouter({ ... })router.beforeEach((to, from, next) > {// ... }) to: 即将要进入的目标路由对象 from: 当前导航正要…

如何捕捉行情爆发的前兆

在金融市场的激烈角逐中,每一次行情的爆发都是投资者获取丰厚回报的关键时刻。然而,如何识别并把握这些时刻,却是一门需要深厚金融专业知识和敏锐洞察力的艺术。今天,我们就来深入探讨行情爆发的初期信号,揭示那些能够…

常用的设计模式,以及c++代码简答示例

常用的设计模式及其 C 简短示例: 1. 单例模式(Singleton Pattern) 作用:确保一个类只有一个实例,并提供全局访问点。 示例代码: class Singleton { private:static Singleton* instance;Singleton() {} …

MFC的.rc 和.rc2文件【常见问题】

目录 一、介绍 .rc 文件 .rc2 文件 使用上的建议 二、实例 工程截图 rc文件 rc2文件 注意: 三、使用 能否删除? 一、介绍 在MFC项目中,.rc和.rc2文件都是资源脚本文件,但它们有不同的用途和管理方式。它们两指向的是同…

重学SpringBoot3-集成Redis(九)之共享Session

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(九)之共享Session 1. 为什么需要 Session 共享2. Spring Session 和 Redis 的集成2.1. 引入依赖2.2. 配置 Redis 连接…