
VPC 생성
eksctl을 이용해서 VPC의 subnets를 이용해서 클러스터를 생성
aws --versionbrew install weaveworks/tap/eksctleksctl versioncurl -O https://s3.us-west-2.amazonaws.com/amazon-eks/1.31.0/2024-09-12/bin/darwin/amd64/kubectlkubectl version --clientAWS CloudFormation 에서 템플릿 파일을 활용하여 VPC 생성
base_resources_cfn.yaml : VPC를 생성하기 위한 CloudFormation 파일
base_resources_cfn.yaml
AWSTemplateFormatVersion: "2010-09-09"
# Parameters 부분은 변수 설정
Parameters:
ClusterBaseName:
Type: String
Default: eks-work
TargetRegion:
Type: String
Default: ap-northeast-2
AvailabilityZone1:
Type: String
Default: ap-northeast-2a
AvailabilityZone2:
Type: String
Default: ap-northeast-2b
AvailabilityZone3:
Type: String
Default: ap-northeast-2c
VpcBlock:
Type: String
Default: 192.168.0.0/16
WorkerSubnet1Block:
Type: String
Default: 192.168.0.0/24
WorkerSubnet2Block:
Type: String
Default: 192.168.1.0/24
WorkerSubnet3Block:
Type: String
Default: 192.168.2.0/24
# 리소스 부분
Resources:
# VPC 생성 부분
EksWorkVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcBlock
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Sub ${ClusterBaseName}-VPC
# VPC 안에서 사용할 서브넷으로 현재는 3개인데 위에 subnet을 추가하면 더 배치가 가능(AZ는 한국의 경우 4개 밖에 없으므로 5개 이상을 생성하고자 하는 경우에는 AZ를 중복시켜야 함)
WorkerSubnet1:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone1
CidrBlock: !Ref WorkerSubnet1Block
VpcId: !Ref EksWorkVPC
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub ${ClusterBaseName}-WorkerSubnet1
WorkerSubnet2:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone2
CidrBlock: !Ref WorkerSubnet2Block
VpcId: !Ref EksWorkVPC
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub ${ClusterBaseName}-WorkerSubnet2
WorkerSubnet3:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone3
CidrBlock: !Ref WorkerSubnet3Block
VpcId: !Ref EksWorkVPC
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub ${ClusterBaseName}-WorkerSubnet3
# 인터넷 게이트웨이 설정: 이 설정이 있어야 안에 만들어지는 노드들이 인터넷이 가능 - 게이트웨이를 생성하고 부착하는 부분
InternetGateway:
Type: AWS::EC2::InternetGateway
VPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref EksWorkVPC
# 라우팅 테이블을 만드는 부분
# Route 테이블을 출력한 이유는 다른 서브넷을 추가할 경우 라우팅 테이블에 서브넷을 추가해야만 그 서브넷에 다른 서브넷에서 접근이 가능하기 때문에
WorkerSubnetRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref EksWorkVPC
Tags:
- Key: Name
Value: !Sub ${ClusterBaseName}-WorkerSubnetRouteTable
WorkerSubnetRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref WorkerSubnetRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
WorkerSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref WorkerSubnet1
RouteTableId: !Ref WorkerSubnetRouteTable
WorkerSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref WorkerSubnet2
RouteTableId: !Ref WorkerSubnetRouteTable
WorkerSubnet3RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref WorkerSubnet3
RouteTableId: !Ref WorkerSubnetRouteTable
# 다른 곳에서 사용할 정보를 외부에 출력하기 위한 부분
# 서브넷을 출력한 이유는 이 서브넷을 알아야 eks 가 node 컴퓨터에 주소를 할당해서 배치를 하기 때문
Outputs:
VPC:
Value: !Ref EksWorkVPC
WorkerSubnets:
Value: !Join
- ","
- [!Ref WorkerSubnet1, !Ref WorkerSubnet2, !Ref WorkerSubnet3]
RouteTable:
Value: !Ref WorkerSubnetRouteTable
AWS [CloudFormation] - [스택] - [스택 생성]
[파일 선택]을 클릭하여, base_resources_cfn.yaml 파일 업로드

eks-work-base 후 생성
eks-work-VPC 생성 확인

eksctl create cluster
--vpc-public-subnets VPC의Subnets값
--name 클러스터이름(하나의 컴퓨터 내에서는 중복이 안되지만 다른 컴퓨터의 경우는 중복 가능)
--region 리전이름
--version 버전(1.xx: 현재는 24 부터 31까지 지원 - 쿠버네티스 관련 명령은 kubectl 을 이용하는데 AWS에서는 하나 이전 버전까지는 문제가 없을 거라고 합니다.)
--nodegroup-name 노드그룹이름(중복 가능)
--node-type 인스턴스하드웨어종류(사용할 수 있는 값이 제한적)
--nodes 노드의개수
--nodes-min 최소 노드의 개수
--nodes-max 최대 노드의 개수
eksctl create cluster --vpc-public-subnets subnet-056f729efa8aa92a7,subnet-07d9fda0bd3ff825b,subnet-064bdefd519c17d16 --name eks-work-cluster --region ap-northeast-2 --version 1.31 --nodegroup-name eks-work-nodegroup --node-type t2.micro --nodes 3 --nodes-min 3 --nodes-max 5 Permission Denied 에러가 발생하는 경우: IAM에서 사용자에게 권한을 추가kubectl get nodes 설정한 만큼의 노드가 보이는지 확인


kubectl config get-contexts 
kubectl config use-context 컨텍스트이름Pod 배포를 위한 야믈 파일 작성: pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx-app
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
야믈 파일 실행
kubectl apply -f pod.yaml
pod 생성 확인
kubectl get pods

service.yamlapiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: LoadBalancer
selector:
app: nginx-app
ports:
- protocol: TCP
port: 80
targetPort: 80kubectl apply -f service.yamlkubectl get svc


배포된 서비스가 있다면 삭제
kubectl delete svc nginx-service
클러스터 목록을 확인하여 클러스터 이름 확인
eksctl get cluster

eksctl delete cluster --name eks-work-cluster