AWS EKS Cluster 생성 / eksctl CLI 이용 / Ubuntu 18.04

AWS EKS Cluster 생성 / eksctl CLI 이용 / Ubuntu 18.04

1. 실행 환경

  • Ubuntu 18.04 LTS 64bit, root user
  • EKS Cluster
    • Version 1.18
    • Subnet 10.0.0.0/16
  • aws CLI
    • Region ap-northeast-2
    • Version 2.1.34
  • eksctl CLI
    • Version 0.43.0

2. aws CLI 설치

$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86-64.zip" -o "awscliv2.zip"
$ unzip awscliv2.zip
$ sudo ./aws/install

aws CLI를 설치한다.

$ aws configure
AWS Access Key ID [None]: <Access Key>
AWS Secret Access Key [None]: <Secret Access Key>
Default region name [None]: ap-northeast-2
Default output format [None]:

aws CLI에 인증정보를 설정한다.

3. eksctl 설치

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

4. SSH Key 생성

$ aws ec2 create-key-pair --key-name ssup2-eks-ssh --query 'KeyMaterial' --output text > ssup2-eks-ssh.pem

EKS Node에 SSH로 접근하기 위한 SSH Key를 생성한다.

5. Cluster 생성

EKS Cluster를 생성한다.

$ cat > ssup2-eks-cluster.yaml << EOL
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
  name: ssup2-eks-cluster
  region: ap-northeast-2
nodeGroups:
  - name: nodegroup-1
    instanceType: t3.medium
    desiredCapacity: 2
    ssh:
      publicKeyName: ssup2-eks-ssh
  - name: nodegroup-2
    instanceType: t3.medium
    desiredCapacity: 2
    ssh:
      publicKeyName: ssup2-eks-ssh
EOL

EKS Cluster Spec을 작성한다.

