CentOS Stream 10
Sponsored Link

Kubernetes : Horizontal Pod Autoscaler の設定2025/01/27

 

Horizontal Pod Autoscaler を設定すると、特定の Pod で、設定した CPU/メモリー 使用率を超えた場合に、自動的に Pod のレプリカ数を増やすことができます。

当例では以下のように 4 台のノードを使用して Kubernetes クラスターを構成しています。

+----------------------+   +----------------------+
|  [ ctrl.srv.world ]  |   |   [ dlp.srv.world ]  |
|     Manager Node     |   |     Control Plane    |
+-----------+----------+   +-----------+----------+
        eth0|10.0.0.25             eth0|10.0.0.30
            |                          |
------------+--------------------------+-----------
            |                          |
        eth0|10.0.0.51             eth0|10.0.0.52
+-----------+----------+   +-----------+----------+
| [ node01.srv.world ] |   | [ node02.srv.world ] |
|     Worker Node#1    |   |     Worker Node#2    |
+----------------------+   +----------------------+

[1]

こちらを参考に、Metrics Server の設定を適用しておきます

[2] Horizontal Pod Autoscaler を設定した Deployment の例です。
[cent@ctrl ~]$
vi my-nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: my-nginx
  name: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: my-nginx
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - image: nginx
        name: my-nginx
        resources:
          # requests : pod 作成時に必要とするリソースを設定
          requests:
            # 250m : 0.25 CPU
            cpu: 250m
            memory: 64Mi
          # 使用可能なリソースの上限を設定
          limits:
            cpu: 500m
            memory: 128Mi

[cent@ctrl ~]$
vi hpa.yml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-nginx-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    # 適用対象の Deployment 名
    name: my-nginx
  minReplicas: 1
  # レプリカの最大数
  maxReplicas: 4
  metrics:
  - type: Resource
    resource:
      # CPU 使用率が 20% を超えたらスケーリング
      name: cpu
      target:
        type: Utilization
        averageUtilization: 20

[cent@ctrl ~]$
kubectl apply -f my-nginx.yml -f hpa.yml

deployment.apps/my-nginx created
horizontalpodautoscaler.autoscaling/my-nginx-hpa created

[cent@ctrl ~]$
kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-5496ddfbbb-xjzmt   1/1     Running   0          7s

[cent@ctrl ~]$
kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-5496ddfbbb-xjzmt   0m           17Mi

[cent@ctrl ~]$
kubectl get hpa

NAME           REFERENCE             TARGETS       MINPODS   MAXPODS   REPLICAS   AGE
my-nginx-hpa   Deployment/my-nginx   cpu: 0%/20%   1         4         1          91s

# 対象の pod に適当に負荷をかけて再度確認

[cent@ctrl ~]$
kubectl get hpa

NAME           REFERENCE             TARGETS        MINPODS   MAXPODS   REPLICAS   AGE
my-nginx-hpa   Deployment/my-nginx   cpu: 50%/20%   1         4         4          5m43s

# 設定通り pod が複製されている

[cent@ctrl ~]$
kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-5496ddfbbb-hrn7d   1/1     Running   0          102s
my-nginx-5496ddfbbb-l45wv   1/1     Running   0          102s
my-nginx-5496ddfbbb-xjzmt   1/1     Running   0          5m57s
my-nginx-5496ddfbbb-zsv88   1/1     Running   0          102s

[cent@ctrl ~]$
kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-5496ddfbbb-hrn7d   0m           7Mi
my-nginx-5496ddfbbb-l45wv   0m           17Mi
my-nginx-5496ddfbbb-xjzmt   0m           18Mi
my-nginx-5496ddfbbb-zsv88   0m           7Mi
関連コンテンツ