Kubernetes CPU 多线程

Kubernetes CPU multithreading

我有一个 4 核 CPU,我创建了一个 CPU 资源限制为 100m 的 Kubernetes Pod,这意味着它将占用一个核心功率的 1/10。

我想在这种情况下,100m甚至不是一个完整的核心,如果我的应用程序是多线程应用程序,我的应用程序的线程会并行运行吗?还是所有线程都只在核心部分(100毫核心)运行?

谁能进一步解释背后的机制?


目前我找到的最接近的答案是这个:

For a single-threaded program, a cpu usage of 0.1 means that if you
could freeze the machine at a random moment in time, and look at what
each core is doing, there is a 1 in 10 chance that your single thread
is running at that instant. The number of cores on the machine does
not affect the meaning of 0.1. For a container with multiple threads,
the container's usage is the sum of its thread's usage (per previous
definition.) There is no guarantee about which core you run on, and
you might run on a different core at different points in your
container's lifetime. A cpu limit of 0.1 means that your usage is not
allowed to exceed 0.1 for a significant period of time. A cpu request
of 0.1 means that the system will try to ensure that you are able to
have a cpu usage of at least 0.1, if your thread is not blocking
often.

我认为以上听起来很合乎逻辑。根据我的问题,100m 的 CPU 核心能力将分布在所有 CPU 核心上,这意味着多线程应该在 Kubernetes 中工作。

更新:

此外,这个答案很好地解释了,虽然它可能在单核中运行线程(或者根据问题少于一个核心功率),但由于操作系统的调度能力,它仍然会尝试运行指令单元并联,但不超过指定的时钟功率(根据问题为 100m)。


查看与 Kubernetes 中的资源相关的文档:

您可以使用文章中描述的资源:

To specify a CPU request for a Container, include the
resources:requests field in the Container resource manifest. To
specify a CPU limit, include resources:limits.

In this exercise, you create a Pod that has one Container. The
Container has a request of 0.5 CPU and a limit of 1 CPU. Here is the
configuration file for the Pod:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Pod
metadata:
  name: cpu-demo
  namespace: cpu-example
spec:
  containers:
  - name: cpu-demo-ctr
    image: vish/stress
    resources:
      limits:
        cpu:"1"
      requests:
        cpu:"0.5"
    args:
    - -cpus
    -"2"

对您的问题的补充:
是的,它不会并行运行(多核线程)。但是您可以在 pod 中为您的应用程序显示几个核心,然后使用多线程来执行它。

The args section of the configuration file provides arguments for
the Container when it starts. The -cpus"2" argument tells the
Container to attempt to use 2 CPUs.


我仔细查看了有问题的 GitHub 问题线程。线程中有一些来回,但我想我理解了它,并想分享到目前为止答案中似乎缺少的一些东西:

  • 100m不等于核心功率的1/10。它是 CPU 时间的绝对量,无论节点中的内核数量如何,都将保持不变。
  • 虽然 CPU 时间可能很好地分配给节点的多个内核,但真正的并行性仍然取决于 CPU 限制远高于单个线程所需的 CPU。否则,您的多线程应用程序将同时运行(即线程在同一个 CPU 内核上轮流运行)而不是并行运行(即线程在不同的内核上运行)。