3. EKS Fargate Profile配置与Pod调度验证

EKS Fargate Profile配置与Pod调度验证 学习卡片

Fargate Profile 与 Pod 调度的匹配逻辑

Fargate Profile 是 EKS 中把 Pod 调度到 Fargate 的“准入规则”。它由三部分组成:cluster(所属集群)、podExecutionRoleArn(Fargate 节点上 kubelet 与 CNI 使用的 IAM 角色)、以及 selectors(命名空间 + 可选标签)。只有 Pod 的命名空间和标签同时命中某个 selector,才会被 Fargate 调度器接管;否则 Pod 会回到 EC2 节点组或保持 Pending。

关键约束:Fargate Profile 的 selector 只支持 namespace 和 labels,不支持 nodeSelector、affinity、tolerations 等节点级字段;Fargate Pod 也不支持 DaemonSet、HostNetwork、HostPath、privileged 等特性。因此验证调度的核心是确认 Pod 的 spec.nodeName 指向一个 fargate-* 虚拟节点,且节点标签包含 eks.amazonaws.com/compute-type=fargate。

Fargate Profile fp-app-ns cluster: eks-prod podExecutionRoleArn: arn:aws:iam::… selectors namespace: app-ns labels: tier=web 调度匹配判断 Pod 命名空间 = app-ns ? Pod 标签 tier=web ? 两者同时命中 → 交给 Fargate 调度器 调度结果 命中 → Fargate 虚拟节点 nodeName: fargate-ip-10-0-x-x… label: compute-type=fargate 未命中 → EC2 节点组 nodeName: ip-10-0-x-x… label: compute-type=ec2 规则 命中 未命中 验证命令与判读要点 kubectl get pod -n app-ns -o wide kubectl get node -l eks.amazonaws.com/compute-type=fargate kubectl describe pod web-xxx -n app-ns | grep -i node ✓ Pod STATUS=Running,nodeName 以 fargate- 开头 ✓ 节点标签 compute-type=fargate 存在 ✓ 无 EC2 节点承载该 Pod 常见未命中原因 ① 命名空间不在 selector 中 ② Pod 缺少 selector 要求的标签 ③ Pod 使用了 hostNetwork / privileged 等 Fargate 不支持字段 ④ podExecutionRole 缺少 ecr / logs 权限导致镜像拉取失败,Pod 卡在 Pending 或 ImagePullBackOff

创建 Fargate Profile

使用 eksctl 创建最直接,它会自动创建并绑定 podExecutionRole。以下命令为命名空间 app-ns 创建 Profile,并只接管带 tier=web 标签的 Pod:

eksctl create fargateprofile \
  --cluster eks-prod \
  --name fp-app-ns \
  --namespace app-ns \
  --labels tier=web \
  --region ap-southeast-1

若使用 AWS CLI,需要先准备信任策略并创建角色,再创建 Profile:

# 1. 创建 podExecutionRole 信任策略
cat > fargate-trust.json <<'EOF'
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": { "Service": "eks-fargate-pods.amazonaws.com" },
    "Action": "sts:AssumeRole"
  }]
}
EOF

aws iam create-role \
  --role-name eks-fargate-pod-role \
  --assume-role-policy-document file://fargate-trust.json

aws iam attach-role-policy \
  --role-name eks-fargate-pod-role \
  --policy-arn arn:aws:iam::aws:policy/AmazonEKSFargatePodExecutionRolePolicy

# 2. 创建 Fargate Profile
aws eks create-fargate-profile \
  --cluster-name eks-prod \
  --fargate-profile-name fp-app-ns \
  --pod-execution-role-arn arn:aws:iam::123456789012:role/eks-fargate-pod-role \
  --selectors namespace=app-ns,labels={tier=web} \
  --region ap-southeast-1
Profile 创建是异步的,状态从 CREATING 变为 ACTIVE 后才可调度。可用 aws eks describe-fargate-profile 或 eksctl get fargateprofile 查看状态。

部署无节点 Pod 并验证

创建命名空间并部署一个带 tier=web 标签的 Deployment。注意:Fargate 不支持 DaemonSet,因此这里用 Deployment 验证:

kubectl create namespace app-ns

cat <<'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: app-ns
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
      tier: web
  template:
    metadata:
      labels:
        app: web
        tier: web
    spec:
      containers:
      - name: nginx
        image: public.ecr.aws/nginx/nginx:1.25
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: "250m"
            memory: "512Mi"
EOF

等待 Pod 就绪后,用以下命令确认调度位置:

kubectl get pod -n app-ns -o wide
kubectl get node -l eks.amazonaws.com/compute-type=fargate
kubectl describe pod -n app-ns -l tier=web | grep -E "Node:|Node-Selectors"

预期结果:Pod 的 NODE 列显示 fargate-ip-10-0-x-x.ap-southeast-1.compute.internal,且 kubectl get node 能看到对应的 Fargate 虚拟节点。若 Pod 落在 EC2 节点上,说明 selector 未命中,需检查命名空间和标签是否与 Profile 完全一致。

快速复查清单

检查项命令 / 判据通过标准
Profile 状态aws eks describe-fargate-profile --query 'fargateProfile.status'ACTIVE
Selector 匹配对比 Profile selectors 与 Pod namespace/labels命名空间与标签均命中
Pod 调度节点kubectl get pod -n app-ns -o wideNODE 以 fargate- 开头
节点标签kubectl get node -l eks.amazonaws.com/compute-type=fargate存在对应虚拟节点
Pod 状态kubectl get pod -n app-nsRunning 且 READY 1/1
Fargate Pod 的镜像拉取依赖 podExecutionRole 的 ECR 权限;若使用私有 ECR,需额外附加 AmazonEC2ContainerRegistryReadOnly。日志需通过 Fluent Bit 转发到 CloudWatch,Fargate 节点本身不保留日志。