APC 技術ブログ

株式会社エーピーコミュニケーションズの技術ブログです。

株式会社 エーピーコミュニケーションズの技術ブログです。

【AWS】EKSアドオンのPod設定を変更する方法

はじめに

こんにちは!クラウド事業部の中根です。
EKSアドオンについて、レプリカ数やラベル、Resourceなどの設定変更ができることをご存じでしょうか?
設定方法について、例を使ってご紹介します。

設定変更手順

CoreDNSアドオンを例に、設定方法をご紹介します。
アドオンやバージョンによって、設定できる項目は違うのですが、基本的な流れは同じです。

1. 設定できる項目の確認

マネジメントコンソールとCLIの2つの方法があります。

  • マネジメントコンソールから確認(既存のEKSクラスタがない場合)
    クラスターの作成画面に移動します。

    適当に必須値を入力し、ステップ4の「アドオンを選択」まで進んでください。
    使用するアドオンを選択し、次に進みます。

    次の画面で、各アドオンについての設定スキーマを確認することができます。

  • マネジメントコンソールから確認(既存のEKSクラスタがある場合)
    クラスタの詳細から、「アドオン」タブを開きます。

    使用するアドオンを選択し、「編集」を押下します。

    次の画面で、設定スキーマを確認することができます。

  • CLI
    まずはアドオンのバージョンを決めましょう。
    Kubernetesバージョンで利用可能なアドオンのバージョンは、以下のコマンドで見ることができます。

  K8S_VER=<EKSクラスタのバージョン>
  ADDON_NAME=<アドオンの名前>
  aws eks describe-addon-versions --addon-name $ADDON_NAME --query "addons[].addonVersions[?compatibilities[?clusterVersion=='${K8S_VER}']].addonVersion"

次のコマンドの出力が、アドオンの設定スキーマになります。

  ADDON_NAME=<アドオンの名前>
  ADDON_VERSION=<アドオンのバージョン>
  aws eks describe-addon-configuration --addon-name $ADDON_NAME --addon-version $ADDON_VERSION --query "configurationSchema" --output text | jq -r .

設定スキーマの見方

例として、CoreDNSアドオン(バージョン:v1.11.3-eksbuild.2)の設定スキーマを以下に示します。

  {
    "$ref": "#/definitions/Coredns",
    "$schema": "http://json-schema.org/draft-06/schema#",
    "definitions": {
      "Coredns": {
        "additionalProperties": false,
        "properties": {
          "affinity": {
            "default": {
              "affinity": {
                "nodeAffinity": {
                  "requiredDuringSchedulingIgnoredDuringExecution": {
                    "nodeSelectorTerms": [
                      {
                        "matchExpressions": [
                          {
                            "key": "kubernetes.io/os",
                            "operator": "In",
                            "values": [
                              "linux"
                            ]
                          },
                          {
                            "key": "kubernetes.io/arch",
                            "operator": "In",
                            "values": [
                              "amd64",
                              "arm64"
                            ]
                          }
                        ]
                      }
                    ]
                  }
                },
                "podAntiAffinity": {
                  "preferredDuringSchedulingIgnoredDuringExecution": [
                    {
                      "podAffinityTerm": {
                        "labelSelector": {
                          "matchExpressions": [
                            {
                              "key": "k8s-app",
                              "operator": "In",
                              "values": [
                                "kube-dns"
                              ]
                            }
                          ]
                        },
                        "topologyKey": "kubernetes.io/hostname"
                      },
                      "weight": 100
                    }
                  ]
                }
              }
            },
            "description": "Affinity of the coredns pods",
            "type": [
              "object",
              "null"
            ]
          },
          "autoScaling": {
            "additionalProperties": false,
            "description": "autoScaling configurations",
            "properties": {
              "enabled": {
                "default": false,
                "description": "the option to enable eks managed autoscaling for coredns",
                "type": "boolean"
              },
              "maxReplicas": {
                "description": "the max value that autoscaler can scale up the coredns replicas to",
                "maximum": 1000,
                "minimum": 2,
                "type": "integer"
              },
              "minReplicas": {
                "default": 2,
                "description": "the min value that autoscaler can scale down the coredns replicas to",
                "maximum": 1000,
                "minimum": 2,
                "type": "integer"
              }
            },
            "required": [
              "enabled"
            ],
            "type": "object"
          },
          "computeType": {
            "type": "string"
          },
          "corefile": {
            "description": "Entire corefile contents to use with installation",
            "type": "string"
          },
          "nodeSelector": {
            "additionalProperties": {
              "type": "string"
            },
            "type": "object"
          },
          "podAnnotations": {
            "properties": {},
            "title": "The podAnnotations Schema",
            "type": "object"
          },
          "podDisruptionBudget": {
            "description": "podDisruptionBudget configurations",
            "properties": {
              "enabled": {
                "default": true,
                "description": "the option to enable managed PDB",
                "type": "boolean"
              },
              "maxUnavailable": {
                "anyOf": [
                  {
                    "pattern": ".*%$",
                    "type": "string"
                  },
                  {
                    "type": "integer"
                  }
                ],
                "default": 1,
                "description": "maxUnavailable value for managed PDB, can be either string or integer; if it's string, should end with %"
              },
              "minAvailable": {
                "anyOf": [
                  {
                    "pattern": ".*%$",
                    "type": "string"
                  },
                  {
                    "type": "integer"
                  }
                ],
                "description": "minAvailable value for managed PDB, can be either string or integer; if it's string, should end with %"
              }
            },
            "type": "object"
          },
          "podLabels": {
            "properties": {},
            "title": "The podLabels Schema",
            "type": "object"
          },
          "replicaCount": {
            "type": "integer"
          },
          "resources": {
            "$ref": "#/definitions/Resources"
          },
          "tolerations": {
            "default": [
              {
                "key": "CriticalAddonsOnly",
                "operator": "Exists"
              },
              {
                "effect": "NoSchedule",
                "key": "node-role.kubernetes.io/control-plane"
              }
            ],
            "description": "Tolerations of the coredns pod",
            "items": {
              "type": "object"
            },
            "type": "array"
          },
          "topologySpreadConstraints": {
            "description": "The coredns pod topology spread constraints",
            "type": "array"
          }
        },
        "title": "Coredns",
        "type": "object"
      },
      "Limits": {
        "additionalProperties": false,
        "properties": {
          "cpu": {
            "type": "string"
          },
          "memory": {
            "type": "string"
          }
        },
        "title": "Limits",
        "type": "object"
      },
      "Resources": {
        "additionalProperties": false,
        "properties": {
          "limits": {
            "$ref": "#/definitions/Limits"
          },
          "requests": {
            "$ref": "#/definitions/Limits"
          }
        },
        "title": "Resources",
        "type": "object"
      }
    }
  }

