현재 Pod(cotainer)가 실행되면서 CPU 값이 급등하게 증가하게 되면 자동으로 Pod를 Scaling 하고자합니다. 즉, Pod에 부하가 발생시 Pod를 자동으로 Auto Scaling을 구현하도록 합니다.
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl get deployment metrics-server -n kube-system
또는
kubectl top pods -n kube-system
#수정합니다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
namespace: wsi-skills-namespace-2
labels:
app: backend-deployment
spec:
replicas: 2
selector:
matchLabels:
app: backend-deployment
template:
metadata:
labels:
app: backend-deployment
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: worker2 #Node Key
operator: In
values:
- backend #Node Value
containers:
- name: backend-dockerimages
image: [[DockerImage]]
ports:
- containerPort: 8080
resources: #이 부분을 꼭 지정해줘야합니다
requests:
cpu: "150m"
limits:
cpu: "250m"
kubectl apply -f deployment.yaml
kubectl get pods [[PodFullName]] --namespace=wsi-skills-namespace-2 -o yaml | grep resources -C 8
cat << EOF > hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: backend-end-hpa-k8s
namespace: wsi-skills-namespace-2 #Namespace를 지정해줘야합니다 중요!!!!
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: backend-deployment
minReplicas: 2 #Pod minSize
maxReplicas: 5 #Pod MaxSize
targetCPUUtilizationPercentage: 30
EOF
kubectl apply -f hpa.yaml
kubectl get hpa -w -n wsi-skills-namespace
sudo yum install -y httpd-tools
#이 테스트는 internal 기존에 부하 시키기
ab -c 200 -n 200 -t 30 http://$(kubectl get ingress/[[ingressName]] -o jsonpath='{.status.loadBalancer.ingress[*].hostname}')/대상그룹HealthCheckPath
#이 테스트는 리다이렉션해서 부하시키기
while true;do curl -L wsi-match-alb-1805030163.ap-northeast-2.elb.amazonaws.com/?type=test; sleep 0; done
7. Pod의 갯수를 확인합니다. Pod에 갯수가 늘어나는 것을 확인할 수 있습니다.
kubectl get deployment backend-deployment --namespace=wsi-skills-namespace
kubectl get hpa
kubectl delete hpa [[HPAName]]