2
2

More than 1 year has passed since last update.

備忘録:kubernetes/EKS基礎③(認証関連)

Last updated at Posted at 2022-12-22

備忘録

下記認証の際にk8sの仕組みとAWS IAMの仕組みを使用するため複雑
仕組みと各マニフェスト間の設定の紐付けをまとめておく

  • kubectlでEKSに接続する時
  • IAM Roles for Service Accounts(IRSA)

kubectlでの認証

概要

aws eks update-kubeconfig --name eks-clu
kubectl get node
NAME                                                STATUS   ROLES    AGE   VERSION
ip-192-168-92-154.ap-northeast-1.compute.internal   Ready    <none>   25m   v1.23.13-eks-fb123a0

~/.kube/configが作成される。clustersにクラスタ情報、usersにユーザー情報、 contextsにクラスターとユーザーの関連付けが記載される。

~/.kube/config
users: # users以下を記載
- name: arn:aws:eks:ap-northeast-1:xxxxxxxxxxx:cluster/eks-clu
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      args:
      - --region
      - ap-northeast-1
      - eks
      - get-token
      - --cluster-name
      - eks-clu
      command: aws

aws --region ap-northeast-1 eks get-token --cluster-name eks-cluでトークンを取得してEKSのコントロールプレーンに送信。EKS側でIAMを参照して登録されているユーザ情報を確認。

eksctlでクラスタを作成したユーザはデフォルトで管理者権限がある。
下記からeks-opeという新しいユーザを作成して検証する。

aws configure # eks-opeユーザの情報入力
kubectl get node
error: You must be logged in to the server (Unauthorized)

仕組み

下記のようにaws-auth,RoleBinding,Roleの3つのリソース作成が必要。
Roleで許可する操作を定義して、RoleBindingでk8sのグループを作成してRoleに紐づける。aws-authでIAMユーザとk8sのグループを紐づける。

スクリーンショット 2022-12-22 8.02.44.png

手順

クラスタを作成した権限のユーザで下記マニフェストを作成して実行

ClusterRole.yml
clusterrolebinding.rbac.authorization.k8s.io/test-clu-rolebinding created
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: test-clu-role
rules:
- apiGroups: [""]
  resources: ["pods","nodes"]
  verbs: ["get", "watch", "list"]
ClusterRoleBinding.yml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: test-clu-rolebinding
subjects:
- kind: Group
  name: operators
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: test-clu-role
  apiGroup: rbac.authorization.k8s.io
eksctl create iamidentitymapping \
    --cluster eks-clu \
    --region=ap-northeast-1 \
    --arn arn:aws:iam::{AWS_ACCOUNT_ID}:user/eks-ope \
    --group operators \
    --no-duplicate-arns
eksctl get iamidentitymapping --cluster eks-clu --region=ap-northeast-1

eks-opeユーザのIAM認証情報でEKSクラスタに接続出来ることを確認

参考

サービスアカウントのIAMロール

IAM Roles for Service Accounts(IRSA)と呼ばれる。IAMロールをPodに紐づけてAWSリソースへのアクセス制御をする仕組み

仕組み

スクリーンショット 2022-12-22 10.27.18.png

ServiceAccoutを作成するとトークン情報を含んだSecretが作成される。このSecret情報がPodの所定のディレクトリにマウントされる。Podはこの情報を使用してOICD Provider経由で認証。Service Accountに関連付けられたロールが利用出来るようになる。認証情報は環境変数としてPod内に格納。(多分)

手順

全体的に設定の紐付けは下記のようになっています。

スクリーンショット 2022-12-22 10.47.46.png

クラスター用の IAM OIDC プロバイダーの作成

eksctl utils associate-iam-oidc-provider --cluster eks-clu --approve

IAMロール作成

aws iam create-role --role-name sa-role --assume-role-policy-document \
'{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::{AWS_ACCOUNT_ID}:oidc-provider/oidc.eks.ap-northeast-1.amazonaws.com/id/{OIDC-Provider-ID}"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "oidc.eks.ap-northeast-1.amazonaws.com/id/{OIDC-Provider-ID}:sub":"system:serviceaccount:default:test-sa"
        }
      }
    }
  ]
}'

IAMロールにIAMポリシー紐付け(テストでS3ReadOnlyAccessを追加)

aws iam attach-role-policy --role-name sa-role --policy-arn 'arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess'

サービスアカウント作成

ServiceAccount.yml
apiVersion: v1
kind: ServiceAccount
metadata:
  namespace: default
  name: test-sa
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::{AWS_ACCOUNT_ID}:role/sa-role

テスト用Pod作成

test-pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
  namespace: default
spec:
  containers:
  - name: test-pod
    image: amazon/aws-cli
    command: ["aws"]
    args: ["s3", "ls"]
  serviceAccountName: test-sa

S3にアクセス出来ていることを確認

kubectl logs test-pod
2022-11-30 22:47:14 cf-templates-xxxxxxxxx-ap-northeast-1

関連記事

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2