1. EKS GPU 服务部署实战指南在云计算领域GPU加速服务已经成为AI推理、图像处理和语音转录等场景的标配。AWS EKS作为托管Kubernetes服务结合Karpenter的自动节点管理能力可以构建高弹性、低成本的GPU服务架构。本文将分享我在实际项目中部署EKS GPU推理服务的完整经验包含架构设计、核心组件配置和避坑指南。2. 架构设计与核心组件2.1 整体架构解析我们的目标架构需要满足以下核心需求自动按需创建GPU节点高效管理GPU资源生命周期安全暴露服务给终端用户实现模型数据的持久化存储架构工作流程如下用户请求通过ALB进入集群ALB将流量路由到ClusterIP ServiceService将请求分发到GPU PodKarpenter监测到Pending的GPU Pod后自动创建对应节点Pod调度到新节点开始处理请求当负载降低时自动回收闲置节点2.2 组件选型理由Karpenter vs Cluster Autoscaler选择Karpenter的主要原因更快的节点供应速度平均快40-60秒支持实例灵活性无需预先定义节点组更精细的回收策略控制原生支持EC2 Spot实例集成ALB Ingress Controller相比Nginx Ingress的优势原生集成AWS安全组和WAF支持基于路径的路由自动管理SSL证书提供详细的监控指标GPU实例选型根据我们的测试不同实例类型适用场景g5.xlarge (A10G)适合高精度FP32推理g6.xlarge (L4)性价比最高的FP16推理g4dn.xlarge (T4)兼容性最好的通用推理3. 核心配置实现3.1 Karpenter节点配置EC2NodeClass定义apiVersion: karpenter.k8s.aws/v1beta1 kind: EC2NodeClass metadata: name: gpu-node-class spec: amiFamily: Bottlerocket # 专门为容器优化的OS role: KarpenterNodeRole-GPU subnetSelectorTerms: - tags: karpenter.sh/discovery: ${CLUSTER_NAME} securityGroupSelectorTerms: - tags: karpenter.sh/discovery: ${CLUSTER_NAME} blockDeviceMappings: - deviceName: /dev/xvda ebs: volumeSize: 100Gi volumeType: gp3 encrypted: true关键参数说明Bottlerocket AMI相比Amazon Linux减少70%的启动时间gp3卷类型提供稳定的IOPS性能100Gi磁盘为模型缓存预留足够空间NodePool配置apiVersion: karpenter.sh/v1beta1 kind: NodePool metadata: name: gpu-node-pool spec: template: spec: nodeClassRef: name: gpu-node-class requirements: - key: karpenter.sh/capacity-type operator: In values: [spot, on-demand] # 优先使用Spot实例 - key: node.kubernetes.io/instance-type operator: In values: [g5.xlarge, g6.xlarge, g4dn.xlarge] - key: kubernetes.io/arch operator: In values: [amd64] taints: - key: nvidia.com/gpu effect: NoSchedule disruption: consolidationPolicy: WhenUnderutilized expireAfter: 720h # 防止频繁回收3.2 GPU工作负载部署部署示例apiVersion: apps/v1 Kind: Deployment metadata: name: gpu-inference spec: replicas: 2 selector: matchLabels: app: gpu-inference template: metadata: labels: app: gpu-inference spec: tolerations: - key: nvidia.com/gpu operator: Exists containers: - name: inference image: nvcr.io/nvidia/tritonserver:22.12-py3 resources: limits: nvidia.com/gpu: 1 volumeMounts: - name: model-storage mountPath: /models volumes: - name: model-storage persistentVolumeClaim: claimName: model-pvc重要提示必须显式声明GPU资源请求否则Pod不会被调度到GPU节点3.3 ALB Ingress配置apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: gpu-ingress annotations: alb.ingress.kubernetes.io/scheme: internet-facing alb.ingress.kubernetes.io/target-type: ip alb.ingress.kubernetes.io/listen-ports: [{HTTPS:443}] alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:... spec: ingressClassName: alb rules: - host: inference.example.com http: paths: - path: / pathType: Prefix backend: service: name: gpu-inference port: number: 80004. 模型存储方案4.1 EBS Snapshot挂载方案对于大型模型文件我们采用EBS快照创建PV的方案创建初始EBS卷并上传模型制作快照aws ec2 create-snapshot \ --volume-id vol-123456 \ --description ResNet-50 Model创建StorageClassapiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: ebs-snapshot provisioner: ebs.csi.aws.com parameters: type: gp3 encrypted: true创建PVC时指定快照apiVersion: v1 kind: PersistentVolumeClaim metadata: name: model-pvc spec: storageClassName: ebs-snapshot dataSource: name: model-snapshot kind: VolumeSnapshot apiGroup: snapshot.storage.k8s.io accessModes: - ReadOnlyMany resources: requests: storage: 100Gi这种方案的优点新节点启动时无需重复下载模型多个Pod可以共享只读卷快照跨AZ可用5. 实战问题排查5.1 GPU节点启动失败常见错误现象节点处于NotReady状态nvidia-device-plugin pod崩溃排查步骤检查kubelet日志journalctl -u kubelet -n 100 | grep -i error验证NVIDIA驱动nvidia-smi # 在节点上执行常见修复方案确保使用正确的AMI建议Bottlerocket检查节点安全组是否允许拉取容器镜像验证节点IAM角色权限5.2 Pod无法调度典型错误Pod一直处于Pending状态事件显示Insufficient nvidia.com/gpu解决方案检查NodePool配置是否包含所需实例类型验证Pod的resources.limits是否正确设置检查taint和toleration是否匹配5.3 ALB创建失败常见原因子网未正确标记安全组配置错误IAM权限不足诊断命令kubectl get ingress -o wide kubectl describe ingress gpu-ingress aws elbv2 describe-load-balancers --query LoadBalancers[?contains(LoadBalancerName,k8s)]6. 性能优化建议6.1 成本优化Spot实例策略spec: template: spec: requirements: - key: karpenter.sh/capacity-type operator: In values: [spot] - key: karpenter.sh/price-capacity-type operator: In values: [spot]实例类型优先级首先尝试g6.xlarge (L4)其次g5.xlarge (A10G)最后g4dn.xlarge (T4)6.2 自动伸缩配置通过KEDA实现基于请求量的自动伸缩apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: gpu-scaler spec: scaleTargetRef: name: gpu-inference triggers: - type: prometheus metadata: serverAddress: http://prometheus-server metricName: http_requests_total threshold: 100 query: sum(rate(http_requests_total{appgpu-inference}[1m]))6.3 监控方案推荐监控指标节点级别GPU利用率DCGM exporter显存使用量温度指标Pod级别请求延迟错误率批处理吞吐量配置示例apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: gpu-monitor spec: selector: matchLabels: app: gpu-inference endpoints: - port: metrics interval: 15s在实施这套架构后我们的GPU推理服务实现了以下改进节点供应时间从平均5分钟缩短到90秒基础设施成本降低65%通过Spot实例服务可用性达到99.95%模型更新部署时间从小时级降到分钟级最后分享一个实用技巧使用Karpenter的Consolidation策略时建议设置适当的expireAfter如24h避免频繁的节点回收影响长时运行的推理任务。同时对于关键生产负载可以创建专用的on-demand NodePool作为后备方案。