Open10

EKS Pod Identity

ishii1648ishii1648

環境作る

$ eksctl create cluster --name $CLUSTER_NAME \
        --region ap-northeast-1 \
        --version 1.31 \
        --vpc-private-subnets $SUBNET_IDS \
        --without-nodegroup
$ eksctl create nodegroup \
        --cluster $CLUSTER_NAME \
        --name test-group-2 \
        --nodes 1 \
        --managed \
        --subnet-ids $SUBNET_IDS \
        --node-private-networking \
        --spot
ishii1648ishii1648

eks-pod-identity-agent は daemonset として動作してる

❯ k get daemonsets | grep eks-pod-identity
eks-pod-identity-agent   1         1         1       1            1           <none>          23m
ishii1648ishii1648

EKS Pod Identity では k8s serice account に annotation を設定する必要がない

その代わり EKS クラスタ/IAMロール/Namespace/k8s Service Account を関連付ける必要がある

❯ a eks list-pod-identity-associations --cluster-name test-cluster
{
    "associations": [
        {
            "clusterName": "test-cluster",
            "namespace": "default",
            "serviceAccount": "my-sa",
            "associationArn": "arn:aws:eks:ap-northeast-1:787080764332:podidentityassociation/test-cluster/a-rpcft8f8fmf1uiqxf",
            "associationId": "a-xxxxxxxxxxxx"
        }
    ]
}
ishii1648ishii1648
❯ k describe sa my-sa
Name:                my-sa
Namespace:           default
Labels:              <none>
Annotations:         <none>
Image pull secrets:  <none>
Mountable secrets:   <none>
Tokens:              <none>
Events:              <none>
ishii1648ishii1648

Pod を作成すると pod-identity-webhook が以下のような設定を PodSpec に差し込む

IRSA と同じく、環境変数、volumeMount, volume を追加しているが内容は少しずつ違う。

  env:
    - name: AWS_STS_REGIONAL_ENDPOINTS
      value: regional
    - name: AWS_DEFAULT_REGION
      value: ap-northeast-1
    - name: AWS_REGION
      value: ap-northeast-1
    - name: AWS_CONTAINER_CREDENTIALS_FULL_URI
      value: http://169.254.170.23/v1/credentials
    - name: AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE
      value: /var/run/secrets/pods.eks.amazonaws.com/serviceaccount/eks-pod-identity-token
  volumeMounts:
    - mountPath: /var/run/secrets/pods.eks.amazonaws.com/serviceaccount
      name: eks-pod-identity-token
      readOnly: true
volumes:
  - name: eks-pod-identity-token
    projected:
      defaultMode: 420
      sources:
      - serviceAccountToken:
          audience: pods.eks.amazonaws.com
          expirationSeconds: 86400
          path: eks-pod-identity-token
ishii1648ishii1648

IRSA は pod-identity-webhook が annotation(eks.amazonaws.com/role-arn)が設定されている k8s service account を持つ Pod を mutate していたが、EKS Pod Identity では annotation を設定しなくても mutate が発生している

pod-identity-webhook は mutate すべき Pod をどのように判別しているのか?

ishii1648ishii1648

Container Credentials Config は Pod Identity の関連付けと連動してると思われる

関連付けされた k8s service account と namespace に一致する場合は EKS Pod Identity 用のmutate を実行する
https://github.com/aws/amazon-eks-pod-identity-webhook/blob/2a1a1131d6e9ef0060e7ce77bfe0bb9c0a09fb1e/pkg/handler/handler.go#L414-L433

ちなみに Audience: pods.eks.amazonaws.com が入る(後ほど TokenRequest API で発行される JWT トークンには aud: pods.eks.amazonaws.com が含まれるはず)
https://github.com/aws/amazon-eks-pod-identity-webhook/blob/2a1a1131d6e9ef0060e7ce77bfe0bb9c0a09fb1e/main.go#L79