eksctl을 사용하여 EKS 클러스터 생성하기 (1.21 ver)

Jay·2022년 4월 3일
0

개요

eksctl은 Amazon EKS에서 Kubernetes 클러스터를 생성하고 관리하기 위한 명령줄 유틸리티이다.

본 문서에서는 eksctl을 실행할 수 있는 환경을 구성하고, 클러스터 및 노드 그룹을 생성하는 과정을 설명한다.

사전 구성

IAM User 생성

EKS 클러스터 생성을 위한 IAM User를 생성한다.

액세스 유형

aws cli에 Credential을 등록하여 사용할 것이므로, 프로그래밍 방식 액세스을 선택한다.

연결 정책

  • AmazonEC2FullAccess (Managed)
  • AWSCloudFormationFullAccess (Managed)
  • EKSFullAccess
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "eks:*",
            "Resource": "*"
        },
        {
            "Action": [
                "ssm:GetParameter",
                "ssm:GetParameters"
            ],
            "Resource": [
                "arn:aws:ssm:*:<account_id>:parameter/aws/*",
                "arn:aws:ssm:*::parameter/aws/*"
            ],
            "Effect": "Allow"
        },
        {
             "Action": [
               "kms:CreateGrant",
               "kms:DescribeKey"
             ],
             "Resource": "*",
             "Effect": "Allow"
        },
        {
             "Action": [
               "logs:PutRetentionPolicy"
             ],
             "Resource": "*",
             "Effect": "Allow"
        }        
    ]
}
  • IAMLimitedAccess
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:CreateInstanceProfile",
                "iam:DeleteInstanceProfile",
                "iam:GetInstanceProfile",
                "iam:RemoveRoleFromInstanceProfile",
                "iam:GetRole",
                "iam:CreateRole",
                "iam:DeleteRole",
                "iam:AttachRolePolicy",
                "iam:PutRolePolicy",
                "iam:ListInstanceProfiles",
                "iam:AddRoleToInstanceProfile",
                "iam:ListInstanceProfilesForRole",
                "iam:PassRole",
                "iam:DetachRolePolicy",
                "iam:DeleteRolePolicy",
                "iam:GetRolePolicy",
                "iam:GetOpenIDConnectProvider",
                "iam:CreateOpenIDConnectProvider",
                "iam:DeleteOpenIDConnectProvider",
                "iam:TagOpenIDConnectProvider",                
                "iam:ListAttachedRolePolicies",
                "iam:TagRole"
            ],
            "Resource": [
                "arn:aws:iam::<account_id>:instance-profile/eksctl-*",
                "arn:aws:iam::<account_id>:role/eksctl-*",
                "arn:aws:iam::<account_id>:oidc-provider/*",
                "arn:aws:iam::<account_id>:role/aws-service-role/eks-nodegroup.amazonaws.com/AWSServiceRoleForAmazonEKSNodegroup",
                "arn:aws:iam::<account_id>:role/eksctl-managed-*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iam:GetRole"
            ],
            "Resource": [
                "arn:aws:iam::<account_id>:role/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iam:CreateServiceLinkedRole"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "iam:AWSServiceName": [
                        "eks.amazonaws.com",
                        "eks-nodegroup.amazonaws.com",
                        "eks-fargate.amazonaws.com"
                    ]
                }
            }
        }
    ]
}

User 생성 후 액세스 키와 비밀 키를 저장한다.

AWS CLI 업데이트 및 자격 증명 구성

AWS CLI 업데이트

EKS 클러스터에 kubectl를 사용하여 접근하기 위해, AWS CLI를 버전 1.21 이상으로 업데이트한다.
다음 명령을 사용하여 AWS CLI를 최신 버전으로 업데이트할 수 있다.

$ aws --version # 버전을 확인하여, 1.21 이하인 경우 업데이트
$ pip install awscli --upgrade --user

위에서 생성한 IAM User의 Credential을 등록한다.

$ aws configure

사용 툴 설치

eksctl 설치

eksctl 바이너리를 다운로드받아 설치한다.

$ curl --silent --location "https://github.com/weaveworks/eksctl/releases/download/latest_release/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
$ sudo mv -v /tmp/eksctl /usr/local/bin

kubectl 설치

생성된 쿠버네티스 클러스터 API와 통신하기 위해 kubectl을 설치한다.

$ curl -o kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.21.2/2021-07-05/bin/linux/amd64/kubectl
$ chmod +x ./kubectl
$ mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$PATH:$HOME/bin
$ echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc
$ kubectl version --client

EKS 클러스터 및 노드 그룹 생성

생성할 EKS 클러스터와 노드그룹의 정보는 다음과 같다.

클러스터 이름eks-an2-cnl-dev
쿠버네티스 버전1.21
리전ap-northeast-2
노드 그룹 이름nodegroup-an2-cnl-dev
- 인스턴스 타입t3.micro
- min1
- desired2
- max3

flag를 사용하는 방법

$ eksctl create cluster \
		--name eks-an2-cnl-dev \
		--version 1.21 \
		--vpc-private-subnets SUBNET-ID-1,SUBNET-ID-2
		--without-nodegroup
$ eksctl create nodegroup \
		--cluster eks-an2-cnl-dev \
		--name nodegroup-an2-cnl-dev \
		--node-type t3.micro \
		--nodes 2 \
		--nodes-min 1 \
		--nodes-max 3 \
		--node-private-networking \
		--managed=false

