"CKA SimA 05 Kustomize로 HPA 오토스케일러 설정"의 두 판 사이의 차이

(새 문서: ==개요== ;Kustomize를 사용하여 HorizontalPodAutoscaler 구성 ==사전작업== <syntaxhighlight lang="bash"> mkdir -p /tmp/hpa-demo/{base,staging,prod} </syntaxhighlight> ==b...)
 
 
(같은 사용자의 중간 판 9개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
== 개요 ==
;Kustomize를 사용하여 HorizontalPodAutoscaler 구성
;kustomize를 활용한 HPA 구성
* HPA를 base 레벨에 정의하고, overlay(staging/prod)에서 조건별로 오버라이드하는 방식 설명
* ConfigMap 기반 수평 확장을 제거하고 HPA 리소스를 도입하여 CPU 기반 자동 확장 설정
* 환경별로 최대 복제본 수(maxReplicas)를 다르게 설정


==사전작업==
== 디렉터리 구조 ==
<syntaxhighlight lang="bash">
* <code>base</code>: 공통 리소스 정의 (Deployment, ServiceAccount, HPA 기본값)
mkdir -p /tmp/hpa-demo/{base,staging,prod}
* <code>staging</code>: base를 상속하며 네임스페이스 및 일부 속성 수정
* <code>prod</code>: base를 상속하며 HPA maxReplicas 확장
 
<syntaxhighlight lang="console" highlight="1">
# tree /opt/course/5/api-gateway
/opt/course/5/api-gateway
├── base
│  ├── api-gateway.yaml
│  └── kustomization.yaml
├── staging
│  ├── api-gateway.yaml
│  └── kustomization.yaml
└── prod
    ├── api-gateway.yaml
    └── kustomization.yaml
</syntaxhighlight>
</syntaxhighlight>


==base 디렉터리 구성==
== base 구성 ==
<syntaxhighlight lang="yaml" title="/tmp/hpa-demo/base/kustomization.yaml">
<syntaxhighlight lang="yaml" title="/opt/course/5/api-gateway/base/kustomization.yaml">
apiVersion: kustomize.config.k8s.io/v1beta1
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
kind: Kustomization


resources:
resources:
   - hpa-demo.yaml
   - api-gateway.yaml
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="yaml" title="/tmp/hpa-demo/base/hpa-demo.yaml">
<syntaxhighlight lang="yaml" title="/opt/course/5/api-gateway/base/api-gateway.yaml">
apiVersion: autoscaling/v2
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
kind: HorizontalPodAutoscaler
metadata:
metadata:
   name: hpa-demo
   name: api-gateway
spec:
spec:
   scaleTargetRef:
   scaleTargetRef:
     apiVersion: apps/v1
     apiVersion: apps/v1
     kind: Deployment
     kind: Deployment
     name: hpa-demo
     name: api-gateway
   minReplicas: 2
   minReplicas: 2
   maxReplicas: 4
   maxReplicas: 4
39번째 줄: 56번째 줄:
kind: ServiceAccount
kind: ServiceAccount
metadata:
metadata:
   name: hpa-demo
   name: api-gateway
---
---
apiVersion: apps/v1
apiVersion: apps/v1
kind: Deployment
kind: Deployment
metadata:
metadata:
   name: hpa-demo
   name: api-gateway
spec:
spec:
  replicas: 1
   selector:
   selector:
     matchLabels:
     matchLabels:
       app: hpa-demo
       id: api-gateway
   template:
   template:
     metadata:
     metadata:
       labels:
       labels:
         app: hpa-demo
         id: api-gateway
     spec:
     spec:
       serviceAccountName: hpa-demo
       serviceAccountName: api-gateway
       containers:
       containers:
         - name: web
         - name: httpd
           image: nginx:1.25-alpine
           image: httpd:2-alpine
</syntaxhighlight>
</syntaxhighlight>


==staging 오버레이 구성==
== staging 오버레이 ==
<syntaxhighlight lang="yaml" title="/tmp/hpa-demo/staging/kustomization.yaml">
<syntaxhighlight lang="yaml" title="/opt/course/5/api-gateway/staging/kustomization.yaml">
apiVersion: kustomize.config.k8s.io/v1beta1
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
kind: Kustomization
69번째 줄: 87번째 줄:


patches:
patches:
   - path: hpa-demo-patch.yaml
   - path: api-gateway.yaml


transformers:
transformers:
77번째 줄: 95번째 줄:
     metadata:
     metadata:
       name: staging-ns-transformer
       name: staging-ns-transformer
     namespace: hpa-demo-staging
     namespace: api-gateway-staging
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="yaml" title="/tmp/hpa-demo/staging/hpa-demo-patch.yaml">
<syntaxhighlight lang="yaml" title="/opt/course/5/api-gateway/staging/api-gateway.yaml">
apiVersion: apps/v1
apiVersion: apps/v1
kind: Deployment
kind: Deployment
metadata:
metadata:
   name: hpa-demo
   name: api-gateway
   labels:
   labels:
     env: staging
     env: staging
</syntaxhighlight>
</syntaxhighlight>


==prod 오버레이 구성==
== prod 오버레이 ==
<syntaxhighlight lang="yaml" title="/tmp/hpa-demo/prod/kustomization.yaml">
<syntaxhighlight lang="yaml" title="/opt/course/5/api-gateway/prod/kustomization.yaml">
apiVersion: kustomize.config.k8s.io/v1beta1
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
kind: Kustomization
98번째 줄: 116번째 줄:


patches:
patches:
   - path: hpa-demo-patch.yaml
   - path: api-gateway.yaml


transformers:
transformers:
106번째 줄: 124번째 줄:
     metadata:
     metadata:
       name: prod-ns-transformer
       name: prod-ns-transformer
     namespace: hpa-demo-prod
     namespace: api-gateway-prod
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="yaml" title="/tmp/hpa-demo/prod/hpa-demo-patch.yaml">
<syntaxhighlight lang="yaml" title="/opt/course/5/api-gateway/prod/api-gateway.yaml">
apiVersion: autoscaling/v2
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
kind: HorizontalPodAutoscaler
metadata:
metadata:
   name: hpa-demo
   name: api-gateway
spec:
spec:
   maxReplicas: 6
   maxReplicas: 6
120번째 줄: 138번째 줄:
kind: Deployment
kind: Deployment
metadata:
metadata:
   name: hpa-demo
   name: api-gateway
   labels:
   labels:
     env: prod
     env: prod
</syntaxhighlight>
</syntaxhighlight>


==적용==
== 적용 ==
<syntaxhighlight lang="bash">
<syntaxhighlight lang="console" highlight="1">
# 스테이징 환경 적용
# k kustomize /opt/course/5/api-gateway/staging | k apply -f -
kubectl kustomize /tmp/hpa-demo/staging | kubectl apply -f -
serviceaccount/api-gateway unchanged
deployment.apps/api-gateway unchanged
horizontalpodautoscaler.autoscaling/api-gateway created
</syntaxhighlight>


# 프로덕션 환경 적용
<syntaxhighlight lang="console" highlight="1">
kubectl kustomize /tmp/hpa-demo/prod | kubectl apply -f -
# k kustomize /opt/course/5/api-gateway/prod | k apply -f -
serviceaccount/api-gateway unchanged
deployment.apps/api-gateway unchanged
horizontalpodautoscaler.autoscaling/api-gateway created
</syntaxhighlight>
</syntaxhighlight>


==결과 확인==
== 결과 확인 ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console" highlight="1">
# 스테이징
# k -n api-gateway-staging get hpa api-gateway
kubectl -n hpa-demo-staging get hpa hpa-demo
NAME         REFERENCE              TARGETS  MINPODS  MAXPODS  REPLICAS  AGE
NAME       REFERENCE              TARGETS  MINPODS  MAXPODS  REPLICAS  AGE
api-gateway  Deployment/api-gateway  0%/50%    2        4        2          1m
hpa-demo    Deployment/hpa-demo  0%/50%    2        4        2          1m
</syntaxhighlight>


# 프로덕션
<syntaxhighlight lang="console" highlight="1">
kubectl -n hpa-demo-prod get hpa hpa-demo
# k -n api-gateway-prod get hpa api-gateway
NAME       REFERENCE              TARGETS  MINPODS  MAXPODS  REPLICAS  AGE
NAME         REFERENCE              TARGETS  MINPODS  MAXPODS  REPLICAS  AGE
hpa-demo    Deployment/hpa-demo  0%/50%    2        6        2          1m
api-gateway  Deployment/api-gateway  0%/50%    2        6        2          1m
</syntaxhighlight>
</syntaxhighlight>


==같이 보기==
== 마무리 정리 ==
* [[kubectl apply]]
* 기존 ConfigMap <code>horizontal-scaling-config</code>은 더 이상 사용되지 않으므로 수동 삭제 필요
<syntaxhighlight lang="console" highlight="1">
# k -n api-gateway-staging delete cm horizontal-scaling-config
configmap "horizontal-scaling-config" deleted
</syntaxhighlight>
 
<syntaxhighlight lang="console" highlight="1">
# k -n api-gateway-prod delete cm horizontal-scaling-config
configmap "horizontal-scaling-config" deleted
</syntaxhighlight>
 
* HPA가 관리하는 Deployment의 <code>replicas:</code> 설정은 HPA가 덮어쓰기 때문에 제거 가능
 
== 같이 보기 ==
* [[CKA 2025]]
* [[HorizontalPodAutoscaler]]
* [[kubectl kustomize]]
* [[kubectl kustomize]]
* [[HorizontalPodAutoscaler]]
* [[kustomization.yaml]]
* [[Kustomize 오버레이 구조]]
* [[Kustomize 오버레이 구조]]
* [[kubectl apply]]
== 참고 ==
* https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
* https://cloudutsuk.com/posts/certification/cka/cka-pratice-test-1/#question-5--kustomize-configure-hpa-autoscaler


[[분류:Kustomize]]
[[분류:Kustomize]]
[[분류:HPA]]
[[분류:HPA]]
[[분류:CKA]]
[[분류:CKA 2025]]

2025년 6월 21일 (토) 12:51 기준 최신판

1 개요[ | ]

kustomize를 활용한 HPA 구성
  • HPA를 base 레벨에 정의하고, overlay(staging/prod)에서 조건별로 오버라이드하는 방식 설명
  • ConfigMap 기반 수평 확장을 제거하고 HPA 리소스를 도입하여 CPU 기반 자동 확장 설정
  • 환경별로 최대 복제본 수(maxReplicas)를 다르게 설정

2 디렉터리 구조[ | ]

  • base: 공통 리소스 정의 (Deployment, ServiceAccount, HPA 기본값)
  • staging: base를 상속하며 네임스페이스 및 일부 속성 수정
  • prod: base를 상속하며 HPA maxReplicas 확장
# tree /opt/course/5/api-gateway
/opt/course/5/api-gateway
├── base
│   ├── api-gateway.yaml
│   └── kustomization.yaml
├── staging
│   ├── api-gateway.yaml
│   └── kustomization.yaml
└── prod
    ├── api-gateway.yaml
    └── kustomization.yaml

3 base 구성[ | ]

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - api-gateway.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-gateway
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-gateway
  minReplicas: 2
  maxReplicas: 4
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 50
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: api-gateway
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      id: api-gateway
  template:
    metadata:
      labels:
        id: api-gateway
    spec:
      serviceAccountName: api-gateway
      containers:
        - name: httpd
          image: httpd:2-alpine

4 staging 오버레이[ | ]

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - ../base

patches:
  - path: api-gateway.yaml

transformers:
  - |-
    apiVersion: builtin
    kind: NamespaceTransformer
    metadata:
      name: staging-ns-transformer
    namespace: api-gateway-staging
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-gateway
  labels:
    env: staging

5 prod 오버레이[ | ]

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - ../base

patches:
  - path: api-gateway.yaml

transformers:
  - |-
    apiVersion: builtin
    kind: NamespaceTransformer
    metadata:
      name: prod-ns-transformer
    namespace: api-gateway-prod
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-gateway
spec:
  maxReplicas: 6
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-gateway
  labels:
    env: prod

6 적용[ | ]

# k kustomize /opt/course/5/api-gateway/staging | k apply -f -
serviceaccount/api-gateway unchanged
deployment.apps/api-gateway unchanged
horizontalpodautoscaler.autoscaling/api-gateway created
# k kustomize /opt/course/5/api-gateway/prod | k apply -f -
serviceaccount/api-gateway unchanged
deployment.apps/api-gateway unchanged
horizontalpodautoscaler.autoscaling/api-gateway created

7 결과 확인[ | ]

# k -n api-gateway-staging get hpa api-gateway
NAME          REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
api-gateway   Deployment/api-gateway  0%/50%    2         4         2          1m
# k -n api-gateway-prod get hpa api-gateway
NAME          REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
api-gateway   Deployment/api-gateway  0%/50%    2         6         2          1m

8 마무리 정리[ | ]

  • 기존 ConfigMap horizontal-scaling-config은 더 이상 사용되지 않으므로 수동 삭제 필요
# k -n api-gateway-staging delete cm horizontal-scaling-config
configmap "horizontal-scaling-config" deleted
# k -n api-gateway-prod delete cm horizontal-scaling-config
configmap "horizontal-scaling-config" deleted
  • HPA가 관리하는 Deployment의 replicas: 설정은 HPA가 덮어쓰기 때문에 제거 가능

9 같이 보기[ | ]

10 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}