1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Helmを使って, k8s DeploymentをUpgrade/Rollbackしてみた

Posted at

本記事でやること

HelmのVersion 3を使って、k8sのResourceであるDeploymentのUpgrade/Rollbackを確認してみる。

環境

root@ras01:~# helm version
version.BuildInfo{Version:"v3.5.3", GitCommit:"041ce5a2c17a58be0fcd5f5e16fb3e7e95fea622", GitTreeState:"dirty", GoVersion:"go1.15.8"}

root@ras01:~# kubectl version
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"cb303e613a121a29364f75cc67d3d580833a7479", GitTreeState:"clean", BuildDate:"2021-04-08T16:31:21Z", GoVersion:"go1.16.1", Compiler:"gc", Platform:"linux/arm64"}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"cb303e613a121a29364f75cc67d3d580833a7479", GitTreeState:"clean", BuildDate:"2021-04-08T16:25:06Z", GoVersion:"go1.16.1", Compiler:"gc", Platform:"linux/arm64"}

実行

1. Helm Chartの作成

 最初に、Helm Chartを新規作成に以下、コマンドを実行する。

root@ras01:~# helm create deployment
Creating deployment

root@ras01:~# tree deployment
deployment
├── Chart.yaml
├── charts
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── service.yaml
│   ├── serviceaccount.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

 上記の構成を持ったChart deploymentが作成される。しかし、ややこしいのでtemplate配下のdeployment.yaml以外は削除する。

root@ras01:~# tree deployment/
deployment/
├── Chart.yaml
├── charts
├── templates
│   └── deployment.yaml
└── values.yaml

2. templateの定義

 以下のようにnginx:1.20.0のDeploymentを定義する。

deployment.yaml
cat deployment/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Release.Name }}
  name: {{ .Release.Name }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
      - image: nginx:1.20.0
        name: nginx
        resources: {}

3. Helm ChartのRelease

 以下のように、コマンドを実行し、先ほど定義したHelm Chartに基づいてdeploymentリリースをインストールして、デプロイする。

root@ras01:~# helm install deployment deployment/
NAME: deployment
LAST DEPLOYED: Wed Apr 21 21:49:58 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

root@ras01:~# helm list
NAME            NAMESPACE       REVISION        UPDATED                                       STATUS          CHART                   APP VERSION
deployment      default         1               2021-04-21 21:49:08.509322219 +0000 UTC       deployed        deployment-0.1.0        1.16.0

helm listから現在インストールされているRelaseの一覧を確認できる。

4. Helm ChartのUpgrade

 次に、nginx:1.20.0からnginx:latestのUpgradeを行う。先ほどと同じくdeployment/templates/deployment.yamlを編集する。

deployment.yaml
apiVersion: apps/v1
kind: Deployment
(省略)
    spec:
      containers:
      - image: nginx:latest
        name: nginx
        resources: {}

今回は、helm upgradeを用いて、Upgradeを行う。

root@ras01:~# helm upgrade deployment deployment/
Release "deployment" has been upgraded. Happy Helming!
NAME: deployment
LAST DEPLOYED: Wed Apr 21 21:52:08 2021
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None

root@ras01:~# helm list
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
deployment      default         2               2021-04-21 21:52:08.509847689 +0000 UTC deployed        deployment-0.1.0        1.16.0

root@ras01:~# k describe deploy deployment | grep Image
    Image:        nginx:latest

 上記のように、REVISON 2として、デプロイが行えたことがわかり、PodのImageがnginx:latestになっていることもわかる。
 helm historyのコマンドを実行すると、これまでデプロイしたReleaseのRevisonを確認することもできる。

root@ras01:~# helm history deployment
REVISION        UPDATED                         STATUS          CHART                   APP VERSION     DESCRIPTION
1               Wed Apr 21 21:49:58 2021        superseded      deployment-0.1.0        1.16.0          Install complete
2               Wed Apr 21 21:52:08 2021        deployed        deployment-0.1.0        1.16.0          Upgrade complete

5. Helm RollBack

デプロイ後、何かしらの理由(PodのErrorやCrashloopbackoff, pvcの未作成等)でエラーが発生した場合に、Rollbackを行うコマンドも用意されている。また任意のRevisionを指定して、Rollbackを行うことも可能である。

root@ras01:~# helm rollback deployment 1
Rollback was a success! Happy Helming!

root@ras01:~# helm history deployment
REVISION        UPDATED                         STATUS          CHART                   APP VERSION     DESCRIPTION
1               Wed Apr 21 21:49:58 2021        superseded      deployment-0.1.0        1.16.0          Install complete
2               Wed Apr 21 21:52:08 2021        superseded      deployment-0.1.0        1.16.0          Upgrade complete
3               Wed Apr 21 22:08:11 2021        deployed        deployment-0.1.0        1.16.0          Rollback to 1

root@ras01:~# k describe deploy deployment | grep Image
    Image:        nginx:1.20.0

最後に

不要になったReleaseを削除するために、忘れずにアンインストールを行う。

root@ras01:~# helm uninstall deployment
release "deployment" uninstalled
root@ras01:~# helm list
NAME    NAMESPACE       REVISION        UPDATED STATUS  CHART   APP VERSION

参考文献

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?