实例:Helm安装Mysql
本课介绍怎么通过Helm2安装Mysql。
确保添加了一个仓库
首先,Mysql的源文件,安装包在远程服务器上,为了让Helm知道在哪里找到文件,我们需要通过helm repo add 添加仓库,如下:
$ helm repo add stable http://mirror.azure.cn/kubernetes/charts/ "stable" has been added to your repositories
有没有同学,感兴趣helm repo add命令在做什么?其实很简单,是在下载、解析仓库文件。 仓库文件是: http://mirror.azure.cn/kubernetes/charts/index.yaml,大家可以打开这个文件看一下,有7M多。
这个过程如下:
graph TB 开始 --> 下载index.yaml 下载index.yaml --> 解析index.yaml --> 结束
helm repo add命令详解
我们在看看这个命令
helm repo add stable http://mirror.azure.cn/kubernetes/charts/
- helm repo add 是关键词,表示添加仓库的意思。
- stable 是仓库的名字
- http://mirror.azure.cn/kubernetes/charts/ 是仓库在远程的地址
查看仓库是否安装成功
下面的命令会列出仓库中所有的软件包。
$ helm search repo bitnami/jasperreports 7.0.9 7.2.0 The JasperReports server can be used as a stand-alone or ... stable/jasperreports 7.0.9 7.2.0 The JasperReports server can be used as a stand-alone or ... stable/artifactory 7.3.1 6.1.0 DEPRECATED Universal Repository Manager supporting all ma... stable/artifactory-ha 0.4.1 6.2.0 DEPRECATED Universal Repository Manager supporting all ma... stable/chartmuseum 2.8.0 0.11.0 Host your own Helm Chart Repository stable/dmarc2logstash 1.2.0 1.0.3 Provides a POP3-polled DMARC XML report injector into Ela... stable/satisfy 1.0.0 3.0.4 Composer repo hosting with Satisfy stable/sentry 4.0.1 9.1.2 Sentry is a cross-platform crash reporting and aggregatio... stable/sonatype-nexus 1.22.0 3.20.1-01 Sonatype Nexus is an open source repository manager
可以看到,我的机器上安装的仓库有点多。
搜索Mysql
仓库安装好了,我们就可以搜索Mysql并安装了。helm search是搜索的意思,这里搜索mysql
$ helm search mysql
mysql有点多,我们选择一个安装就可以了
搜索结果如下:
NAME CHART VERSION APP VERSION DESCRIPTION stable/mysql 1.6.2 5.7.28 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.2 v0.11.0 A Helm chart for prometheus mysql exporter with... stable/percona 1.2.0 5.7.17 free, fully compatible, enhanced, open source d... stable/percona-xtradb-cluster 1.0.3 5.7.19 free, fully compatible, enhanced, open source d... stable/phpmyadmin 4.2.12 5.0.1 phpMyAdmin is an mysql administration frontend stable/gcloud-sqlproxy 0.6.1 1.11 DEPRECATED Google Cloud SQL Proxy stable/mariadb 7.3.9 10.3.22 Fast, reliable, scalable, and easy to use open-...
本文我们安装第一个stable/mysql。
helm安装mysql
helm install是安装的意思:
helm install stable/mysql
- stable/mysql 表示我们要安装的stable/mysql的这个包。
执行结果:
NAME: terrific-quoll LAST DEPLOYED: Sat Mar 7 10:34:58 2020 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/ConfigMap NAME DATA AGE terrific-quoll-mysql-test 1 0s ==> v1/Deployment NAME READY UP-TO-DATE AVAILABLE AGE terrific-quoll-mysql 0/1 1 0 0s ==> v1/PersistentVolumeClaim NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE terrific-quoll-mysql Bound pv11 10Gi RWO 0s ==> v1/Pod(related) NAME READY STATUS RESTARTS AGE terrific-quoll-mysql-7dcf4bc4bd-ql9gp 0/2 Init:0/2 0 0s ==> v1/Secret NAME TYPE DATA AGE terrific-quoll-mysql Opaque 2 0s ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE terrific-quoll-mysql ClusterIP 10.1.22.254 <none> 3306/TCP 0s NOTES: MySQL can be accessed via port 3306 on the following DNS name from within your cluster: terrific-quoll-mysql.default.svc.cluster.local To get your root password run: MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default terrific-quoll-mysql -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 terrific-quoll-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 command to route the connection: kubectl port-forward svc/terrific-quoll-mysql 3306 mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
等一会,等镜像下载成功,各种服务启动成功,你就可以使用Mysql了。安装非常简单。
上面的内容我们有必要细致解释一下,因为它非常重要。
查看服务、pod、deployment是否启动成功
上面部署后,我们可以通过kubectl get svc | grep mysql
,kubectl get pod | grep mysql
等查看:
$ kubectl get svc | grep mysql terrific-quoll-mysql ClusterIP 10.1.22.254 <none> 3306/TCP 2m41s
$ kubectl get pod | grep mysql terrific-quoll-mysql-7dcf4bc4bd-ql9gp 2/2 Running 0 4m50s
如果为running,就表示启动成功了。
$ kubectl get deployment | grep mysql terrific-quoll-mysql 1/1 1 1 5m46s
参看mysql的密码
在上面一大段英文中,其实已经说明了怎么知道mysql的密码,执行如下命令:
MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default terrific-quoll-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo) echo $MYSQL_ROOT_PASSWORD
这样就输出了mysql的密码,如果你想自定义密码,那么在安装之前,你就需要指定密码,这里我们暂不解释。
生成一个客户端来访问mysql
执行命令:
bash
$ kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il
启动了一个ubuntu,然后在ubuntu上安装mysql客户端:
bash
$ apt-get update && apt-get install mysql-client -y
最后连接mysql服务器就可以了:
bash
mysql -h terrific-quoll-mysql -p
注意,上面的terrific-quoll-mysql
这个服务,你是需要自己更kubectl get svc查出来的,在你的机器上可能不一样。
最后输入用户名、密码就可以登录 。
helm卸载mysql
如果要卸载,非常简单,只需要执行uninstall + 名字就可以了,如下:
$ helm delete terrific-quoll release "terrific-quoll" uninstalled
如果你创建的时候,有命名空间,那么加上命令空间,就可以了。
$ helm uninstall terrific-quoll -n namesapce
注意,terrific-quoll这个名字你可以在前面找到,在你的电脑上,可能不同。