본문 바로가기
AEWS2기

[실습] terrafrom - EKS 배포

by yeongki0944 2024. 4. 27.

 

1. eks 배포 terraform 코드 clone

# 코드 가져오기
git clone https://github.com/gasida/aews-cicd.git
cd aews-cicd/4

 

ap-northeast-2의 VPC를 모두 사용했기에, us-east-1으로 배포하다록 var.tf파일 수정

 

# terraform 환경 변수 저장
export TF_VAR_KeyName=[각자 ssh keypair]
export TF_VAR_KeyName='eks-pem-key'
echo $TF_VAR_KeyName

 

 

 

 

terraform으로 eks배포 중  에러 발생

 

 EC2 Role > access key로 변경

 

10분뒤 terraform apply 성공

 

AWS Console 확인

 

eks 명령어

# ExternalDNS
MyDomain=<자신의 도메인>
*MyDomain=gasida.link*
MyDnzHostedZoneId=$(aws route53 list-hosted-zones-by-name --dns-name "${MyDomain}." --query "HostedZones[0].Id" --output text)
echo $MyDomain, $MyDnzHostedZoneId
curl -s -O <https://raw.githubusercontent.com/gasida/PKOS/main/aews/externaldns.yaml>
MyDomain=$MyDomain MyDnzHostedZoneId=$MyDnzHostedZoneId envsubst < externaldns.yaml | kubectl apply -f -

# kube-ops-view
helm repo add geek-cookbook <https://geek-cookbook.github.io/charts/>
helm install kube-ops-view geek-cookbook/kube-ops-view --version 1.2.2 --set env.TZ="Asia/Seoul" --namespace kube-system
kubectl patch svc -n kube-system kube-ops-view -p '{"spec":{"type":"LoadBalancer"}}'
kubectl annotate service kube-ops-view -n kube-system "external-dns.alpha.kubernetes.io/hostname=kubeopsview.$MyDomain"
echo -e "Kube Ops View URL = "

# AWS LB Controller
helm repo add eks <https://aws.github.io/eks-charts>
helm repo update
helm install aws-load-balancer-controller eks/aws-load-balancer-controller -n kube-system --set clusterName=$CLUSTER_NAME \\
  --set serviceAccount.create=false --set serviceAccount.name=aws-load-balancer-controller

# gp3 스토리지 클래스 생성
kubectl apply -f <https://raw.githubusercontent.com/gasida/PKOS/main/aews/gp3-sc.yaml>

 

 

2. terraform 코드 재사용 검증 : 두번째 EKS 클러스터 배포

cd ..
mkdir 5
cd 5
cp ../4/*.tf .
ls

 

terraform init
terraform apply -auto-approve -var=ClusterBaseName=myeks2 -var=KubernetesVersion="1.28"

 

 

 

 

 

3. 리소스 삭제

# 첫번째 클러스터 삭제
# CLB는 terraform으로 배포하지 않고 직접 수동으로 배포되었으니, 수동으로 삭제를 해주어야 함
helm uninstall kube-ops-view --namespace kube-system

# 클러스터 삭제
terraform destroy -auto-approve

# 두번째 클러스터 삭제
terraform destroy -auto-approve -var=ClusterBaseName=myeks2 -var=KubernetesVersion="1.28