K8S 스케일링 요약
1. 스케일링이란?
트래픽 부하에 따라 컴퓨팅 자원(Pod 또는 Node)의 수를 늘리거나 줄이는 작업.
스케일링 방향
| 종류 |
방식 |
도구 |
| 수평적 (Scale-out) |
Pod 개수 증가 |
HPA |
| 수직적 (Scale-up) |
Pod 사양(CPU/Memory) 증가 |
VPA |
오토스케일링 종류
| 종류 |
대상 |
작동 원리 |
| HPA |
Pod 개수 |
CPU/메모리가 설정치 초과 시 Pod 복제 |
| VPA |
Pod 자원량 |
CPU/메모리 Request 값 최적화 |
| CA (Cluster Autoscaler) |
Node 개수 |
자원 부족 시 실제 서버 추가 |
HPA 작동 과정
- Metrics Server가 각 Pod의 CPU/메모리 실시간 수집
- 설정 기준값과 현재 사용량 비교
- 기준 초과 시 Deployment replicas 자동 증가
- 새 Pod로 트래픽 분산
2. HPA 작업 순서
1단계: Metrics Server 정상화
- 기존 잔재 삭제 → 공식 components.yaml 설치
- EKS 필수 패치:
--kubelet-insecure-tls 옵션 추가
- 보안 그룹: 4443, 10250 포트 개방
2단계: 애플리케이션 배포
resources.requests.cpu: 100m 반드시 설정 (미설정 시 HPA 미작동)
- EKS 안정 이미지:
public.ecr.aws 계열 사용
3단계: HPA 설정 및 노드 확장
kubectl autoscale deployment deployment-2048 --cpu-percent=3 --min=2 --max=5
eksctl scale nodegroup --cluster free-vpc-cluster --name standard-nodes --nodes 2
4단계: 부하 테스트 및 모니터링
# 부하 발생
kubectl run -i --tty load-generator --rm --image=busybox:1.28 -- \
/bin/sh -c "while true; do wget -q -O- http://service-2048; done"
# 모니터링
kubectl get hpa -w
kubectl get pods -o wide -w
3. EKS 생성 및 실습
EKS 클러스터 생성 (eks-freetier-setup.yaml)
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: free-vpc-cluster
region: ap-northeast-2
version: "1.34"
vpc:
nat:
gateway: Single
managedNodeGroups:
- name: standard-nodes
instanceType: t3.small
minSize: 1
maxSize: 2
desiredCapacity: 1
privateNetworking: true
iam:
withAddonPolicies:
imageBuilder: true
autoScaler: true
addons:
- name: vpc-cni
- name: coredns
- name: kube-proxy
Metrics Server 설치 및 패치
# 설치
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# 패치 (인증서 무시 + InternalIP 설정)
kubectl patch deployment metrics-server -n kube-system --type='json' -p='[{"op": "replace",
"path": "/spec/template/spec/containers/0/args", "value": [
"--cert-dir=/tmp",
"--secure-port=4443",
"--kubelet-preferred-address-types=InternalIP",
"--kubelet-use-node-status-port",
"--metric-resolution=15s",
"--kubelet-insecure-tls"
]}]'
보안 그룹 4443 포트 개방
export NODE_SG=$(aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
"Name=tag:kubernetes.io/cluster/free-vpc-cluster,Values=owned" \
--query "Reservations[0].Instances[0].SecurityGroups[0].GroupId" \
--output text)
aws ec2 authorize-security-group-ingress \
--group-id $NODE_SG --protocol tcp --port 4443 --source-group $NODE_SG
4. 2048 앱 배포
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-2048
spec:
replicas: 1
selector:
matchLabels:
app: game-2048
template:
metadata:
labels:
app: game-2048
spec:
containers:
- name: container-2048
image: public.ecr.aws/l6m2t8p7/docker-2048:latest
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
limits:
cpu: 200m
kubectl expose deployment deployment-2048 --port=80 --target-port=80 --name=service-2048
kubectl autoscale deployment deployment-2048 --cpu-percent=2 --min=1 --max=10
5. 최종 체크리스트
6. 삭제
# 현재 네임스페이스 리소스 삭제
kubectl delete all --all
kubectl delete pvc,configmap,ingress --all
# Metrics Server 삭제
kubectl delete deployment metrics-server -n kube-system
# EKS 클러스터 전체 삭제
eksctl delete cluster --name free-vpc-cluster