zaki work log

作業ログやら生活ログやらなんやら

[Kubernetes] Helm getting started

2020.11追記:
使用するリポジトリについて、以前はドキュメントにも載っていた https://kubernetes-charts.storage.googleapis.com がサービス終了し、新しく https://charts.helm.sh/stable を使用するようになっています。
Helmバージョン3.4.1からは旧リポジトリは追加不可、追加済みの場合は警告が出るようになります。


マニフェスト管理ツールの一つであるHelm (version 3)に入門してみた。
まずはツールのインストールと、Helmを使ったアプリケーションのデプロイをQuickstart Guideの内容に沿って実施。

helm.sh

環境

今回の環境は以下の通り

作業はクラスタに対する操作権限のあるmasterノードで実施。

Helmのインストール

インストールのドキュメントに沿って導入すればOK

helm.sh

macOS/Windows/LinuxそれぞれHomebrew/Chocolatey/Snapでインストールできる。
手元の環境は素のCentOS 7でyumしか使ってないので、スクリプトでインストールした。

$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh
[zaki@k8s-master01 work]$ which helm
/usr/bin/which: no helm in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/zaki/.local/bin:/home/zaki/bin)
[zaki@k8s-master01 work]$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
[zaki@k8s-master01 work]$ chmod 700 get_helm.sh 
[zaki@k8s-master01 work]$ ./get_helm.sh 
Downloading https://get.helm.sh/helm-v3.2.2-linux-amd64.tar.gz
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
[zaki@k8s-master01 work]$ 
[zaki@k8s-master01 work]$ which helm
/usr/local/bin/helm
[zaki@k8s-master01 work]$ helm version
version.BuildInfo{Version:"v3.2.2", GitCommit:"a6ea66349ae3015618da4f547677a14b9ecc09b3", GitTreeState:"clean", GoVersion:"go1.13.12"}

Helmを使ったアプリケーションのデプロイ

リポジトリ設定

