You need to enable JavaScript to run this app. AKS中ASP.NET 8 CPU密集型进程利用率未达预期求助
优惠活动
大模型
产品
解决方案
定价
更多

AKS中ASP.NET 8 CPU密集型进程利用率未达预期求助

问题概述

我有一个基于ASP.NET 8的CPU密集型F#进程,负责解析大量数据并执行计算,工作流高度可并行,因此采用异步计算和无限制并行度的Async.Parallel来最大化性能:

  • 测试环境:16核64G内存的Windows 10笔记本,进程运行时16核利用率均达100%。
  • 生产环境:16核64G内存的AKS Linux节点,基于mcr.microsoft.com/dotnet/aspnet:8.0构建Docker镜像,Pod未设置资源限制,但节点CPU利用率极少超过20%,kubectl top pods显示Pod仅使用2693m CPU(预期为16核即16000m)。

需要提升CPU利用率以缩短执行时间,怀疑Pod配置有误。

更新:Pod描述输出

Name:             yatp-maintenance-train-study-8mvwd
Namespace:        yatp
Priority:         0
Service Account:  default
Node:             aks-studypool-35366450-vmss000000/10.224.0.8
Start Time:       Thu, 05 Oct 2023 10:47:17 +1100
Labels:           controller-uid=d6ea910b-67fb-43fb-b168-a8ccfa4c920c
                  job-name=yatp-maintenance-train-study
Annotations:      <none>
Status:           Running
IP:               10.244.3.6
IPs:
  IP:           10.244.3.6
Controlled By:  Job/yatp-maintenance-train-study
Containers:
  yatp-maintenance-train-study:
    Container ID:  containerd://95cea040177e3b1593e6e593f979c448af783d678032a78e742aff500e5f452c
    Image:         yapdemocontainerregistry.azurecr.io/yatp-maintenance:0.0.5
    Image ID:      yapdemocontainerregistry.azurecr.io/yatp-maintenance@sha256:8a8c2f02c8106417ffd0d025a2831b142294237b9764d30a34bfa6a9648a1872
    Port:          8080/TCP
    Host Port:     0/TCP
    Args:
      train-study
      --product
      GCA@650
      --def
      Pmap@Pete_0.0.4
    State:          Running
      Started:      Thu, 05 Oct 2023 10:47:18 +1100
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:     80
      memory:  350Gi
    Environment:
      ASPNETCORE_URLS:                       http://+:8080
      YATP_ENVIRONMENT:                      AKS
      YATP_AZURE_STORAGE_CONNECTION_STRING:  DefaultEndpointsProtocol=https;AccountName=yatpdemo1storageaccount;AccountKey=XzsajB+bBSls5+017BCMgLkLhVCwpVlksRi9P7iJTFDlG4PlrOuHN3yDtXfv5q+FfImoH+FinWlj+AStCaC35A==;EndpointSuffix=core.windows.net
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-jhmkk (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-jhmkk:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              scope=study
Tolerations:                 kubernetes.azure.com/scalesetpriority=spot:NoSchedule
                             node.kubernetes.io/memory-pressure:NoSchedule op=Exists
                             node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
                             yatp=yatp:NoSchedule
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  24m   default-scheduler  Successfully assigned yatp/yatp-maintenance-train-study-8mvwd to aks-studypool-35366450-vmss000000
  Normal  Pulling    24m   kubelet            Pulling image "yapdemocontainerregistry.azurecr.io/yatp-maintenance:0.0.5"
  Normal  Pulled     24m   kubelet            Successfully pulled image "yapdemocontainerregistry.azurecr.io/yatp-maintenance:0.0.5" in 956.43429ms (956.443792ms including waiting)
  Normal  Created    24m   kubelet            Created container yatp-maintenance-train-study
  Normal  Started    24m   kubelet            Started container yatp-maintenance-train-study

问题分析与解决方案

1. 修正Pod CPU与内存请求配置

从Pod描述看,你设置的cpu: 80请求值完全不合理——节点仅16核,请求80核会导致调度器无法分配匹配资源,kubelet会强制限制Pod的CPU使用。同时原memory: 350Gi请求远超节点64G内存,会引发资源调度异常或OOM风险。

  • 调整资源配置为节点实际可用范围:
    resources:
      requests:
        cpu: "15" # 留1核给节点系统进程
        memory: "32Gi"
      limits:
        cpu: "16" # 允许Pod使用全部节点CPU
        memory: "60Gi"
    

2. 优化.NET线程池配置

Linux环境下.NET线程池默认配置可能无法充分利用多核,Async.Parallel依赖线程池执行并行任务,需在程序启动时显式设置最小工作线程数:

open System.Threading

let coreCount = Environment.ProcessorCount
// CPU密集型任务可设置为核心数的2倍,确保线程池有足够执行资源
ThreadPool.SetMinThreads(coreCount * 2, coreCount * 2)

3. 验证容器实际CPU配额

进入Pod执行以下命令,确认容器可用CPU配额:

kubectl exec -it yatp-maintenance-train-study-8mvwd -- cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us
kubectl exec -it yatp-maintenance-train-study-8mvwd -- cat /sys/fs/cgroup/cpu/cpu.cfs_period_us

可用CPU核数 = cpu.cfs_quota_us / cpu.cfs_period_us,若结果远小于16,说明kubelet仍在限制CPU,需重新检查Pod资源配置。

4. 排查节点资源占用

执行kubectl top nodes查看节点上其他Pod的CPU占用情况,确认是否有其他进程抢占资源。若存在高负载Pod,需调整调度策略或清理闲置资源。

5. 确认Docker镜像运行时配置

确保镜像使用的mcr.microsoft.com/dotnet/aspnet:8.0针对Linux优化,可在Dockerfile中添加以下环境变量:

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true
ENV DOTNET_RUNNING_IN_CONTAINER=true

内容的提问来源于stack exchange,提问作者Franco Tiveron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:15:04