パッと見だと分かりにくいですが、先頭の以下記述から、definitions.Coredns.propertiesから見たJSONパスを指定する必要があります。

  {
    "$ref": "#/definitions/Coredns",

例えば、最初のaffinityの中身を見ると、nodeAffinitypodAntiAffinityとありますし、descriptionにも簡単な説明文があることから、設定できる項目がわかります。
また、Kubernetesマニフェストと同じ項目名になっていることが多いです。

先頭以外の場所でも$ref となっている場合は、示されているJSONパスを確認すると、どう設定するべきかわかります。
例えば、例示した設定スキーマのresourcesでは、definitions.Resources.propertiesの要素を使用する必要があります。

            "resources": {
              # 示されているJSONパスを確認
              "$ref": "#/definitions/Resources"
            },

          (中略)

        # 対象のJSONパス
        "Resources": {
          "additionalProperties": false,
          "properties": {

            # この要素を使用する
            "limits": {
              "$ref": "#/definitions/Limits"
            },
            "requests": {
              "$ref": "#/definitions/Limits"
            }
          },
          "title": "Resources",
          "type": "object"
        }
      }
    }

解読すると、以下のようになっているということです。

              "resources": {
                "limits": {
                  "$ref": "#/definitions/Limits"
                },
                "requests": {
                  "$ref": "#/definitions/Limits"
                }
              },

2. 設定変更する

マネジメントコンソールとTerraformの2つの方法をご紹介します。
例として、Resourcesを変更してみます。
CoreDNSアドオンはデフォルトだと、以下のようなResourcesになっています。

* マネジメントコンソール
クラスタの詳細から、「アドオン」タブを開きます。

使用するアドオンを選択し、「編集」を押下します。

JSONとYAMLのどちらの形式でもよいので、変更したい項目について指定します。

参考までに両形式での記載例を載せておきます。
YAML

  resources:
    limits:
      memory: "190Mi"
    requests:
      cpu: "120m"
      memory: "90Mi"

JSON

  {
    "resources": {
      "limits": {
        "memory": "190Mi"
      },
      "requests": {
        "cpu": "120m",
        "memory": "90Mi"
      }
    }
  }

変更するとReplicaSetsが置き換わり、設定が適用されていることが確認できます。

  • Terraform
    TerraformのAWSプロバイダーのEKSモジュールを使う場合は、以下のように指定します。
    configuration_valuesは文字列なので、HCLで書く場合はjsonencode()を利用します。
  module "eks" {
    source = "terraform-aws-modules/eks/aws"

    (中略)

    cluster_addons = {
      coredns = {
        addon_version        = var.coredns_addon_version
        configuration_values = jsonencode({
          resources = {
            limits   = {
              memory = "200Mi"
            }
            requests = {
              cpu    = "110m"
              memory = "80Mi"
            }
          }
        })
      }
    }
  }

terraform applyすると、設定が適用されていることが確認できます。

まとめ

今回は、EKSアドオンのPod設定を変更する方法をご紹介しました。
EKSアドオンは非常に便利なので、細かい制御をしたい場合は、本記事を参考に設定変更してください!

終わりに

APCはAWS Advanced Tier Services(アドバンストティアサービスパートナー)認定を受けております。

その中で私達クラウド事業部はAWSなどのクラウド技術を活用したSI/SESのご支援をしております。
www.ap-com.co.jp

https://www.ap-com.co.jp/service/utilize-aws/

また、一緒に働いていただける仲間も募集中です!
今年もまだまだ組織規模拡大中なので、ご興味持っていただけましたらぜひお声がけください。

www.ap-com.co.jp