例として、公式のHelm Chartリポジトリを登録する。
(※ 2020.11.13でこのリポジトリは使用できなくなっています。最新(Helm 3.4.1時点)ではhttps://charts.helm.sh/stableを使用する。 ドキュメント参照 )

[zaki@k8s-master01 work]$ helm repo add stable https://kubernetes-charts.storage.googleapis.com/
"stable" has been added to your repositories

実行すると~/.config/helm/repositories.yamlに設定内容が記録される。

apiVersion: ""
generated: "0001-01-01T00:00:00Z"
repositories:
- caFile: ""
  certFile: ""
  insecure_skip_tls_verify: false
  keyFile: ""
  name: stable
  password: ""
  url: https://kubernetes-charts.storage.googleapis.com/
  username: ""

generatedの時刻は…あれ?

Chart検索

mysqlで検索

[zaki@k8s-master01 work]$ helm search repo mysql
NAME                                    CHART VERSION   APP VERSION     DESCRIPTION                                       
stable/mysql                            1.6.4           5.7.30          Fast, reliable, scalable, and easy to use open-...
stable/mysqldump                        2.6.0           2.4.1           A Helm chart to help backup MySQL databases usi...
stable/prometheus-mysql-exporter        0.5.3           v0.11.0         A Helm chart for prometheus mysql exporter with...
stable/percona                          1.2.1           5.7.26          free, fully compatible, enhanced, open source d...
stable/percona-xtradb-cluster           1.0.4           5.7.19          free, fully compatible, enhanced, open source d...
stable/phpmyadmin                       4.3.5           5.0.1           DEPRECATED phpMyAdmin is an mysql administratio...
stable/gcloud-sqlproxy                  0.6.1           1.11            DEPRECATED Google Cloud SQL Proxy                 
stable/mariadb                          7.3.14          10.3.22         DEPRECATED Fast, reliable, scalable, and easy t...

podのデプロイ

インストール前にキャッシュされているリポジトリ情報をアップデートする。(apt updateみたいな感じ)

[zaki@k8s-master01 work]$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈ Happy Helming!⎈ 

アップデートしたら、例としてmysqlをインストール(apt update後にapt installする感じ)

[zaki@k8s-master01 work]$ helm install stable/mysql --generate-name
NAME: mysql-1591568057
LAST DEPLOYED: Mon Jun  8 07:14:19 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1591568057.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-1591568057 -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql-1591568057 -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/mysql-1591568057 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
[zaki@k8s-master01 work]$ 
[zaki@k8s-master01 work]$ kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
mysql-1591568057-65fc75cc87-6skwb   0/1     Pending   0          20s

おや。。

[zaki@k8s-master01 work]$ kubectl get event
LAST SEEN   TYPE      REASON              OBJECT                                   MESSAGE
78s         Warning   FailedScheduling    pod/mysql-1591568057-65fc75cc87-6skwb    running "VolumeBinding" filter plugin for pod "mysql-1591568057-65fc75cc87-6skwb": pod has unbound immediate PersistentVolumeClaims
78s         Normal    SuccessfulCreate    replicaset/mysql-1591568057-65fc75cc87   Created pod: mysql-1591568057-65fc75cc87-6skwb
9s          Normal    FailedBinding       persistentvolumeclaim/mysql-1591568057   no persistent volumes available for this claim and no storage class is set
78s         Normal    ScalingReplicaSet   deployment/mysql-1591568057              Scaled up replica set mysql-1591568057-65fc75cc87 to 1

なるほど、ストレージは用意してない。
(先にRookの準備をしておけば良かった…)

pvの追加

[zaki@k8s-master01 work]$ kubectl get pvc
NAME               STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-1591568057   Pending                                                     2m43s

中身はこんな感じ

spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi
  volumeMode: Filesystem

暫定でhostpathのpvを作成

kind: PersistentVolume
apiVersion: v1
metadata:
  name: hostpath-pv
spec:
  capacity:
    storage: 100Gi
  accessModes:
  - ReadWriteOnce
  hostPath:
    path: /var/tmp/pv
[zaki@k8s-master01 work]$ kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
mysql-1591568057-65fc75cc87-6skwb   1/1     Running   0          7m50s

動きました。
(DBの設定は省略)

install状態

[zaki@k8s-master01 work]$ helm ls
NAME                    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
mysql-1591568057        default         1               2020-06-08 07:14:19.400555066 +0900 JST deployed        mysql-1.6.4     5.7.30  

install時の情報

[zaki@k8s-master01 work]$ helm status mysql-1591568057
NAME: mysql-1591568057
LAST DEPLOYED: Mon Jun  8 07:14:19 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1591568057.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-1591568057 -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql-1591568057 -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/mysql-1591568057 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}

削除

helm installでインストールしたpodはhelm uninstallで削除する。

[zaki@k8s-master01 work]$ kubectl get deploy,pod
NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mysql-1591568057   1/1     1            1           15m

NAME                                    READY   STATUS    RESTARTS   AGE
pod/mysql-1591568057-65fc75cc87-6skwb   1/1     Running   0          15m
[zaki@k8s-master01 work]$ helm ls
NAME                    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
mysql-1591568057        default         1               2020-06-08 07:14:19.400555066 +0900 JST deployed        mysql-1.6.4     5.7.30    
[zaki@k8s-master01 work]$ helm uninstall mysql-1591568057
release "mysql-1591568057" uninstalled
[zaki@k8s-master01 work]$ kubectl get deploy,pod
NAME                                    READY   STATUS        RESTARTS   AGE
pod/mysql-1591568057-65fc75cc87-6skwb   1/1     Terminating   0          15m

備考

(用語)chartとrelease

  • chart(Helm用のパッケージ)とrelease(パッケージからデプロイされたリソース一式)
  • イメージとコンテナ
  • クラスとオブジェクト

みたいに解釈したけど合ってるよね

helm search repoとhelm search hub

OpenShift

Helm 3はOpenShift 4.4でもGA Supportしてる。

access.redhat.com


インターネットに接続できるって素晴らしい。