1. 기본 환경 배포
이번 실습은 IAM 사용자 계정을 통해 관리 콘솔에 접근하고 액세스 키를 활용해 awscli 도구를 사용합니다.
해당 작업을 수행하지 않았다면 아래 토글을 확장해 작업을 선행하고 본격적인 실습에 들어갑니다.
IAM 사용자 생성 및 액세스 키 생성
1.1. Terraform을 통한 기본 인프라 배포
Terraform을 통한 기본 인프라 배포에 앞서 SSH 키 페어, IAM User Access Key ID, IAM User Secret Access Key를 미리 확인하고 메모해 둡니다.
Terraform으로 기본 인프라 배포
cd cnaee_class_tf/ch2
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 접근 확인
2. 데모 애플리케이션 배포 및 확인
2.1. ingress_before 배포 및 확인
데모 애플리케이션 다운로드 및 확인
wget https://github.com/cloudneta/cnaeelab/raw/master/_data/demo_ch2_2.zip
unzip demo_ch2_2.zip
Bash
복사
# 실습 파일 다운로드 및 압축 해제
cd demo_ch2_2
tree
Bash
복사
# 대상 경로 진입 및 확인
apt install python3-pip -y
pip install boto3
pip install kubernetes
Bash
복사
# Python 스크립트를 위한 도구 설치
데모 애플리케이션 배포(ingress_before)
kubectl apply -f ingress_before.yaml
Bash
복사
# ingress_before.yaml 파일 배포
kubectl get pods,service,cm,deploy,ingress
Bash
복사
# pods, service, configmap, deployment, ingress 확인
watch -d "aws elbv2 describe-load-balancers --output text --query 'LoadBalancers[*].[LoadBalancerName, State.Code]'"
Bash
복사
# [신규 터미널] ALB 상태 모니터링
Web 접속 스크립트 확인
chmod +x *.sh
Bash
복사
# 스크립트 실행 권한 부여
./set_env.sh
Bash
복사
# ALB 주소 변수화 스크립트
./req_access.sh
Bash
복사
# Web 접속 스크립트 (1회) - HTTPCode, TotalTime, PodIP를 출력
while true; do ./req_access.sh; sleep 0.1; done
Bash
복사
# Web 접속 스크립트 (무한) - 0.1초 마다
모니터링 설정
cd demo_ch2_2
while true; do ./req_access.sh; sleep 0.1; done
Bash
복사
# [신규 터미널1] Web 접속 스크립트 (무한)
apt install ngrep -y
Bash
복사
# [신규 터미널2] HTTP Code 확인 (200 OK 제외)
ngrep -d any -W byline port 80 | awk -v datecmd='date "+%Y-%m-%d %H:%M:%S.%2N"' '
/HTTP\/1.1/ {
cmd = datecmd; cmd | getline timestamp; close(cmd)
gsub(/\r/,"",$2); gsub("/", "", $2);
if ($2 != "200" && $2 != "")
print timestamp, $2
}'
Bash
복사
aws elbv2 describe-target-groups --query 'TargetGroups[*].TargetGroupArn' --output text
export ELB_TG_ARN=$(aws elbv2 describe-target-groups --query 'TargetGroups[*].TargetGroupArn' --output text)
Bash
복사
# [신규 터미널3] ALB Taget Group - IP, State
python3 demo_ch2_2/tg.py
Bash
복사
python3 demo_ch2_2/pod.py
Bash
복사
# [신규 터미널4] 파드 정보 모니터링
[Before] Pod Rolling Update
kubectl rollout restart deploy demo-web
Bash
복사
# replicas 3에서 rolling update
Note:
Pod Rollout 상태에서 모니터링 정보를 통해 트래픽 유실을 확인합니다.
[Before] Pod Scale-In
kubectl scale deployment demo-web --replicas 1
Bash
복사
# Deployment Replicas 수정 (3->1)
Note:
Pod Scale-In 상태에서 모니터링 정보를 통해 트래픽 유실을 확인합니다.
[Before] Pod Rolling Update
kubectl rollout restart deploy demo-web
Bash
복사
# replicas 1에서 rolling update
Note:
Pod Lifecycle를 확인합니다.
2.2. ingress_after 배포 및 확인
데모 애플리케이션 배포(ingress_after)
cat ingress_after.yaml
Bash
복사
# ingress_after.yaml 파일 확인
kubectl apply -f ingress_after.yaml
Bash
복사
# ingress_after.yaml 파일 배포
Note:
기존 구성 환경에서 변경된 내용만 반영하여 Deployment만 수정됩니다.
[After] Pod Rolling Update
kubectl rollout restart deploy demo-web
Bash
복사
# replicas 3에서 rolling update
Note:
Pod Rollout 상태에서 모니터링 정보를 통해 트래픽 유실을 확인합니다.
[After] Pod Scale-In
kubectl scale deployment demo-web --replicas 1
Bash
복사
# Deployment Replicas 수정 (3->1)
Note:
Pod Scale-In 상태에서 모니터링 정보를 통해 트래픽 유실을 확인합니다.
[After] Pod Rolling Update
kubectl rollout restart deploy demo-web
Bash
복사
# replicas 1에서 rolling update
Note:
Pod Lifecycle를 확인합니다.
3. 실습 환경 삭제
실습 환경 삭제와 Terraform 삭제 작업을 진행합니다.
3.1. 실습 자원 삭제
실습 애플리케이션 삭제
kubectl delete -f ingress_after.yaml
Bash
복사
# 데모 애플리케이션 삭제
3.2. Terraform 삭제
Terraform 자원 삭제
nohup sh -c "terraform destroy -auto-approve" > delete.log 2>&1 &
Bash
복사
# terraform 자원 삭제
Warning:
nohup으로 백그라운드로 실행하도록 변경했습니다.
Terraform 삭제가 완료되면 정상적으로 자원 삭제 되었는지 확인을 합니다.(cat delete.log)
여기까지 2장의 두 번째 실습인 “Ingress ALB 환경에서 트래픽 유실 없는 서비스 하기” 실습을 마칩니다.
수고하셨습니다 :)