ArgoCDでhelmを使う方法と既存のReleaseをArgoCD管理へ移行する方法

akichan
14 min readApr 12, 2020

--

motivation

  • ArgoCDでhelm chartをデプロイする方法を試したことがなかった
  • 現在手動で helm install しているリソースの管理をArgoCDに移行したい

ArgoCDでhelmの基本

ArgoCDでhelmを使う方法に関する公式のドキュメントはこちら。

できること

  • 複数のvaluesファイルを渡す
  • ArgoCDのApplicationのパラメータでvalueを上書き
  • リリース名の変更
  • helm.sh/hookをargocd hookにマッピングして処理
  • パラメータ置換に環境変数を利用できる

helm使うにあたり困ることはなさそう。

実験

helmでデプロイする場合

比較のためにまずは普通にhelmでデプロイしてみる。stable/wordpress chartを使う。

$ helm install wordpress-helm stable/wordpress -n install-by-helm
$ helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
wordpress-helm install-by-helm 1 2020–04–12 09:57:03.250342 +0900 JST deployed wordpress-9.0.3 5.3.2

このリリース情報はhelm v3では同じnamespaceにsecretとして保存されている。

$ k get secret -l owner=helm
NAME TYPE DATA AGE
sh.helm.release.v1.wordpress-helm.v1 helm.sh/release.v1 1 99s

ArgoCDでデプロイする場合

argocd コマンドで stable/wordpress chart の applicationを作成。

argocd app create wordpress \
--repo https://kubernetes-charts.storage.googleapis.com \
--helm-chart wordpress \
--revision 9.0.3 \
--dest-namespace install-by-argocd \
--dest-server https://kubernetes.default.svc

argocd app list
NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH SYNCPOLICY CONDITIONS REPO PATH TARGET
wordpress https://kubernetes.default.svc install-by-argocd default Synced Progressing <none> <none> https://kubernetes-charts.storage.googleapis.com 9.0.3

deploymentやserviceのラベルに argo.argoproj.io/instanceラベルが付与されている。ArgoCDはこれで自分の管理下のどのApplicationのリソースか判別している。

k -n install-by-argocd get deploy wordpress -o json | jq ‘.metadata[“labels”]’
{
“app.kubernetes.io/instance”: “wordpress”,
“app.kubernetes.io/managed-by”: “Tiller”,
“app.kubernetes.io/name”: “wordpress”,
“argocd.argoproj.io/instance”: “wordpress”,
“helm.sh/chart”: “wordpress-9.0.3”
}

ArgoCDでは内部で単純に helm installしているわけではなく、 helm template でmanifestを生成し、必要なラベルを付与してmanifestを適用している。そのため、リリース情報はsecretsとして登録されておらず、次のように確認することができない。

$ k get secret -n install-by-argocd -l owner=helm
No resources found in install-by-argocd namespace.

$ helm -n install-by-argocd ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION

だからと言ってArgoCDでchartのバージョンアップなどのロールバックなどが行えない訳ではない。ArgoCD側のロールバック機能がある。ArgoCDの変更の記録はApplicationに記録されている。

$ argocd app history wordpress
ID DATE REVISION
0 2020-04-12 10:22:57 +0900 JST 9.0.3
1 2020-04-12 10:49:14 +0900 JST 9.0.2
### 実際には Application中に記録されている
$ k get application wordpress -n argocd -o json | jq '.status["history"]'
[
{
"deployedAt": "2020-04-12T01:22:57Z",
"id": 0,
"revision": "9.0.3",
"source": {
"chart": "wordpress",
"repoURL": "https://kubernetes-charts.storage.googleapis.com",
"targetRevision": "9.0.3"
}
},
{
"deployedAt": "2020-04-12T01:49:14Z",
"id": 1,
"revision": "9.0.2",
"source": {
"chart": "wordpress",
"repoURL": "https://kubernetes-charts.storage.googleapis.com",
"targetRevision": "9.0.2"
}
}
]

ロールバックは次のように実行する。

### rolleback実行
$ argocd app rollback wordpress 0
(---- 省略 ----)
### historyからREVISONが 9.0.2 -> 9.0.3 になったことがわかる
$ argocd app history wordpress
ID DATE REVISION
0 2020-04-12 10:22:57 +0900 JST 9.0.3
1 2020-04-12 10:49:14 +0900 JST 9.0.2
2 2020-04-12 11:05:18 +0900 JST 9.0.3

helmでinstallしたものをArgoCD管理にする

wordpress namespaceへ helm でインストール

$ helm install wordpress stable/wordpress -n wordpress$ helm ls -n wordpress
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
wordpress wordpress 1 2020-04-12 11:11:18.616532 +0900 JST deployed wordpress-9.0.3 5.3.2

