카테고리 없음

Terraform 으로 AWS 연동 후 EKS 띄우고 동작방법

하얀잔디 2026. 6. 16. 16:48
## 배경

k3s만 써봤는데 EKS도 한번 해보고 싶어서 시작함.
온프레미스랑 뭐가 다른지 궁금했음.

---

## 사전 준비

- AWS CLI 설치
- Terraform 설치
- kubectl 설치
- IAM Access Key 발급

aws configure 로 자격증명 설정하고 시작.

---

## 구조 이해

EKS는 컨트롤 플레인(API Server, etcd 등)을 AWS가 관리해줌.
k3s는 마스터 노드 직접 관리해야 했는데 그 부분이 없어진 거임.

네트워크 구조는 이렇게 됨:

인터넷
  ↓
Internet Gateway
  ↓
Public 서브넷 (NAT Gateway, LoadBalancer 위치)
  ↓
Private 서브넷 (워커 노드, Pod 위치)

VPC는 AWS 안에 나만 쓰는 사설 네트워크 공간임.
다른 계정이랑 IP 대역 겹쳐도 완전 격리돼서 상관없음.

---

## Terraform 코드

파일 구조:

versions.tf   → provider 버전 고정
variables.tf  → 변수 선언
main.tf       → VPC + EKS 생성
outputs.tf    → 클러스터 정보 출력

커뮤니티 모듈 두 개 사용:
- terraform-aws-modules/vpc/aws
- terraform-aws-modules/eks/aws

모듈 덕분에 IGW, 라우팅 테이블, IAM Role 같은 것들을
하나하나 안 써도 됨. 핵심 설정만 넘겨주면 알아서 만들어줌.

main.tf 핵심 부분:

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 5.0"

  cidr            = "10.0.0.0/16"
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]

  enable_nat_gateway = true
  single_nat_gateway = true  # 비용 절감
}

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "~> 20.0"

  cluster_name    = "my-eks-cluster"
  cluster_version = "1.30"
  vpc_id          = module.vpc.vpc_id
  subnet_ids      = module.vpc.private_subnets

  cluster_endpoint_public_access           = true
  enable_cluster_creator_admin_permissions = true

  eks_managed_node_groups = {
    default = {
      instance_types = ["t3.medium"]
      desired_size   = 2
    }
  }
}

cluster_endpoint_public_access = true 이거 없으면
로컬에서 kubectl 자체가 안 됨. 꼭 넣어야 함.

---

## 실행

terraform init
terraform plan
terraform apply  # 15~20분 소요

완료되면 kubectl 연결:

aws eks update-kubeconfig --region ap-northeast-2 --name my-eks-cluster
kubectl get nodes

---

## nginx 앱 배포

hellosion.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: hellosion-html
data:
  index.html: |
    <h1>hellosion</h1>

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellosion
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hellosion
  template:
    metadata:
      labels:
        app: hellosion
    spec:
      containers:
        - name: nginx
          image: nginx:alpine
          ports:
            - containerPort: 80
          volumeMounts:
            - name: html
              mountPath: /usr/share/nginx/html
      volumes:
        - name: html
          configMap:
            name: hellosion-html

---
apiVersion: v1
kind: Service
metadata:
  name: hellosion
spec:
  type: LoadBalancer
  selector:
    app: hellosion
  ports:
    - port: 80
      targetPort: 80

kubectl apply -f k8s/hellosion.yaml
kubectl get svc hellosion

EXTERNAL-IP에 AWS가 자동으로 도메인 붙여줌.
거기로 curl 치면 hellosion 출력됨.

k3s 때는 MetalLB 따로 설치해야 했는데
EKS는 type: LoadBalancer 선언만 하면 AWS가 ELB 자동 생성해줌.


## 비용 및 종료

시간당 약 $0.26 발생함. 연습 끝나면 바로 꺼야 함.

# 앱 먼저 삭제 (LoadBalancer 남으면 destroy 실패)
kubectl delete -f k8s/hellosion.yaml

# 인프라 전체 삭제
terraform destroy

# 확인
aws eks list-clusters
aws elbv2 describe-load-balancers \
  --query "LoadBalancers[*].LoadBalancerName" \
  --region ap-northeast-2

모두 빈 배열 나오면 완전히 꺼진 거임.

---

## k3s랑 비교 정리

항목              k3s              EKS
컨트롤 플레인      직접 관리         AWS 관리
kubeconfig        파일 직접 복사    aws eks update-kubeconfig
LoadBalancer      MetalLB 설치 필요  자동 생성
kubectl 사용법     동일              동일

kubectl 명령어는 완전히 동일함.
인프라 세팅 방법만 다른 거임.