1
1

More than 1 year has passed since last update.

Amazon EKSでAWS Load Balancer ControllerにS3ログ出力を設定する

Last updated at Posted at 2022-10-29

はじめに

Amazon EKSでAWS Load Balancer ControllerにS3ログ出力を設定する方法をまとめます。
Mac環境を想定しています。

実行環境の準備

  1. AWS CLIの設定
    AWS CloudFormationを動かすためのAWS CLIの設定を参考にしてください。

  2. EKSクラスタの構築
    Macでeksctlを利用してAmazon EKSのクラスターを構築するを参考にしてください。

  3. EKSのコンテキストの設定
    MacにてAmazon EKSの設定をするを参考にしてください。

  4. Helmの設定
    Amazon EKSでHelmを利用するを参考にしてください。

  5. ArgoCDの設定
    Amazon EKSでArgoCDを利用するを参考にしてください。

  6. AWS Load Balancer Controllerの設定
    Amazon EKSでAWS Load Balancer Controllerを利用するを参考にしてください。

  7. AWS Load Balancer ControllerのAWS Certificate Managerの設定
    Amazon EKSでAWS Load Balancer ControllerにAWS Certificate Managerを利用するを参考にしてください。

S3バケットの作成

  1. S3バケットを作成する
    ※今回は akane-dev-elb-access-log というバケット名にする

    BUCKET_NAME=akane-dev-elb-access-log
    aws s3api create-bucket \
      --bucket ${BUCKET_NAME} \
      --create-bucket-configuration LocationConstraint=ap-northeast-1
    
  2. バケットポリシーを設定する
    Application Load Balancer のアクセスログを有効にするを参考にしてください。
    ※今回は東京リージョンのため、ELBのサービスアカウント(ELBのAWSアカウントID)である582318560864を設定する
    ※今回はプレフィクスをpublic-elbにする。

    export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
    BUCKET_NAME=akane-dev-elb-access-log
    PREFIX=public-elb
    BUCKET_POLICY=$(echo -n '{
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "AWS": "arn:aws:iam::582318560864:root"
          },
          "Action": "s3:PutObject",
          "Resource": "arn:aws:s3:::'; echo -n "${BUCKET_NAME}"; echo -n '/'; echo -n "${PREFIX}"; echo -n '/AWSLogs/'; echo -n "${ACCOUNT_ID}"; echo -n '/*"
        }
      ]
    }')
    aws s3api put-bucket-policy \
    --bucket ${BUCKET_NAME} \
    --policy "${BUCKET_POLICY}"
    

環境設定

  1. akane-ingress.yaml を作成する
    Custom attributesを参考にしてください。
    ACMのARNを設定する
    ※今回は akane-dev-elb-access-log というバケット名にする
    ※今回はプレフィクスをpublic-elbにする。

    akane-ingress.yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      annotations:
        kubernetes.io/ingress.class: alb
        alb.ingress.kubernetes.io/scheme: internet-facing
        alb.ingress.kubernetes.io/target-type: ip
        alb.ingress.kubernetes.io/certificate-arn: ${ACMのARN}
        alb.ingress.kubernetes.io/load-balancer-attributes: access_logs.s3.enabled=true,access_logs.s3.bucket=akane-dev-elb-access-log,access_logs.s3.prefix=public-elb
      namespace: akane
    spec:
      rules:
      - http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: guestbook-ui
                port:
                  number: 3000
    
  2. クラスターに適用する

    kubectl apply -f akane-ingress.yaml
    

クリーンアップ

  1. クラスターから削除する

    kubectl delete -f akane-ingress.yaml
    
  2. S3バケットを削除する
    ※今回は akane-dev-elb-access-log というバケット名にする。

    aws s3 rb s3://akane-dev-elb-access-log --force
    
1
1
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
1
1