LoginSignup
1
1

More than 1 year has passed since last update.

Amazon EKSでNGINX Ingress ControllerにS3ログ出力を設定する

Last updated at Posted at 2022-10-28

はじめに

Amazon EKSでNGINX Ingress 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. NGINX Ingress Controllerの設定
    Amazon EKSでNGINX Ingress Controllerを利用するを参考にしてください。

  6. NGINX Ingress ControllerのAWS Certificate Managerの設定
    Amazon EKSでNGINX Ingress 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. バケットポリシーを設定する
    Classic 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. paatch.yaml を作成する
    ELB Access Logs on AWSを参考にしてください。
    ※バケット名、プレフィクスを設定する
    ※今回は akane-dev-elb-access-log というバケット名にする。
    ※今回はプレフィクスをpublic-elbにする。

    paatch.yaml
    controller:
      service:
        targetPorts:
          http: http
          https: http
        annotations:
          service.beta.kubernetes.io/aws-load-balancer-ssl-cert: ${ACMのARN}
          service.beta.kubernetes.io/aws-load-balancer-backend-protocol: "http"
          service.beta.kubernetes.io/aws-load-balancer-ssl-ports: "https"
          service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout: '3600'
          # Specifies whether access logs are enabled for the load balancer
          service.beta.kubernetes.io/aws-load-balancer-access-log-enabled: "true"
          # The interval for publishing the access logs. You can specify an interval of either 5 or 60 (minutes).
          service.beta.kubernetes.io/aws-load-balancer-access-log-emit-interval: "60"
          # The name of the Amazon S3 bucket where the access logs are stored
          service.beta.kubernetes.io/aws-load-balancer-access-log-s3-bucket-name: "akane-dev-elb-access-log"
          # The logical hierarchy you created for your Amazon S3 bucket, for example `my-bucket-prefix/prod`
          service.beta.kubernetes.io/aws-load-balancer-access-log-s3-bucket-prefix: "public-elb/prod"
    
  2. NGINX Ingress ControllerのチャートをHelmでアップグレードする
    ※事前にKubernetesクラスターのコンテキストの設定をします。

    helm upgrade -i nginx-ingress-controller \
        ingress-nginx/ingress-nginx \
        -n nginx-ingress-controller \
        -f patch.yaml
    

クリーンアップ

  1. NGINX Ingress ControllerをHelmで削除する

    helm delete nginx-ingress-controller -n nginx-ingress-controller
    
  2. Namespaceを削除する

    kubectl delete ns nginx-ingress-controller
    
  3. 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