[root@k8s-master ~]# helm init Creating /root/.helm Creating /root/.helm/repository Creating /root/.helm/repository/cache Creating /root/.helm/repository/local Creating /root/.helm/plugins Creating /root/.helm/starters Creating /root/.helm/cache/archive Creating /root/.helm/repository/repositories.yaml Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com Adding local repo with URL: http://127.0.0.1:8879/charts $HELM_HOME has been configured at /root/.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. For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation Happy Helming!
[root@k8s-master ~]# helm search NAME CHART VERSION APP VERSION DESCRIPTION stable/acs-engine-autoscaler 2.1.3 2.1.1 Scales worker nodes within agent pools stable/aerospike 0.1.7 v3.14.1.2 A Helm chart for Aerospike in Kubernetes stable/anchore-engine 0.1.3 0.1.6 Anchore container analysis and policy evaluatio... ......
[root@k8s-master helm]# helm repo update #仓库更新有时会提示无法连接 Hang tight while we grab the latest from your chart repositories... ...Skip local chart repository ...Unable to get an update from the "stable" chart repository (https://kubernetes-charts.storage.googleapis.com): Get https://kubernetes-charts.storage.googleapis.com/index.yaml: dial tcp 216.58.220.208:443: connect: connection refused Update Complete. ⎈ Happy Helming!⎈
[root@k8s-master helm]# helm repo list NAME URL stable https://kubernetes-charts.storage.googleapis.com local http://127.0.0.1:8879/charts
[root@k8s-master helm]# helm repo remove stable #移除stable repo "stable" has been removed from your repositories [root@k8s-master helm]# helm repo list NAME URL local http://127.0.0.1:8879/charts
#增加阿里云的charts仓库 [root@k8s-master helm]# helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts "stable" has been added to your repositories [root@k8s-master helm]# helm repo list NAME URL local http://127.0.0.1:8879/charts stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts [root@k8s-master helm]# helm repo update #再次更新repo Hang tight while we grab the latest from your chart repositories... ...Skip local chart repository ...Successfully got an update from the "stable" chart repository Update Complete. ⎈ Happy Helming!⎈
与 yum 一样,helm 也支持关键字搜索:
1 2 3 4 5 6 7
[root@k8s-master ~]# helm search mysql NAME CHART VERSION APP VERSION DESCRIPTION stable/mysql 0.3.5 Fast, reliable, scalable, and easy to use open-... stable/percona 0.3.0 free, fully compatible, enhanced, open source d... stable/percona-xtradb-cluster 0.0.2 5.7.19 free, fully compatible, enhanced, open source d... stable/gcloud-sqlproxy 0.2.3 Google Cloud SQL Proxy stable/mariadb 2.1.6 10.1.31 Fast, reliable, scalable, and easy to use open-...
3. Connect using the mysql cli, then provide your password: $ mysql -h reeling-bronco-mysql -p
To connect to your database directly from outside the K8s cluster: MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306
# Execute the following commands to route the connection: export POD_NAME=$(kubectl get pods --namespace default -l "app=reeling-bronco-mysql" -o jsonpath="{.items[0].metadata.name}") kubectl port-forward $POD_NAME 3306:3306
mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
输出分为三部分:
① chart 本次部署的描述信息:
NAME 是 release 的名字,因为我们没用 -n 参数指定,Helm 随机生成了一个,这里是 reeling-bronco。
② 当前 release 包含的资源:Service、Deployment、Secret 和 PersistentVolumeClaim,其名字都是 reeling-bronco-mysql,命名的格式为 ReleasName-ChartName。
③ NOTES 部分显示的是 release 的使用方法。比如如何访问 Service,如何获取数据库密码,以及如何连接数据库等。
通过 kubectl get 可以查看组成 release 的各个对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[root@k8s-master helm]# kubectl get service reeling-bronco-mysql NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE reeling-bronco-mysql ClusterIP 10.99.245.169 <none> 3306/TCP 3m
[root@k8s-master helm]# kubectl get deployment reeling-bronco-mysql NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE reeling-bronco-mysql 1 1 1 0 3m
[root@k8s-master helm]# kubectl get pvc reeling-bronco-mysql NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE reeling-bronco-mysql Pending 4m
[root@k8s-master helm]# kubectl get secret reeling-bronco-mysql NAME TYPE DATA AGE reeling-bronco-mysql Opaque 2 4m
由于我们还没有准备 PersistentVolume,当前 release 还不可用。
helm list 显示已经部署的 release,helm delete 可以删除 release。
1 2 3 4 5 6
[root@k8s-master helm]# helm list NAME REVISION UPDATED STATUS CHART NAMESPACE reeling-bronco 1 Wed Mar 27 03:10:31 2019 DEPLOYED mysql-0.3.5 default
② Chart 和 Release 是 Helm 预定义的对象,每个对象都有自己的属性,可以在模板中使用。如果使用下面命令安装 chart:
1 2 3 4
[root@k8s-master templates]# helm search stable/mysql NAME CHART VERSION APP VERSION DESCRIPTION stable/mysql 0.3.5 Fast, reliable, scalable, and easy to use open-... [root@k8s-master templates]# helm install stable/mysql -n my
那么:
1 2 3 4 5
.Chart.Name 的值为 mysql .Chart.Version 的值为 0.3.5 .Release.Name }} 的值为 my .Release.Service }} 始终取值为 Tiller template "mysql.fullname" . }} 计算结果为 `my-mysql
③ 这里指定 mysql-root-password 的值,不过使用了 if-else 的流控制,其逻辑为:
输出的实际上是 values.yaml 的内容。阅读注释就可以知道 MySQL chart 支持哪些参数,安装之前需要做哪些准备。其中有一部分是关于存储的:
1 2 3 4 5 6 7 8 9 10 11 12 13
## Persist data to a persistent volume persistence: enabled: true ## database data Persistent Volume Storage Class ## If defined, storageClassName: <storageClass> ## If set to "-", storageClassName: "", which disables dynamic provisioning ## If undefined (the default) or set to null, no storageClassName spec is ## set, choosing the default provisioner. (gp2 on AWS, standard on ## GKE, AWS & OpenStack) ## # storageClass: "-" accessMode: ReadWriteOnce size: 8Gi
[root@k8s-master volumes]# kubectl apply -f mysql-pv.yaml persistentvolume/mysql-pv2 created
[root@k8s-master volumes]# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE mysql-pv2 8Gi RWO Retain Available 5s
[root@k8s-master ~]# helm upgrade --set imageTag=5.7.15 my stable/mysql Release "my" has been upgraded. Happy Helming! LAST DEPLOYED: Tue Oct 30 23:42:36 2018 ...... [root@k8s-master ~]# kubectl get deployment my-mysql -o wide NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR my-mysql 1 1 1 0 11m my-mysql mysql:5.7.15 app=my-mysql
helm history 可以查看 release 所有的版本。通过 helm rollback 可以回滚到任何版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[root@k8s-master ~]# helm history my REVISION UPDATED STATUS CHART DESCRIPTION 1 Tue Oct 30 23:31:42 2018 SUPERSEDED mysql-0.3.5 Install complete 2 Tue Oct 30 23:42:36 2018 DEPLOYED mysql-0.3.5 Upgrade complete [root@k8s-master ~]# helm rollback my 1 Rollback was a success! Happy Helming! 回滚成功,MySQL 恢复到 5.7.14。
[root@k8s-master ~]# helm history my REVISION UPDATED STATUS CHART DESCRIPTION 1 Tue Oct 30 23:31:42 2018 SUPERSEDED mysql-0.3.5 Install complete 2 Tue Oct 30 23:42:36 2018 SUPERSEDED mysql-0.3.5 Upgrade complete 3 Tue Oct 30 23:44:28 2018 DEPLOYED mysql-0.3.5 Rollback to 1 [root@k8s-master ~]# kubectl get deployment my-mysql -o wide NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR my-mysql 1 1 1 1 13m my-mysql mysql:5.7.14 app=my-mysql
(5)通过 helm repo add 将新仓库添加到 Helm。 [root@k8s-master ~]# helm repo add newrepo http://192.168.56.12:8080/charts "newrepo" has been added to your repositories [root@k8s-master ~]# helm repo list NAME URL local http://127.0.0.1:8879/charts stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts newrepo http://192.168.56.12:8080/charts
(6)现在已经可以 repo search 到 mychart 了。 [root@k8s-master ~]# helm search mychart NAME CHART VERSION APP VERSION DESCRIPTION local/mychart 0.1.0 1.0 A Helm chart for Kubernetes newrepo/mychart 0.1.0 1.0 A Helm chart for Kubernetes
除了 newrepo/mychart,这里还有一个 local/mychart。这是因为在执行第 2 步打包操作的同时,mychart 也被同步到了 local 的仓库。
(8)如果以后仓库添加了新的 chart,需要用 helm repo update 更新本地的 index。 [root@k8s-master ~]# helm repo update Hang tight while we grab the latest from your chart repositories... ...Skip local chart repository ...Successfully got an update from the "newrepo" chart repository ...Successfully got an update from the "stable" chart repository Update Complete. ⎈ Happy Helming!⎈