Notice
Recent Posts
Recent Comments
«   2025/10   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

ITFragile

[Kubernetes] 06. Web server HPA 구성 본문

Project

[Kubernetes] 06. Web server HPA 구성

경요 2023. 3. 22. 17:17

 

불필요한 리소스 낭비를 줄이고 파드 개수를 리소스 사용량을 기준으로 자동으로 관리하기 위해

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개로 변경되었다