brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Nov 11. 2021

EKS 5탄-3.EC2에서 EKS 생성 ,삭제

5탄 - 3/3


<1>  Cloudformation으로 VPC 생성

<2>  eks 클러스터 생성을 위한 ec2  수동생성 1대

<3>  eksctl로  cluster 생성하기

<4> 소스 다운로드

<5> 확인

<6>  EC2 >  세션 매니저로 접속한다.

<7> 소스 빌드와 컨테이너 이미지 생성 = 그래들 사용

<8> 삭제

<9> 적용 사례

<10> 다른 실습 보기



<1>  Cloudformation으로 VPC 생성


오래곤

us-west-2


public-subnet 3

private-subnet 3

db-subnet 2


Cloudformation




<2>  eks 클러스터 생성을 위한 ec2  수동생성 1대


https://brunch.co.kr/@topasvga/1692


1

ec2 키 페어 생성

k888

ec2를  public subnet에 공인 ip 할당 설치

admin-role을 ec2에  할당

sudo su -

kubectl

스크립트 설치



2

Pub subet에 ec2를 생성하면 자동으로 public IP를 할당받도록 한다.

Pub subnet 3개에 대해 auto-assign IP settings로 변경 필요

https://docs.aws.amazon.com/vpc/latest/userguide/vpc-ip-addressing.html#subnet-public-ip


//  설정하지 않으면 아래 오류가 나온다.

one or more Amazon EC2 Subnets of [subnet-08f6b6bcddd37465a, subnet-0d0300bd9516e22b0, subnet-0c414d627a067953c] for node group eks-work-nodegroup2 does not automatically assign public IP addresses to instances launched into it. If you want your instances to be assigned a public IP address, then you need to enable auto-assign public IP address for the subnet. See IP addressing in VPC guide:




<3> eksctl로  cluster 생성하기



1

node가 설치될 서브넷을 사전에 확인한다.


콘솔에서 public subnet 확인 = 워커 노드용 서브넷

subnet-0c414d627a067953c,subnet-0d0300bd9516e22b0,subnet-08f6b6bcddd37465a



2

MySSHKeypair=`aws ec2 describe-key-pairs --query 'KeyPairs[*].KeyName' --output text`

echo $MySSHKeypair



3

사전 테스트

eksctl create cluster --vpc-public-subnets subnet-0c414d627a067953c,subnet-08f6b6bcddd37465a,subnet-0d0300bd9516e22b0 --name eks-work-cluster --region us-west-2 --version 1.21  --nodegroup-name eks-work-nodegroup --node-type t3.medium --nodes 2 --nodes-min 2 --nodes-max 6  --with-oidc --node-volume-size=20 --ssh-access --ssh-public-key $MySSHKeypair --dry-run


eksctl create cluster --vpc-public-subnets subnet-0c414d627a067953c,subnet-08f6b6bcddd37465a,subnet-0d0300bd9516e22b0 --name eks-work-cluster --region us-west-2 --version 1.21  --nodegroup-name eks-work-nodegroup --node-type t3.medium --nodes 2 --nodes-min 2 --nodes-max 6  --with-oidc --node-volume-size=20 --ssh-access --ssh-public-key $MySSHKeypair

(생성중)

따로 중지하지 않고 기다려야 한다.


(15분 걸린다)



4

2021-11-08 20:20:24 [ℹ]  node "ip-10-20-10-171.us-west-2.compute.internal" is ready

2021-11-08 20:20:24 [ℹ]  node "ip-10-20-10-47.us-west-2.compute.internal" is ready

2021-11-08 20:20:25 [ℹ]  kubectl command should work with "/root/.kube/config", try 'kubectl get nodes'

2021-11-08 20:20:25 [✔]  EKS cluster "eks-work-cluster3" in "us-west-2" region is ready




<4>  소스 다운로드



1

EC2 >  세션 매니저로 접속 한다.



2

git clone https://github.com/dybooksIT/k8s-aws-book.git




<5> 확인



1

kubectl config get-contexts

CURRENT   NAME                                                        CLUSTER                                 AUTHINFO                                                    NAMESPACE

*         i-015df0933cd6851a5@eks-work-cluster3.us-west-2.eksctl.io   eks-work-cluster3.us-west-2.eksctl.io   i-015df0933cd6851a5@eks-work-cluster3.us-west-2.eksctl.io




2

/root/k8s-aws-book/eks-env


more 02_nginx_k8s.yaml

apiVersion: v1

kind: Pod

metadata:

  name: nginx-pod

  labels:

    app: nginx-app

spec:

  containers:

  - name: nginx-container

    image: nginx

    ports:

    - containerPort: 80




4

kubectl apply -f 02_nginx_k8s.yaml

pod/nginx-pod created



5

kubectl port-forward nginx-pod 8080:80

Forwarding from 127.0.0.1:8080 -> 80

Forwarding from [::1]:8080 -> 80



6

curl localhost:8080

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

html { color-scheme: light dark; }

body { width: 35em; margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif; }

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>


<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>


<p><em>Thank you for using nginx.</em></p>

</body>

</html>




7

kubectl delete pods nginx-pod

pod "nginx-pod" deleted





<6>  애플리케이션을 EKS 에 배포 준비


애플리케이션을 EKS에 배포하려면 컨테이너 이미지를 생성한다.

그 이미지를 컨테이너 레지스트리 저장소에 Push 해야 한다.

Push 하기 위해 도커 데스크톱을 설치한다.




<7> 소스 빌드와 컨테이너 이미지 생성 = 그래들 사용



1

자바 애플리케이션 빌드 도구인 그래들을 사용한다.



2

yum install java-11-amazon-corretto -y


alternatives --config java


There is 1 program that provides 'java'.

  Selection    Command

-----------------------------------------------

*+ 1           /usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java



java -version


openjdk version "11.0.21" 2023-10-17 LTS

OpenJDK Runtime Environment Corretto-11.0.21.9.1 (build 11.0.21+9-LTS)

OpenJDK 64-Bit Server VM Corretto-11.0.21.9.1 (build 11.0.21+9-LTS, mixed mode)





3

cd /root/k8s-aws-book/backend-app


./gradlew clean build





<8> 삭제


cloudformation 스택 삭제 = VPC 삭제

명령 EC2 삭제




<9> 다음 과정


https://brunch.co.kr/@topasvga/2466



<10> 다른 실습 보기


https://brunch.co.kr/@topasvga/1679



감사합니다.









매거진의 이전글 EKS 5탄-2. Cloud9생성과 EKS 생성

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari