Helmを使う

公開されたChartを使用してアプリケーションをデプロイする

前述したように、HelmではChartという設定ファイルに基づいてアプリケーションをデプロイする。Chartはhelmコマンドを実行するローカルマシン上に用意したものだけでなく、リポジトリで公開されているChartを直接ダウンロードして利用することもできる。まずはHelmによるアプリケーションデプロイの例として、リポジトリで公開されているChartを利用する方法を見ていく。

すでに述べたように、公開されたChartはArtifact Hubというサイトで検索できる。Artifact Hubでは<リポジトリ名>/<Chart名>/という形式でChartが表示されており、その中でリポジトリ名が「stable」や「incubator」になっているものはHelmの公式リポジトリで提供されているChartで、リポジトリ名がそれ以外のものはサードパーティが提供しているChartである。同じ名前のChartが別のリポジトリで公開されている場合もあるが、それらはソースが異なる点に注意する必要がある。

リポジトリ経由でChartをインストールするには、まずChartを提供するリポジトリをローカルコンピュータに登録する必要がある。Helmをインストールした直後はどのリポジトリも登録されていない状態なので、公式リポジトリで提供されているChartを利用する場合でもリポジトリの追加作業が必要になる。

リポジトリの追加はhelm repo add <リポジトリ名> <URL>コマンドで実行できる。たとえば公式のbitnamiリポジトリを追加する場合は、次のように実行する。

% helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories

なお、Linux環境では使用するリポジトリの設定はホームディレクトリ配下の.config/helmというディレクトリにあり、リポジトリで公開されているChart一覧のキャッシュはホームディレクトリ配下の.cache/helm/repositoryディレクトリに保存される。これらの情報はhelm -hコマンドで表示できるhelmコマンドのヘルプにも記載されているので、必要に応じて確認しておく。

% helm -h
The Kubernetes package manager

... 中略 ...

| Operating System | Cache Path                | Configuration Path             | Data Path               |
|------------------|---------------------------|--------------------------------|-------------------------|
| Linux            | $HOME/.cache/helm         | $HOME/.config/helm             | $HOME/.local/share/helm |
| macOS            | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm      |
| Windows          | %TEMP%\helm               | %APPDATA%\helm                 | %APPDATA%\helm          |

... 以下省略 ...

リポジトリの登録後は、helm searchコマンドでリポジトリに登録されているChartを検索できる。

helm search repo [<検索キーワード>]

キーワードを省略すると、登録されているリポジトリで提供されているすべてのChartが出力される。

% helm search repo
NAME                                        	CHART VERSION	APP VERSION  	DESCRIPTION
bitnami/airflow                             	13.1.5       	2.3.4        	Apache Airflow is a tool to express and execute...
bitnami/apache                              	9.2.3        	2.4.54       	Apache HTTP Server is an open-source HTTP serve...
bitnami/argo-cd                             	4.1.3        	2.4.11       	Argo CD is a continuous delivery tool for Kuber...

... 以下省略 ...

登録済みリポジトリの情報はローカルにキャッシュされるため、時間が経つと古くなることがある。helm repo updateコマンドを実行すると、リポジトリキャッシュを最新の状態に更新できる。

% helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈

Helmでは、Chartを使用してアプリケーションをデプロイすることを「install(インストール)」と呼び、インストールされたアプリケーションのインスタンスを「Release(リリース)」と呼ぶ。また、各インスタンスには固有の名前(Release名)がある。Release名が重複しなければ、同じChartから複数のReleaseを作成することもできる。

リポジトリで公開されたChartを使用してアプリケーションをインストール(デプロイ)するには、helm install <Release名> <Chart名>コマンドを実行する。

次の例は、bitnami/wordpressというChartをwordpressというRelease名でデプロイしたものである。デプロイに成功すると、そのアプリケーションに関するメモなどを含むNOTESが表示される。

% helm install wordpress bitnami/wordpress
NAME: wordpress
LAST DEPLOYED: Thu Sep 15 00:33:19 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: wordpress
CHART VERSION: 15.2.2
APP VERSION: 6.0.2

** Please be patient while the chart is being deployed **

Your WordPress site can be accessed through the following DNS name from within your cluster:

    wordpress.default.svc.cluster.local (port 80)

To access your WordPress site from outside the cluster follow the steps below:

1. Get the WordPress URL by running these commands:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace default -w wordpress'

   export SERVICE_IP=$(kubectl get svc --namespace default wordpress --include "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}")
   echo "WordPress URL: http://$SERVICE_IP/"
   echo "WordPress Admin URL: http://$SERVICE_IP/admin"

2. Open a browser and access WordPress using the obtained URL.

3. Login with the following credentials below to see your blog:

  echo Username: user
  echo Password: $(kubectl get secret --namespace default wordpress -o jsonpath="{.data.wordpress-password}" | base64 -d)

デプロイは現在のKubernetesコンテキストに対して実行されるため、特定のnamespaceを使用したい場合は、事前にkubectl configコマンドなどでコンテキストを設定しておく。

また、このbitnami/wordpress Chartでは永続化ストレージを利用するため、クラスタ内でデフォルトのStorage Classが設定されている必要がある。Storage Classの設定方法は使用しているKubernetesクラスタによって異なるため、Kubernetes用Storage Classの作成については別途確認してほしい。

デプロイされたRelease一覧はhelm listコマンドで確認できる。

% helm list
NAME     	NAMESPACE	REVISION	UPDATED                             	STATUS  	CHART           	APP VERSION
wordpress	default  	1       	2022-09-15 00:33:19.166716 +0900 KST	deployed	wordpress-15.2.2	6.0.2

ここで作成されたPodをkubectl get podsコマンドで確認すると、次のようにwordpress-d5c8dffb7-bws4dwordpress-mariadb-0の2つのPodが作成されていることがわかる。環境によってd5c8dffb7-bws4dの部分は変わる。

% kubectl get pods
NAME                        READY   STATUS             RESTARTS        AGE
wordpress-d5c8dffb7-bws4d   0/1     CrashLoopBackOff   5 (2m21s ago)   7m33s
wordpress-mariadb-0         1/1     Running            0               7m33s

… 作成中(Podにエラーが発生して作成中止) …