ITFragile
[Kubernetes] 06. Web server HPA 구성 본문
불필요한 리소스 낭비를 줄이고 파드 개수를 리소스 사용량을 기준으로 자동으로 관리하기 위해
deployment에 hpa 구성 설정을 해준다.
1. hpa 생성
# hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-webserver
labels:
app: myweb
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myweb
minReplicas: 2 # 최소 파드 개수 2개
maxReplicas: # 최대 파드 개수 10개
targetCPUUtilizationPercentage: 50 # cpu의 평균 사용량 50% 기준으로 제한/생성
이때 cpu 사용량 기준으로 파드가 생성/삭제되므로 디플로이먼트에 리소스를 추가해준다.
2. deployment 수정
# dep-web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myweb
spec:
selector:
matchLabels:
app: myweb
replicas: 2
template:
metadata:
labels:
app: myweb
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: web
topologyKey: "kubernetes.io/hostname"
containers:
- name: webserver
image: docker.io/kyungyyyo/mywebphp:v3
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
resources:
requests:
memory: "100Mi"
cpu: "50m"
limits:
memory: "1Gi"
cpu: "200m"
volumeMounts:
- name: cm-vol
mountPath: /var/www/html/index.php
subPath: index.php
- name: cm-vol2
mountPath: /var/www/html/index.html
subPath: index.html
volumes:
- name: cm-vol
configMap:
name: cmfile
- name: cm-vol2
configMap:
name: cmfile2
kubectl apply -f hpa.yaml
kubectl apply -f dep-web.yaml
3. 확인 테스트
CPU 사용량 증가시켜 확인해보기
kubectl exec myweb-5f5d68bd4b-gdbjh -- dd if=/dev/zero of=/tmp/fileA
# tmp/fileA(없는 경로)를 반복해서 삭제
CPU 사용량이 201%로, 기준인 50%을 초과하여 파드가 8개로 증가되었다.
명령어 중지 후 일정 시간 지난 뒤 확인해보자.
68%로 cpu 줄어들었으나 그 이상 떨어지지 않았다.
명렁어는 중지했으나 ps -ef로 동작중인 프로세스 확인시 동작하고 있음을 확인해서 kill로 프로세스 강제 종료시켰다.
kill -9 16
이때 kill 후 접속한 파드에서 나와야 scale out된 파드들이 다시 원복됨을 주의!!
다시 최소 레플리카수인 2개로 변경되었다
'Project' 카테고리의 다른 글
[Kubernetes] 05. Web-DB 연결을 위한 컨피그맵 수정 (0) | 2023.03.22 |
---|---|
[Kubernetes] 04. Deployment로 web server 인그레스 구성 (0) | 2023.03.22 |
[Kubernetes] 03. Docker image build하기 (0) | 2023.03.21 |
[Kubernetes] 02. DB StatefulSet으로 이중화 구성 (0) | 2023.03.21 |
[Kubernetes] 01. 작업 환경 구성 (0) | 2023.03.20 |