1. 기본 환경 배포
이번 실습은 IAM 사용자 계정을 통해 관리 콘솔에 접근하고 액세스 키를 활용해 awscli 도구를 사용합니다.
해당 작업을 수행하지 않았다면 아래 토글을 확장해 작업을 선행하고 본격적인 실습에 들어갑니다.
IAM 사용자 생성 및 액세스 키 생성
1.1. Terraform을 통한 기본 인프라 배포
Terraform을 통한 기본 인프라 배포에 앞서 SSH 키 페어, IAM User Access Key ID, IAM User Secret Access Key를 미리 확인하고 메모해 둡니다.
Terraform으로 기본 인프라 배포
cd cnaee_class_tf/ch1
Bash
복사
# 실습 디렉터리 경로 진입
export TF_VAR_KeyName=[각자 ssh keypair]
export TF_VAR_MyIamUserAccessKeyID=[각자 iam 사용자의 access key id]
export TF_VAR_MyIamUserSecretAccessKey=[각자 iam 사용자의 secret access key]
export TF_VAR_SgIngressSshCidr=$(curl -s ipinfo.io/ip)/32
Bash
복사
# Terraform 환경 변수 저장
terraform init
terraform plan
Bash
복사
# Terraform 배포
nohup sh -c "terraform apply -auto-approve" > create.log 2>&1 &
Bash
복사
Note:
nohup으로 백그라운드로 실행하도록 변경했습니다.
Terraform 배포가 완료되면 정상적으로 자원 생성이 되었는지 확인을 합니다.(cat create.log)
Terraform을 통한 기본 인프라 배포가 완료되면 관리 콘솔에서 생성된 인프라들을 확인합니다.
Note:
AWS 관리 콘솔에 로그인 할 땐 IAM 사용자 계정으로 진행합니다.
1.2. 기본 정보 확인 및 설정
Terraform 배포가 완료 후 출력되는 Output 정보에서 bastion_host_ip의 퍼블릭 IP를 확인합니다.
해당 IP로 EKS 관리용 인스턴스(myeks-bastion-EC2)에 SSH로 접속하고 아래 명령어를 통해 정보를 확인합니다.
Note:
myeks-bastion-EC2의 OS 변경으로 SSH 접근에 대한 계정을 ubuntu로 지정합니다.
(ssh -i ~/.ssh/XXXX.pem ubuntu@X.X.X.X)
기본 설정 및 확인
aws eks update-kubeconfig \
--region $AWS_DEFAULT_REGION \
--name $CLUSTER_NAME
Shell
복사
# EKS 클러스터 인증 정보 업데이트
kubens default
Bash
복사
# kubens default 설정
echo $AWS_DEFAULT_REGION
echo $CLUSTER_NAME
echo $VPCID
echo $PublicSubnet1,$PublicSubnet2,$PublicSubnet3
echo $PrivateSubnet1,$PrivateSubnet2,$PrivateSubnet3
Bash
복사
# 변수 호출 종합
eksctl get cluster
Bash
복사
# eksctl을 통한 eks cluster 정보 확인
eksctl get nodegroup \
--cluster $CLUSTER_NAME \
--name ${CLUSTER_NAME}-node-group
Bash
복사
# eksctl을 통한 노드 그룹 정보 확인
kubectl get node -owide
Bash
복사
# kubectl을 통한 노드 정보 확인
PublicN1=$(kubectl get node --label-columns=topology.kubernetes.io/zone --selector=topology.kubernetes.io/zone=ap-northeast-2a -o jsonpath={.items[0].status.addresses[0].address})
PublicN2=$(kubectl get node --label-columns=topology.kubernetes.io/zone --selector=topology.kubernetes.io/zone=ap-northeast-2b -o jsonpath={.items[0].status.addresses[0].address})
PublicN3=$(kubectl get node --label-columns=topology.kubernetes.io/zone --selector=topology.kubernetes.io/zone=ap-northeast-2c -o jsonpath={.items[0].status.addresses[0].address})
echo "export PublicN1=$PublicN1" >> /etc/profile
echo "export PublicN2=$PublicN2" >> /etc/profile
echo "export PublicN3=$PublicN3" >> /etc/profile
echo $PublicN1, $PublicN2, $PublicN3
Bash
복사
# 노드 IP 변수 선언
for node in $PublicN1 $PublicN2 $PublicN3; \
do \
ssh -i ~/.ssh/kp_node.pem -o StrictHostKeyChecking=no ec2-user@$node hostname; \
done
Bash
복사
# 노드에 ssh 접근 확인
MyDomain=[각자 도메인]; echo $MyDomain
echo "export MyDomain=$MyDomain" >> /etc/profile
Bash
복사
# 각자의 도메인 변수 선언
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 = http://kubeopsview.$MyDomain:8080/#scale=1.5"
Bash
복사
# kube-ops-view 설치
wget -O eks-node-viewer https://github.com/awslabs/eks-node-viewer/releases/download/v0.7.1/eks-node-viewer_Linux_x86_64
chmod +x eks-node-viewer
sudo mv -v eks-node-viewer /usr/local/bin
eks-node-viewer
Bash
복사
# [신규 터미널] eks-node-viewer 접속
2. AWS Fargate Profile 생성 및 확인
Fargate IAM Role 생성 및 IAM Policy 연결
cat << EOT > fargatepolicy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn:aws:eks:$AWS_DEFAULT_REGION:$ACCOUNT_ID:fargateprofile/$CLUSTER_NAME/*"
}
},
"Principal": {
"Service": "eks-fargate-pods.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOT
Bash
복사
# fargatepolicy.json 생성
aws iam create-role \
--role-name AmazonEKSFargatePodExecutionRole \
--assume-role-policy-document file://"fargatepolicy.json"
Bash
복사
# AmazonEKSFargatePodExecutionRole 생성
aws iam attach-role-policy \
--policy-arn arn:aws:iam::aws:policy/AmazonEKSFargatePodExecutionRolePolicy \
--role-name AmazonEKSFargatePodExecutionRole
Bash
복사
# 생성한 IAM Role에 AmazonEKSFargatePodExecutionRolePolicy 연결
2.1. 첫 번째 Fargate Profile 생성 및 확인
첫 번째 Fargate Profile로 fp-pod를 생성하고 신규 파드를 생성해서 동작을 확인합니다.
첫 번째 Fargate Profile 생성 및 확인
aws eks create-fargate-profile \
--fargate-profile-name fp-pod \
--cluster-name ${CLUSTER_NAME} \
--pod-execution-role-arn arn:aws:iam::${ACCOUNT_ID}:role/AmazonEKSFargatePodExecutionRole \
--selectors namespace=default,labels={env=fargate} \
--subnets ${PrivateSubnet1} ${PrivateSubnet2} ${PrivateSubnet3}
Bash
복사
# fp-pod fargate profile 생성
eksctl get fargateprofile \
--cluster $CLUSTER_NAME \
--name fp-pod -o yaml
Bash
복사
# fp-pod fargate profile 확인
while true; \
do \
eksctl get fargateprofile \
--cluster $CLUSTER_NAME \
--name fp-pod -o yaml | grep status; date; sleep 1; \
done
Bash
복사
# [신규 터미널] fargate profile 모니터링 (fp-pod)
Note:
관리 콘솔에 접근해서 대상 EKS 클러스터를 선택 후 컴퓨팅 탭에서 Fargate Profile을 확인합니다.
신규 파드 3대 생성
cat <<EoF> nginx1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx1
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx1
template:
metadata:
labels:
app: nginx1
env: ng
spec:
containers:
- image: nginx
name: nginx1
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 500m
memory: 512Mi
EoF
Bash
복사
# nginx1.yaml 파일 생성
kubectl create -f nginx1.yaml
Bash
복사
# nginx1.yaml - 실습 자원 생성 (파드 3대)
kubectl describe pod nginx1 | grep scheduler
Bash
복사
# nginx1 파드의 Scheduler 확인
신규 파드 3대 생성(2)
cat <<EoF> nginx2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx2
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx2
template:
metadata:
labels:
app: nginx2
env: fargate
spec:
containers:
- image: nginx
name: nginx2
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 500m
memory: 512Mi
EoF
Bash
복사
# nginx2.yaml 파일 생성
kubectl create -f nginx2.yaml
Bash
복사
# nginx2.yaml - 실습 자원 생성 (파드 3대)
kubectl describe pod nginx2 | grep scheduler
Bash
복사
# nginx2 파드의 Scheduler 확인
Fargate Pod ENI 확인
kubectl get pod -owide
Bash
복사
# 파드 정보 확인 (owide)
FARGATE_IPS=($(kubectl get nodes -o wide | grep fargate | awk '{print $6}'))
for i in "${!FARGATE_IPS[@]}"; do
declare "FargateN$((i+1))=${FARGATE_IPS[i]}"
done
echo "FargateN1: $FargateN1"
echo "FargateN2: $FargateN2"
echo "FargateN3: $FargateN3"
Bash
복사
# Fargate 인스턴스 IP 주소 추출
aws ec2 describe-network-interfaces \
--filters "Name=private-ip-address,Values=$FargateN1,$FargateN2,$FargateN3" \
--query "NetworkInterfaces[*].[NetworkInterfaceId, PrivateIpAddress, OwnerId, RequesterId]" \
--output table
Bash
복사
# 대상 IP 주소에 대한 ENI 정보 (eni_id, ip, owner_id, requester_id)
2.2. 두 번째 Fargate Profile 생성 및 확인
두 번째 Fargate Profile로 fp-coredns를 생성하고 신규 파드를 생성해서 동작을 확인합니다.
두 번째 Fargate Profile 생성 및 확인
aws eks create-fargate-profile \
--fargate-profile-name fp-coredns \
--cluster-name ${CLUSTER_NAME} \
--pod-execution-role-arn arn:aws:iam::${ACCOUNT_ID}:role/AmazonEKSFargatePodExecutionRole \
--selectors namespace=kube-system,labels={k8s-app=kube-dns} \
--subnets ${PrivateSubnet1} ${PrivateSubnet2} ${PrivateSubnet3}
Bash
복사
# fp-coredns fargate profile 생성
eksctl get fargateprofile \
--cluster $CLUSTER_NAME \
--name fp-coredns -o yaml
Bash
복사
# fp-coredns fargate profile 확인
while true; \
do \
eksctl get fargateprofile \
--cluster $CLUSTER_NAME \
--name fp-coredns -o yaml | grep status; date; sleep 1; \
done
Bash
복사
# [신규 터미널] fargate profile 모니터링 (fp-coredns)
Note:
관리 콘솔에 접근해서 대상 EKS 클러스터를 선택 후 컴퓨팅 탭에서 Fargate Profile을 확인합니다.
CoreDNS 정보 확인
kubectl get deploy coredns -n kube-system
Bash
복사
# kube-system 네임스페이스에 coredns Deployment 확인
kubectl describe deploy coredns -n kube-system
Bash
복사
# Deployment에서 coredns 확인 (Labels를 보면 k8s-app=kube-dns 존재)
kubectl describe deploy coredns -n kube-system | sed -n '/Labels/,+1p'
Bash
복사
CoreDNS 재시작
kubectl rollout restart -n kube-system deployment coredns
Bash
복사
# CoreDNS Rollout
Note:
EKS Node Viewer와 kube-ops-view를 통해 배포된 CoreDNS 파드들을 확인합니다.
3. AWS Fargate Profile 삭제 및 확인
3.1. 첫 번째 Fargate Profile 삭제 및 확인
첫 번째 Fargate Profile 삭제 및 확인
aws eks delete-fargate-profile \
--cluster-name ${CLUSTER_NAME} \
--fargate-profile-name fp-pod
Bash
복사
# fp-pod fargate profile 삭제
while true; \
do \
eksctl get fargateprofile \
--cluster $CLUSTER_NAME \
--name fp-pod -o yaml | grep status; date; sleep 1; \
done
Bash
복사
# [신규 터미널] fargate profile 모니터링 (fp-pod)
Note:
EKS Node Viewer와 kube-ops-view를 통해 배포된 nginx1과 nginx2 파드들을 확인합니다.
3.2. 두 번째 Fargate Profile 삭제 및 확인
두 번째 Fargate Profile 삭제 및 확인
aws eks delete-fargate-profile \
--cluster-name ${CLUSTER_NAME} \
--fargate-profile-name fp-coredns
Bash
복사
# fp-coredns fargate profile 삭제
while true; \
do \
eksctl get fargateprofile \
--cluster $CLUSTER_NAME \
--name fp-coredns -o yaml | grep status; date; sleep 1; \
done
Bash
복사
# [신규 터미널] fargate profile 모니터링 (fp-coredns)
Note:
EKS Node Viewer와 kube-ops-view를 통해 배포된 CoreDNS 파드들을 확인합니다.
4. 실습 환경 삭제
실습 환경 삭제와 Terraform 삭제 작업을 진행합니다.
4.1. 실습 자원 삭제
실습 애플리케이션 삭제
kubectl delete deploy nginx1
kubectl delete deploy nginx2
Bash
복사
# Deployment nginx1과 nginx2 삭제
helm uninstall kube-ops-view -n kube-system
Bash
복사
# kube-ops-view 삭제
4.2. Terraform 삭제
Terraform 자원 삭제
nohup sh -c "terraform destroy -auto-approve" > delete.log 2>&1 &
Bash
복사
# terraform 자원 삭제
Warning:
nohup으로 백그라운드로 실행하도록 변경했습니다.
Terraform 삭제가 완료되면 정상적으로 자원 삭제 되었는지 확인을 합니다.(cat delete.log)
여기까지 1장의 두 번째 실습인 “Amazon EKS Cluster - AWS Fargate 구성하기” 실습을 마칩니다.
수고하셨습니다 :)