1 # 테라폼 설치법 앞에서 CloudFotmation으로 EC2를 만들었으면 설치법은 필요없다. EC2에 테라폼이 설치 되어 있다. 아래는 설치법은 참조. Mac (Homebrew):에 설치법 brew tap hashicorp/tap brew install hashicorp/tap/terraform 2 Windows pc에 설치법 Win 키를 누르고 PowerShell을 검색합니다. PowerShell Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) choco install terraform -y 터미널 재시작 (매우 중요) 설치가 끝난 후에는 반드시 PowerShell 창을 껐다가 다시 관리자 권한으로 실행해야 합니다. terraform -version 3 Linux (Ubuntu/Debian) 에 설치법 sudo apt-get update && sudo apt-get install -y gnupg software-properties-common wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install terraform 4 # 실습 시나리오: AWS VPC 구성하기 mkdir 1 cd 1 vi main.tfyes # 1. Provider 설정 (AWS를 쓰겠다고 선언) provider "aws" { region = "ap-northeast-2" # 서울 리전 } # 2. VPC 생성 resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" tags = { Name = "terraform-vpc-demo" } } # 3. Public Subnet 생성 resource "aws_subnet" "public_subnet" { vpc_id = aws_vpc.main.id # 위에서 만든 VPC의 ID를 참조 cidr_block = "10.0.1.0/24" availability_zone = "ap-northeast-2a" map_public_ip_on_launch = true # 인스턴스 실행 시 자동으로 공인 IP 할당 tags = { Name = "terraform-public-subnet" } } # 4. Internet Gateway 생성 (외부 통신용) resource "aws_internet_gateway" "igw" { vpc_id = aws_vpc.main.id tags = { Name = "terraform-igw" } } # 5. Route Table 생성 (0.0.0.0/0 -> IGW로 가라) resource "aws_route_table" "public_rt" { vpc_id = aws_vpc.main.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.igw.id } tags = { Name = "terraform-public-rt" } } # 6. Route Table을 Subnet에 연결 (Association) resource "aws_route_table_association" "public_rt_assoc" { subnet_id = aws_subnet.public_subnet.id route_table_id = aws_route_table.public_rt.id } 콘솔에서 VPC 생성 확인 Name = "terraform-vpc-demo" 삭제 terraform destroy 5 cd mkdir 2 cd 2 vi main.tf ----------------- # 1. Provider 설정 provider "aws" { region = "ap-northeast-2" } # 2. VPC 생성 (EKS를 위한 네트워크 환경) # EKS는 최소 2개의 가용 영역(AZ)과 Public/Private 서브넷이 필요합니다. module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 5.0" name = "my-eks-vpc" cidr = "10.0.0.0/16" azs = ["ap-northeast-2a", "ap-northeast-2c"] private_subnets = ["10.0.1.0/24", "10.0.2.0/24"] # 워커 노드가 배치될 곳 public_subnets = ["10.0.101.0/24", "10.0.102.0/24"] # 로드밸런서(ALB/NLB)가 배치될 곳 enable_nat_gateway = true # Private 노드가 인터넷 통신(이미지 pull 등)을 하려면 필요 single_nat_gateway = true # 비용 절감을 위해 1개만 생성 (운영 환경에선 false 권장) # EKS가 로드밸런서를 생성할 때 태그를 참조하므로 필수 설정 public_subnet_tags = { "kubernetes.io/role/elb" = 1 } private_subnet_tags = { "kubernetes.io/role/internal-elb" = 1 } } # 3. EKS 클러스터 생성 module "eks" { source = "terraform-aws-modules/eks/aws" version = "~> 20.0" cluster_name = "my-terraform-eks" cluster_version = "1.34" # 최신 안정 버전 # 클러스터 네트워크 설정 (위에서 만든 VPC 참조) vpc_id = module.vpc.vpc_id subnet_ids = module.vpc.private_subnets control_plane_subnet_ids = module.vpc.private_subnets # 외부(내 PC)에서 kubectl 명령을 날릴 수 있도록 설정 cluster_endpoint_public_access = true # 4. 관리형 노드 그룹 (Managed Node Group) - 실제 워커 노드 eks_managed_node_groups = { # 노드 그룹 이름 general_nodes = { min_size = 1 max_size = 3 desired_size = 2 instance_types = ["t3.medium"] # t3.small은 EKS 구동에 리소스가 부족할 수 있음 capacity_type = "ON_DEMAND" # 비용 절감 시 "SPOT" 사용 가능 } } # 현재 테라폼을 실행하는 IAM 사용자에게 관리자 권한 부여 enable_cluster_creator_admin_permissions = true tags = { Environment = "dev" Terraform = "true" } } 6 terraform init terraform apply # Plan 확인 후 yes 입력 (15 분) 7 ec2 생성 모니터링 while true; do aws ec2 describe-instances --query "Reservations[*].Instances[*].{PublicIPAdd:PublicIpAddress,InstanceName:Tags[?Key=='Name']|[0].Value,Status:State.Name}" --filters Name=instance-state-name,Values=running --output text ; echo "------------------------------" ; sleep 1; done 8 aws eks update-kubeconfig --region ap-northeast-2 --name my-terraform-eks kubectl get nodes ------ cd mkdir mario cd mario cat <