LoginSignup
5
1

More than 3 years have passed since last update.

HPAをArgo CDの管理対象としたときに発生するスケーリングループの回避方法

Last updated at Posted at 2021-01-11

はじめに

Horizontal Pod Autoscaler (HPA)をArgo CDの管理対象(GitOpsの対象)としたときに、replica数がループする現象が発生しました。
本記事では、そのトラブルの回避方法を紹介します。

環境情報

  • Kubernetes
    • Docker Desktop 2.3.0.4
    • v1.16.6
  • Argo CD v1.8.2

トラブル詳細

再現方法

以下マニフェストのようなdeploymentとHPAをArgo CDの管理対象とする。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: argocd-hpa
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
  namespace: argocd-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageValue: 1
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: nginx
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/nakkoh/argocd_hpa.git
    targetRevision: master
    path: nginx
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd-hpa

トラブルの内容

マニフェストのreplicasとHPAが競合してしまうので、deploymentのステータスがSync OKOutOfSyncを繰り替えし、その度にdeploymentのreplica数が1, 2に変化し続けます。

image.png

回避方法

以下の2通りの方法があります。

replicasを定義しない

以下のマニフェストのようにdeploymentのspec.replicasを定義しないことで、本トラブルを回避することができます。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: argocd-hpa
  labels:
    app: nginx
spec:
  # replicas: 1 ★ 定義しない
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

replicasだけArgo CDの管理対象から外す

以下applicationのマニフェストのようにspec.ignoreDifferencesを定義することでreplicasのみArgo CDの管理下から除外することで、本トラブルを回避できます。

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: nginx
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/nakkoh/argocd_hpa.git
    targetRevision: master
    path: nginx
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd_hpa
  ignoreDifferences:      # ★ 以下行を追加
  - group: apps           # 
    kind: Deployment      # 
    name: nginx           # 
    namespace: argocd-hpa # 
    jsonPointers:         # 
    - /spec/replicas      # 
syncPolicy:
  automated: {}

Helm chart使用時に本トラブルに遭遇した場合は、replicasを定義しないの方法が使えないことがあるため、replicasだけArgo CDの管理対象から外すの方法を使用する必要があります。

参考文献

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1