AWS ECS Fargate 上的 JVM Runtime.getRuntime().availableProcessors() returns 1

JVM Runtime.getRuntime().availableProcessors() returns 1 on AWS ECS Fargate

public class Example {
   public static void main(String[] args) {
      // print statement at the start of the program
      System.out.println("Start...");
      System.out.print("Number of available processors are: ");
      System.out.println( Runtime.getRuntime().availableProcessors());
   }
}

参考:https://www.tutorialspoint.com/get-number-of-available-processors-in-java

当我编译并 运行 AWS ECS Fargate 上的这段代码得到“1”作为输出时,即使我在任务 def(4098 for 4 vCPU).

已测试 Java 版本:

但是当我在 docker 内的 AWS EC2 上做同样的事情时,通过为“docker 运行”分配 CPU 和 --cpus 我获取我传递给 --cpus 的确切数字。 AWS EC2 与 Containerd 的结果相同。

还在 EKS 1.18 中通过设置 CPU 限制对此进行了测试。它 returns CPU 限制中指定的 CPU 数量。

所以,想知道为什么“1”仅在 AWS ECS 中。

我假设您使用控制台创建了任务定义,因为我看到了相同的行为(并且没有预料到)。 The documentation 是这样说的:

Although you can also specify CPU and memory at the container level for Fargate tasks, this is optional. Most use cases are satisfied by only specifying these resources at the task level

我首先创建了一个在任务级别指定 CPU/memory 的任务定义。当我 运行 它时,我看到了与你相同的输出,这就是任务定义的样子(无关信息已省略):

    "taskDefinition": {
        "containerDefinitions": [
            {
                "name": "example",
                "image": ".../example:latest",
                "cpu": 0,
            }
        ],
        "cpu": "4096",
        "memory": "8192",
    }

注意容器CPU是0.

然后我编辑了 容器 定义中的 CPU,并在 运行 任务时看到了可用处理器的预期数量。当我检索任务定义时,它看起来像这样:

    "taskDefinition": {
        "containerDefinitions": [
            {
                "name": "example",
                "image": ".../example:latest",
                "cpu": 4096,
            }
        ],
        "cpu": "4096",
        "memory": "8192",
    }

我没有尝试使用 CloudFormation 或 Terraform 创建任务定义以查看它是否有任何不同。我在 ECS 控制台上发送反馈。