カスタムなHELMチャートを開発して、Kubernetesクラスタへインストールするために、ローカルのリポジトリを利用してテストする手順を確認したメモです。
Kubectl コマンドを動作環境を設置アップ
helmをセットアップするには、kubectl コマンドが、クラスタと接続できること、管理者権限があることが必要です。
imac:helm maho$ kubectl cluster-info
Kubernetes master is running at https://c1.jp-tok.containers.cloud.ibm.com:23972
KubeDNS is running at https://c1.jp-tok.containers.cloud.ibm.com:23972/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kubernetes-dashboard is running at https://c1.jp-tok.containers.cloud.ibm.com:23972/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy
Metrics-server is running at https://c1.jp-tok.containers.cloud.ibm.com:23972/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Helm コマンドのセットアップ
kubectl のセットアップが出来ていれば、helm init で k8s クラスタへインストールできます。
imac:helm maho$ helm init
$HELM_HOME has been configured at /Users/maho/.helm.
Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.
Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
To prevent this, run `helm init` with the --tiller-tls-verify flag.
For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation
Happy Helming!
次のコマンドで、クライアントとサーバー側のバージョンを確認します。
imac:helm maho$ helm version
Client: &version.Version{SemVer:"v2.12.3", GitCommit:"eecf22f77df5f65c823aacd2dbd30ae6c65f186e", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.12.3", GitCommit:"eecf22f77df5f65c823aacd2dbd30ae6c65f186e", GitTreeState:"clean"}
しかし、このhelm init
だけではアクセス権限が足りないので、動作しません。
imac:helm maho$ helm list
Error: configmaps is forbidden: User "system:serviceaccount:kube-system:default" cannot list resource "configmaps" in API group "" in the namespace "kube-system"
tiller サービスアカウントを作成して、アクセス権限を追加します。クラウドのドキュメントを参考にして、権限を追加します。
imac:helm maho$ kubectl apply -f https://raw.githubusercontent.com/IBM-Cloud/kube-samples/master/rbac/serviceaccount-tiller.yaml
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
サービスアカウント tiller で再設定すると、アクセスできるようになります。
imac:helm maho$ helm init --service-account tiller --upgrade
$HELM_HOME has been configured at /Users/maho/.helm.
Tiller (the Helm server-side component) has been upgraded to the current version.
Happy Helming!
helm の動作を確認してみます。今度はエラーが出ません。
imac:helm maho$ helm list
ローカル環境に、HELMチャートのテンプレートを作成
ここからが本題です。
myaplという名前で、カレントディレクトリに、チャートのテンプレートを作成する。
imac:helm maho$ helm create myapl
Creating myapl
作成したテンプレートには以下のディレクトリとファイルが生成される。
imac:helm maho$ tree myapl
myapl
├── Chart.yaml チャートの情報を含むYAMLファイル
├── charts このチャートが依存するチャートが置かれる
├── templates テンプレートのYAMLファイルを配置、Valueの変数と組み合わせて反映される
│ ├── NOTES.txt インストール時に表示する使い方を表示
│ ├── _helpers.tpl
│ ├── deployment.yaml テンプレートYAMLのマニフェスト
│ ├── ingress.yaml 同上
│ ├── service.yaml 同上
│ └── tests
│ └── test-connection.yaml
└── values.yaml 変数YAMLファイル
上記のテンプレートYAMLと変数YAMLを編集して、カスタムなHELMチャートを作成する。
helmコマンドのローカル・リポジトリへ登録する
チャートのアーカイブファイルを、カレントディレクトリへ書き出す。
imac:helm maho$ helm package myapl
Successfully packaged chart and saved it to: /Users/maho/work3/helm/myapl-0.1.0.tgz
ローカルリポジトリ用のディレクトリを作成する。
imac:helm maho$ mkdir charts
作成しておいたチャートのアーカイブを、ローカルリポジトリのディレクトリへ移動する。
imac:helm maho$ mv myapl-0.1.0.tgz charts/
helm のローカル・リポジトリサーバーを起動
helmコマンドで、serveオプションに、リポジトリのパスを指定して、リポジトリサーバーを起動します。この起動によってindex.yamlが作成されます。このindex.yamlはリポジトリのインデックスで、複数のチャート(tgz)を持つことができます。
imac:helm maho$ helm serve --repo-path ./charts/
Regenerating index. This may take a moment.
Now serving you on 127.0.0.1:8879
チャートのアーカイブを追加した時は、以下のコマンドを実行して、index.yaml を更新する。
imac:helm maho$ helm repo index charts
ローカル・リポジトリを更新した場合は、毎回、helm repo add を実行してリフレッシュする。
コマンドの引数は、helm repo add リポジトリ名 リポジトリURL
です。
imac:helm maho$ helm repo add local http://127.0.0.1:8879/charts
"local" has been added to your repositories
リポジトリのリストを表示するには、次のコマンドを実行する。
imac:helm maho$ helm repo list
NAME URL
stable https://kubernetes-charts.storage.googleapis.com
ibm https://registry.bluemix.net/helm/ibm
ibm-charts https://registry.bluemix.net/helm/ibm-charts
kubernetes https://kubernetes-charts.storage.googleapis.com
local http://127.0.0.1:8879/charts
リポジトリのチャートを検索
チャートの検索コマンドの引数は、helm search [REPO NAME/] [CHART NAME]
です。
全リポジトリから検索するには、次のコマンドを実行する。
imac:helm maho$ helm search myapl
NAME CHART VERSION APP VERSION DESCRIPTION
local/myapl 0.1.0 1.0 A Helm chart for Kubernetes
リポジトリ名ローカルから検索するには、次のように リポジトリ名の末尾にスラッシュを付加する。
imac:helm maho$ helm search local/
NAME CHART VERSION APP VERSION DESCRIPTION
local/myapl 0.1.0 1.0 A Helm chart for Kubernetes
ローカルのチャートをk8sクラスタへインストール
ローカルを表すリポジトリ名を指定して、k8sクラスタへインストールします。チャートのインストールの引数は、helm install REPO_NAME/CHART_NAME [-n RELEASE_NAME]
です。 RELEASE_NAMEを付与しなければ、自動的に名前が付与されます。
imac:helm maho$ helm install local/myapl -n myapl1
NAME: myapl1
LAST DEPLOYED: Sun Apr 14 23:00:18 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapl1 ClusterIP 172.21.78.75 <none> 80/TCP 0s
==> v1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
myapl1 1 1 1 0 0s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
myapl1-8469dccc77-dw2vn 0/1 ContainerCreating 0 0s
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=myapl,app.kubernetes.io/instance=myapl1" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:80
リリースされたチャートをリストするには、次のコマンドを実行します。
imac:helm maho$ helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
myapl1 1 Sun Apr 14 23:00:18 2019 DEPLOYED myapl-0.1.0 1.0 default
リリースのテスト
チャートのインストール時に表示されたメッセージを参考に、ポートフォワードを実行します。このコマンドはフォアグラウンドで実行するので、テストするにはターミナルをもう一つ開くか、ブラウザからアクセスします。
imac:helm maho$ export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=myapl,app.kubernetes.io/instance=myapl1" -o jsonpath="{.items[0].metadata.name}")
imac:helm maho$ kubectl port-forward $POD_NAME 8080:80
次は、テンプレートチャートのアクセステストの実行例です。
imac:helm maho$ curl http://localhost:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>