wordpressnamespaceへ同じchart, release nameでAplicationを作成。

$ argocd app create wordpress \
--repo https://kubernetes-charts.storage.googleapis.com \
--helm-chart wordpress \
--revision 9.0.3 \
--dest-namespace wordpress \
--dest-server https://kubernetes.default.svc
application 'wordpress' created

上記のApplicationはAuto syncが有効でないのでまだsyncされていない。diffを確認するとラベルの変更がされることがわかる。argocd.argocdproj.io/instance ラベルの追加と、 heritage ラベルが変更される。

$ argocd app diff wordpress
===== /Secret wordpress/wordpress-mariadb ======
3,4c3,4
< mariadb-password: ++++++++++++
< mariadb-root-password: ++++++++++++
— -
> mariadb-password: ++++++++
> mariadb-root-password: ++++++++
8a9
> argocd.argoproj.io/instance: wordpress
10c11
< heritage: Helm
— -
> heritage: Tiller
===== /ConfigMap wordpress/wordpress-mariadb-tests ======
8a9,10
> labels:
> argocd.argoproj.io/instance: wordpress
===== /Service wordpress/wordpress-mariadb ======
5a6
> argocd.argoproj.io/instance: wordpress
8c9
< heritage: Helm
— -
> heritage: Tiller
===== /Service wordpress/wordpress ======

( — — 省略 — — )

ここで困った問題が発生。statefulset は特定のパラメータの変更しか許可されていないので、ラベルを追加しようとしても失敗してしまう。

apps   StatefulSet            wordpress  wordpress-mariadb        OutOfSync  Healthy            StatefulSet.apps "wordpress-mariadb" is invalid: spec: Forbidden: updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden

ワークアラウンドで、不整合にならないようstatefulsetで作成されるpvcにラベルを付けた後、statefulsetの定義のみ削除して再度syncを実行することで statusがsuccessになった。

### statefulsetの定義のみ削除
$ k delete statefulset wordpress-mariadb --cascade=false
statefulset.apps "wordpress-mariadb" deleted
$ k get po
NAME READY STATUS RESTARTS AGE
wordpress-59ff647944-nvxz6 1/1 Running 0 12m
wordpress-mariadb-0 1/1 Running 0 22m
$ k get pvc -L heritage
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE HERITAGE
data-wordpress-mariadb-0 Bound pvc-79d7b244-d195-4e09-ad43-adc8a67bfd31 8Gi RWO standard 22m Tiller
### sync
$ argocd app sync wordpress
(---- 省略 ----)
Name: wordpress
Project: default
Server: https://kubernetes.default.svc
Namespace: wordpress
URL: https://localhost:8080/applications/wordpress
Repo: https://kubernetes-charts.storage.googleapis.com
Target: 9.0.3
Path:
SyncWindow: Sync Allowed
Sync Policy: <none>
Sync Status: Synced to 9.0.3
Health Status: Progressing
Operation: Sync
Sync Revision: 9.0.3
Phase: Succeeded
Start: 2020-04-12 11:34:45 +0900 JST
Finished: 2020-04-12 11:34:46 +0900 JST
Duration: 1s
Message: successfully synced (all tasks run)
GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE
Secret wordpress wordpress Synced secret/wordpress unchanged
Secret wordpress wordpress-mariadb Synced secret/wordpress-mariadb unchanged
ConfigMap wordpress wordpress-mariadb Synced configmap/wordpress-mariadb unchanged
ConfigMap wordpress wordpress-mariadb-tests Synced configmap/wordpress-mariadb-tests unchanged
PersistentVolumeClaim wordpress wordpress Synced Healthy persistentvolumeclaim/wordpress unchanged
Service wordpress wordpress-mariadb Synced Healthy service/wordpress-mariadb unchanged
Service wordpress wordpress Synced Progressing service/wordpress unchanged
apps Deployment wordpress wordpress Synced Healthy deployment.apps/wordpress configured
apps StatefulSet wordpress wordpress-mariadb Synced Healthy statefulset.apps/wordpress-mariadb created

argocdで変更が加わってもhelmからはそれは追従できない。

$ argocd app history wordpress
ID DATE REVISION
0 2020-04-12 11:34:46 +0900 JST 9.0.3
1 2020-04-12 11:52:05 +0900 JST 9.0.2

$ helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
wordpress wordpress 1 2020-04-12 11:11:18.616532 +0900 JST deployed wordpress-9.0.3 5.3.2

終わりに

  • ArgoCDでhelm chartをデプロイする時は helm template でmanifestを生成して適用される
  • 手動でhelm installしたものをArgoCD管理にすることは一応できた

--

--