$ eksctl create cluster -f ssup2-eks-cluster.yaml
2021-04-06 14:21:50 []  eksctl version 0.43.0
2021-04-06 14:21:50 []  using region ap-northeast-2
2021-04-06 14:21:51 []  setting availability zones to [ap-northeast-2b ap-northeast-2c ap-northeast-2a]
2021-04-06 14:21:51 []  subnets for ap-northeast-2b - public:192.168.0.0/19 private:192.168.96.0/19
2021-04-06 14:21:51 []  subnets for ap-northeast-2c - public:192.168.32.0/19 private:192.168.128.0/19
2021-04-06 14:21:51 []  subnets for ap-northeast-2a - public:192.168.64.0/19 private:192.168.160.0/19
2021-04-06 14:21:51 []  nodegroup "nodegroup-1" will use "ami-018f6f9b3c61e383c" [AmazonLinux2/1.18]
2021-04-06 14:21:51 []  using EC2 key pair "ssup2-eks-ssh"
2021-04-06 14:21:51 []  nodegroup "nodegroup-2" will use "ami-018f6f9b3c61e383c" [AmazonLinux2/1.18]
2021-04-06 14:21:51 []  using EC2 key pair "ssup2-eks-ssh"
2021-04-06 14:21:51 []  using Kubernetes version 1.18
2021-04-06 14:21:51 []  creating EKS cluster "ssup2-eks-cluster" in "ap-northeast-2" region with un-managed nodes
2021-04-06 14:21:51 []  2 nodegroups (nodegroup-1, nodegroup-2) were included (based on the include/exclude rules)
2021-04-06 14:21:51 []  will create a CloudFormation stack for cluster itself and 2 nodegroup stack(s)
2021-04-06 14:21:51 []  will create a CloudFormation stack for cluster itself and 0 managed nodegroup stack(s)
2021-04-06 14:21:51 []  if you encounter any issues, check CloudFormation console or try 'eksctl utils describe-stacks --region=ap-northeast-2 --cluster=ssup2-eks-cluster'
2021-04-06 14:21:51 []  CloudWatch logging will not be enabled for cluster "ssup2-eks-cluster" in "ap-northeast-2"
2021-04-06 14:21:51 []  you can enable it with 'eksctl utils update-cluster-logging --enable-types={SPECIFY-YOUR-LOG-TYPES-HERE (e.g. all)} --region=ap-northeast-2 --cluster=ssup2-eks-cluster'
2021-04-06 14:21:51 []  Kubernetes API endpoint access will use default of {publicAccess=true, privateAccess=false} for cluster "ssup2-eks-cluster" in "ap-northeast-2"
2021-04-06 14:21:51 []  2 sequential tasks: { create cluster control plane "ssup2-eks-cluster", 3 sequential sub-tasks: { wait for control plane to become ready, create addons, 2 parallel sub-tasks: { create nodegroup "nodegroup-1", create nodegroup "nodegroup-2" } } }
2021-04-06 14:21:51 []  building cluster stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:21:52 []  deploying stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:22:22 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:22:52 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:23:52 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:24:52 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:25:52 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:26:52 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:27:52 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:28:53 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:29:53 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:30:53 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:31:53 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:32:53 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-cluster"
2021-04-06 14:32:55 []  building nodegroup stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:32:55 []  building nodegroup stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:32:55 []  --nodes-min=2 was set automatically for nodegroup nodegroup-2
2021-04-06 14:32:55 []  --nodes-max=2 was set automatically for nodegroup nodegroup-2
2021-04-06 14:32:55 []  --nodes-min=2 was set automatically for nodegroup nodegroup-1
2021-04-06 14:32:55 []  --nodes-max=2 was set automatically for nodegroup nodegroup-1
2021-04-06 14:32:55 []  deploying stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:32:55 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:32:55 []  deploying stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:32:55 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:33:11 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:33:13 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:33:31 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:33:31 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:33:46 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:33:47 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:34:03 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:34:07 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:34:20 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:34:24 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:34:40 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:34:43 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:34:56 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:35:01 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:35:15 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:35:19 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:35:34 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:35:34 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:35:52 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:35:53 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:36:07 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:36:09 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:36:25 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:36:26 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:36:41 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:36:43 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:36:57 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-2"
2021-04-06 14:37:00 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-nodegroup-nodegroup-1"
2021-04-06 14:37:00 []  waiting for the control plane availability...
2021-04-06 14:37:00 []  saved kubeconfig as "/root/.kube/config"
2021-04-06 14:37:00 []  no tasks
2021-04-06 14:37:00 []  all EKS cluster resources for "ssup2-eks-cluster" have been created
2021-04-06 14:37:00 []  adding identity "arn:aws:iam::132099918825:role/eksctl-ssup2-eks-cluster-nodegrou-NodeInstanceRole-1DV6ZYBOMIR3T" to auth ConfigMap
2021-04-06 14:37:00 []  nodegroup "nodegroup-1" has 0 node(s)
2021-04-06 14:37:00 []  waiting for at least 2 node(s) to become ready in "nodegroup-1"
2021-04-06 14:37:33 []  nodegroup "nodegroup-1" has 2 node(s)
2021-04-06 14:37:33 []  node "ip-192-168-48-175.ap-northeast-2.compute.internal" is ready
2021-04-06 14:37:33 []  node "ip-192-168-75-136.ap-northeast-2.compute.internal" is ready
2021-04-06 14:37:33 []  adding identity "arn:aws:iam::132099918825:role/eksctl-ssup2-eks-cluster-nodegrou-NodeInstanceRole-1MZ7I3GEFCBJM" to auth ConfigMap
2021-04-06 14:37:33 []  nodegroup "nodegroup-2" has 0 node(s)
2021-04-06 14:37:33 []  waiting for at least 2 node(s) to become ready in "nodegroup-2"
2021-04-06 14:39:55 []  nodegroup "nodegroup-2" has 2 node(s)
2021-04-06 14:39:55 []  node "ip-192-168-46-6.ap-northeast-2.compute.internal" is ready
2021-04-06 14:39:55 []  node "ip-192-168-90-3.ap-northeast-2.compute.internal" is ready
2021-04-06 14:39:57 []  kubectl command should work with "/root/.kube/config", try 'kubectl get nodes'
2021-04-06 14:39:57 []  EKS cluster "ssup2-eks-cluster" in "ap-northeast-2" region is ready

작성한 EKS Cluster Spec과 eksctl CLI를 통해서 EKS Cluster를 생성한다.

6. EKS Cluster 동작 확인

생성한 EKS Cluster의 동작을 확인한다.

$ eksctl utils write-kubeconfig --cluster ssup2-eks-cluster
2021-04-06 14:40:28 []  eksctl version 0.43.0
2021-04-06 14:40:28 []  using region ap-northeast-2
2021-04-06 14:40:29 []  saved kubeconfig as "/root/.kube/config"

생성한 EKS Cluster의 kubeconfig를 설정한다.

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.5", GitCommit:"6b1d87acf3c8253c123756b9e61dac642678305f", GitTreeState:"clean", BuildDate:"2021-03-31T15:33:39Z", GoVersion:"go1.15.10", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18+", GitVersion:"v1.18.9-eks-d1db3c", GitCommit:"d1db3c46e55f95d6a7d3e5578689371318f95ff9", GitTreeState:"clean", BuildDate:"2020-10-20T22:18:07Z", GoVersion:"go1.13.15", Compiler:"gc", Platform:"linux/amd64"}

