KubernetesのパッケージマネージャーHelmを使う

kubernetes

Kubernatesが操舵手なのに対して、Helmは舵。 パッケージはChart(海図)と呼ばれている。

ChartにはデフォルトでGoのtemplateで書かれたManifestが含まれ、values.yamlの値を -f values.yaml や –set key=value フラグで上書きして適用しインストールすることができる。

Helmコマンドをインストールする。 今回はminikubeに入れるので立ち上げる。

$ brew install kubernetes-helm
$ helm version
Client: &version.Version{SemVer:"v2.5.0", GitCommit:"012cb0ac1a1b2f888144ef5a67b8dab6c2d45be6", GitTreeState:"clean"}

# brew cask install virtualbox minikube
$ minikube version
minikube version: v0.20.0

$ minikube start
Kubectl is now configured to use the cluster.

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.2", GitCommit:"922a86cfcd65915a9b2f69f3f193b8907d741d9c", GitTreeState:"clean", BuildDate:"2017-07-21T19:06:19Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.4", GitCommit:"d6f433224538d4f9ca2f7ae19b252e6fcb66a3ae", GitTreeState:"dirty", BuildDate:"2017-06-22T04:31:09Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}

$ kubectl config current-context
minikube

まずk8sクラスタ上にHelmの管理サーバーTillerをインストールする必要がある。

追記(2020-03-11): Helm 3.0からTillerがなくなり helm init も不要になった

ついでにリポジトリをupdateする。

$ helm init
$ ls ~/.helm/
cache		plugins		repository	starters

$ helm repo update

searchでChartを検索する。

$ helm search mysql
NAME                  	VERSION	DESCRIPTION                                       
stable/mysql          	0.2.6  	Fast, reliable, scalable, and easy to use open-...
stable/percona        	0.2.0  	free, fully compatible, enhanced, open source d...
stable/gcloud-sqlproxy	0.1.0  	Google Cloud SQL Proxy                            
stable/mariadb        	0.6.3  	Fast, reliable, scalable, and easy to use open-...

inspectでChartの情報を見ることができる。— の上がChartの情報のChart.yamlで、下がvalues.yaml。

$ helm inspect stable/mysql
description: Fast, reliable, scalable, and easy to use open-source relational database
  system.
engine: gotpl
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
maintainers:
- email: [email protected]
  name: Vic Iglesias
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 0.2.6

---
## mysql image version
## ref: https://hub.docker.com/r/library/mysql/tags/
##
image: "mysql"
imageTag: "5.7.14"

## Specify password for root user
##
## Default: random 10 character string
# mysqlRootPassword: testing

## Create a database user
##
# mysqlUser:
# mysqlPassword:

## Allow unauthenticated access, uncomment to enable
##
# mysqlAllowEmptyPassword: true

## Create a database
##
# mysqlDatabase:

## Specify an imagePullPolicy (Required)
## It's recommended to change this to 'Always' if the image tag is 'latest'
## ref: http://kubernetes.io/docs/user-guide/images/#updating-images
##
imagePullPolicy: IfNotPresent

## Persist data to a persitent volume
persistence:
  enabled: true
  ## If defined, volume.beta.kubernetes.io/storage-class: <storageClass>
  ## Default: volume.alpha.kubernetes.io/storage-class: default
  ##
  # storageClass:
  accessMode: ReadWriteOnce
  size: 8Gi

## Configure resource requests and limits
## ref: http://kubernetes.io/docs/user-guide/compute-resources/
##
resources:
  requests:
    memory: 256Mi
    cpu: 100m

mysqlDatabaseの値を渡してinstallしてみる。 パスワードなどをを保持するSecretPersistent Volumes(PV)を要求するPersistentVolumeClaim(PVC)と ServiceとDeploymentが作成された。

$ echo '{mysqlDatabase: user0db}' > config.yaml
$ helm install -f config.yaml stable/mysql
...
RESOURCES:
==> v1/Secret
NAME                    TYPE    DATA  AGE
pioneering-hydra-mysql  Opaque  2     1s

==> v1/PersistentVolumeClaim
NAME                    STATUS  VOLUME                                    CAPACITY  ACCESSMODES  STORAGECLASS  AGE
pioneering-hydra-mysql  Bound   pvc-9649c097-7088-11e7-8dd5-0800270629d8  8Gi       RWO          standard      1s

==> v1/Service
NAME                    CLUSTER-IP  EXTERNAL-IP  PORT(S)   AGE
pioneering-hydra-mysql  10.0.0.216  <none>       3306/TCP  1s

==> v1beta1/Deployment
NAME                    DESIRED  CURRENT  UP-TO-DATE  AVAILABLE  AGE
pioneering-hydra-mysql  1        1        1           0          1s
$ kubectl get pods
NAME                                      READY     STATUS    RESTARTS   AGE
pioneering-hydra-mysql-3456603420-0pldk   1/1       Running   0          4m

生成されたSecretの値はsecretKeyRefで参照できる。

PVというのはクラスタにプロビジョニングされる、Podとは独立したライフサイクルを持つVolume。StorageClass、ReadWriteOnceのようなアクセスモード、容量を含むPVCでリクエストする。

$ kubectl get storageclasses
NAME                 TYPE
standard (default)   k8s.io/minikube-hostpath 

Secretからrootパスワードを取得し、他のpodから接続できるのとDBが作成されていることを確認する。

$ kubectl get secret --namespace default pioneering-hydra-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo
******

$ kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il
$ apt-get update && apt-get install mysql-client -y
$ mysql -h pioneering-hydra-mysql -p
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| user0db            |
+--------------------+
5 rows in set (0.00 sec)

lsでインストールしたものを確認でき、 deleteで削除できる。

$ helm ls
NAME            	REVISION	UPDATED                 	STATUS CHART      	NAMESPACE
pioneering-hydra	1       	Tue Jul 25 00:55:59 2017	DEPLOYEmysql-0.2.6	default  

$ helm delete pioneering-hydra
$ kubectl get secret --namespace default pioneering-hydra-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo
Error from server (NotFound): secrets "pioneering-hydra-mysql" not found