그 외에 사용할 수 있는 플래그는 다음과 같다.

General flags:
  -n, --name string               EKS cluster name (generated if unspecified, e.g. "scrumptious-sculpture-1648975311")
      --tags stringToString       A list of KV pairs used to tag the AWS resources (e.g. "Owner=John Doe,Team=Some Team") (default [])
  -r, --region string             AWS region
      --zones strings             (auto-select if unspecified)
      --version string            Kubernetes version (valid options: 1.12, 1.13, 1.14, 1.15) (default "1.14")
  -f, --config-file string        load configuration from a file (or stdin if set to '-')
      --timeout duration          maximum waiting time for any long-running operation (default 25m0s)
      --install-vpc-controllers   Install VPC controller that's required for Windows workloads
      --managed                   Create EKS-managed nodegroup
      --fargate                   Create a Fargate profile scheduling pods in the default and kube-system namespaces onto Fargate

Initial nodegroup flags:
      --nodegroup-name string          name of the nodegroup (generated if unspecified, e.g. "ng-46273157")
      --without-nodegroup              if set, initial nodegroup will not be created
  -t, --node-type string               node instance type (default "m5.large")
  -N, --nodes int                      total number of nodes (for a static ASG) (default 2)
  -m, --nodes-min int                  minimum nodes in ASG (default 2)
  -M, --nodes-max int                  maximum nodes in ASG (default 2)
      --node-volume-size int           node volume size in GB
      --node-volume-type string        node volume type (valid options: gp2, io1, sc1, st1) (default "gp2")
      --max-pods-per-node int          maximum number of pods per node (set automatically if unspecified)
      --ssh-access                     control SSH access for nodes. Uses ~/.ssh/id_rsa.pub as default key path if enabled
      --ssh-public-key string          SSH public key to use for nodes (import from local path, or use existing EC2 key pair)
      --node-ami string                Advanced use cases only. If 'ssm' is supplied (default) then eksctl will use SSM Parameter; if 'auto' is supplied then eksctl will automatically set the AMI based on version/region/instance type; if static is supplied (deprecated), then static AMIs will be used; if any other value is supplied it will override the AMI to use for the nodes. Use with extreme care.
      --node-ami-family string         Advanced use cases only. If 'AmazonLinux2' is supplied (default), then eksctl will use the official AWS EKS AMIs (Amazon Linux 2); if 'Ubuntu1804' is supplied, then eksctl will use the official Canonical EKS AMIs (Ubuntu 18.04). (default "AmazonLinux2")
  -P, --node-private-networking        whether to make nodegroup networking private
      --node-security-groups strings   Attach additional security groups to nodes, so that it can be used to allow extra ingress/egress access from/to pods
      --node-labels stringToString     Extra labels to add when registering the nodes in the nodegroup, e.g. "partition=backend,nodeclass=hugememory" (default [])
      --node-zones strings             (inherited from the cluster if unspecified)

Cluster and nodegroup add-ons flags:
      --asg-access            enable IAM policy for cluster-autoscaler
      --external-dns-access   enable IAM policy for external-dns
      --full-ecr-access       enable full access to ECR
      --appmesh-access        enable full access to AppMesh
      --alb-ingress-access    enable full access for alb-ingress-controller

VPC networking flags:
      --vpc-cidr ipNet                 global CIDR to use for VPC (default 192.168.0.0/16)
      --vpc-private-subnets strings    re-use private subnets of an existing VPC
      --vpc-public-subnets strings     re-use public subnets of an existing VPC
      --vpc-from-kops-cluster string   re-use VPC from a given kops cluster
      --vpc-nat-mode string            VPC NAT mode, valid options: HighlyAvailable, Single, Disable (default "Single")

Config File을 사용하는 방법

config.yaml

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: eks-an2-cnl-dev
  region: ap-northeast-2
  version: 1.21
  
vpc:
  subnets:
    private:
      ap-northeast-2a: {id: [SUBNET-ID-1]}
      ap-northeast-2b: {id: [SUBNET-ID-2]}

nodeGroups:
  - name: nodegroup-an2-cnl-dev
    instanceType: t3.micro
    desiredCapacity: 2
    minCapacity: 1
    maxCapacity: 3
    privateNetworking: true

EKS 클러스터 생성을 위한 Config File을 작성한다.
아래 설정 파일은 기존에 구성된 VPC 내에 EKS 클러스터를 생성하고, 노드가 2개인 노드그룹을 생성한다.
그 외의 설정에 대해서는 다음 링크를 참고한다.

https://eksctl.io/usage/creating-and-managing-clusters

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: eks-an2-cnl-birdbox-0
  region: ap-northeast-2
  
vpc:
  subnets:
    private:
      ap-northeast-2a: {id: [subnet-id]}
      ap-northeast-2b: {id: [subnet-id]}

nodeGroups:
  - name: ng-1-workers
    labels: {role: worker}
    instanceType: t2.micro
    desiredCapacity: 2
    privateNetworking: true

작성한 config 파일을 기반으로 클러스터를 생성하기 위해 eksctl crate cluster 명령을 사용한다.

$ eksctl create cluster -f cluster.yaml

kubectl 명령으로 생성된 노드를 확인할 수 있다.

$ kubectl get nodes
profile
공부 하고 싶은 직장인의 기록장

0개의 댓글