$ kubectl get nodes
NAME                                            STATUS   ROLES    AGE   VERSION
ip-192-168-46-6.ap-northeast-2.compute.internal     Ready    <none>   3m29s   v1.18.9-eks-d1db3c
ip-192-168-48-175.ap-northeast-2.compute.internal   Ready    <none>   4m1s    v1.18.9-eks-d1db3c
ip-192-168-75-136.ap-northeast-2.compute.internal   Ready    <none>   4m2s    v1.18.9-eks-d1db3c
ip-192-168-90-3.ap-northeast-2.compute.internal     Ready    <none>   3m25s   v1.18.9-eks-d1db3c

생성한 EKS Cluster의 Node와 Version을 확인한다.

7. AWS Load Balancer Controller 설치

AWS Load Balancer Controller를 설치한다.

$ eksctl utils associate-iam-oidc-provider --cluster ssup2-eks-cluster --approve
2021-04-06 14:44:35 []  eksctl version 0.43.0
2021-04-06 14:44:35 []  using region ap-northeast-2
2021-04-06 14:44:36 []  will create IAM Open ID Connect provider for cluster "ssup2-eks-cluster" in "ap-northeast-2"
2021-04-06 14:44:36 []  created IAM Open ID Connect provider for cluster "ssup2-eks-cluster" in "ap-northeast-2"

Cluster를 위한 OICD Provider를 생성한다.

$ curl -o iam-policy.json https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.1.3/docs/install/iam-policy.json
$ aws iam create-policy --policy-name ssup2-AWSLoadBalancerControllerIAMPolicy --policy-document file://iam-policy.json
{
    "Policy": {
        "PolicyName": "ssup2-AWSLoadBalancerControllerIAMPolicy",
        "PolicyId": "ANPAR5QOEZPURVEDEZBPN",
        "Arn": "arn:aws:iam::132099918825:policy/ssup2-AWSLoadBalancerControllerIAMPolicy",
        "Path": "/",
        "DefaultVersionId": "v1",
        "AttachmentCount": 0,
        "PermissionsBoundaryUsageCount": 0,
        "IsAttachable": true,
        "CreateDate": "2021-04-06T14:50:22+00:00",
        "UpdateDate": "2021-04-06T14:50:22+00:00"
    }
}

$ eksctl create iamserviceaccount --cluster=ssup2-eks-cluster --namespace=kube-system --name=aws-load-balancer-controller --attach-policy-arn=arn:aws:iam::132099918825:policy/ssup2-AWSLoadBalancerControllerIAMPolicy --override-existing-serviceaccounts --approve
2021-04-06 14:52:18 []  eksctl version 0.43.0
2021-04-06 14:52:18 []  using region ap-northeast-2
2021-04-06 14:52:19 []  1 iamserviceaccount (kube-system/aws-load-balancer-controller) was included (based on the include/exclude rules)
2021-04-06 14:52:19 [!]  metadata of serviceaccounts that exist in Kubernetes will be updated, as --override-existing-serviceaccounts was set
2021-04-06 14:52:19 []  1 task: { 2 sequential sub-tasks: { create IAM role for serviceaccount "kube-system/aws-load-balancer-controller", create serviceaccount "kube-system/aws-load-balancer-controller" } }
2021-04-06 14:52:19 []  building iamserviceaccount stack "eksctl-ssup2-eks-cluster-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2021-04-06 14:52:20 []  deploying stack "eksctl-ssup2-eks-cluster-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2021-04-06 14:52:20 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2021-04-06 14:52:36 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2021-04-06 14:52:53 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2021-04-06 14:53:12 []  waiting for CloudFormation stack "eksctl-ssup2-eks-cluster-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2021-04-06 14:53:13 []  created serviceaccount "kube-system/aws-load-balancer-controller"

$ kubectl -n kube-system get sa
NAME                                 SECRETS   AGE
...
aws-load-balancer-controller         1         3m7s
...

AWS Load Balancer Controller가 이용할 Policy를 생성하고 Policy와 연결되는 k8s Service Account를 생성한다.

$ kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/v1.1.1/cert-manager.yaml
$ curl -o v2-1-3-full.yaml https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.1.3/docs/install/v2-1-3-full.yaml

$# Delete service account manifest in v2-1-3-full.yaml
$# Replace "your-cluster-name" to "ssup2-eks-cluster" in deployment manifest in v2-1-3-full.yaml 

$ kubectl apply -f v2-1-3-full.yaml
$ kubectl -n kube-system get pod
NAME                                          READY   STATUS    RESTARTS   AGE
aws-load-balancer-controller-bc59445f-l4brz   1/1     Running   0          28s
...

AWS Load Balancer Controller를 배포한다.

8. EKS Cluster 삭제

$ eksctl delete cluster --region=ap-northeast-2 --name=ssup2-eks-cluster 

EKS Cluster를 삭제한다.