跳转至

使用 helm 部署应用 MySQL

环境说明:k8s 集群中存在 storageclass:nfs-client

[root@k8smaster002 mysql]# kubectl get sc
NAME                   PROVISIONER                                   RECLAIMPOLICY
nfs-client (default)   k8s-sigs.io/nfs-subdir-external-provisioner   Delete       

helm 安装一个 mysql 应用:

传参方式安装一个 mysql 应用:

传参方式安装一个 mysql 应用

1、仓库中搜索 mysql
[root@k8smaster002 mysql]# helm search repo mysql
NAME                                            CHART VERSION   APP VERSION     DESCRIPTION                                       
prometheus-community/prometheus-mysql-exporter  2.5.1           v0.15.1         A Helm chart for prometheus mysql exporter with...
stable/mysql                                    1.6.9           5.7.30          DEPRECATED - Fast, reliable, scalable, and easy...
2、Helm Chart 的安装
[root@k8smaster002 mysql]# helm install stable/mysql --generate-name  --set persistence.storageClass=nfs-client --set mysqlRootPassword=test123
3、部署过程输出的信息:
WARNING: This chart is deprecated
NAME: mysql-1712733475
LAST DEPLOYED: Wed Apr 10 15:18:01 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1712733475.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-1712733475 -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-1712733475 -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-1712733475 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
4、查看 helm 中安装了什么应用
... 
[root@k8smaster002 mysql]# helm list
NAME            NAMESPACE       REVISION          STATUS          CHART           APP VERSION
my-mysql        default         1                 CST deployed    mysql-1.6.9     5.7.30 
...

...
[root@k8smaster002 mysql]# kubectl get pods
NAME                                     READY   STATUS    RESTARTS       AGE
mysql-1712733475-58588bcf5c-87x4q        1/1     Running   0              6m42s
...

...
[root@k8smaster002 mysql]# kubectl get pvc
NAME               STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
mysql-1712733475   Bound    pvc-c0600437-f632-4a89-8f57-f35dc09f67e2   8Gi        RWO            nfs-client     <unset>                 7m50s
...

...
[root@k8smaster002 mysql]# kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM                      STORAGECLASS  
pvc-c0600437-f632-4a89-8f57-f35dc09f67e2   8Gi        RWO            Delete           Bound      default/mysql-1712733475   nfs-client   
...

一个 chart 包是可以多次安装到同一个集群中的,每次安装都会产生一个 release, 每个 release 都可以独立管理和升级。

1、安装第二个 mysql
...
[root@k8smaster002 mysql]# helm install stable/mysql --generate-name  --set persistence.storageClass=nfs-client --set mysqlRootPassword=test456
...

...
[root@k8smaster002 mysql]# helm list
NAME                    NAMESPACE       REVISION        STATUS          CHART           APP VERSION
mysql-1712733475        default         1               deployed        mysql-1.6.9     5.7.30     
mysql-1712735087        default         1               CST deployed    mysql-1.6.9     5.7.30     
...

...
[root@k8smaster002 mysql]# kubectl get pod
NAME                                     READY   STATUS    RESTARTS       AGE
mysql-1712733475-58588bcf5c-55r79        1/1     Running   0              6m
mysql-1712735087-5665bcc547-ncjkc        1/1     Running   0              26s
...
1、kubectl exec -it mysql-1712733475-58588bcf5c-55r79 -- mysql -uroot -ptest123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 144
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database mysql001;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mysql001           |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
1、kubectl exec -it mysql-1712735087-5665bcc547-ncjkc -- mysql -uroot -ptest456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 113
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

查看 chart 资源

1、查看 release=mysql-1712733475 的资源描述
[root@k8smaster002 mysql]# kubectl get all -l release=mysql-1712733475
NAME                                    READY   STATUS    RESTARTS   AGE
pod/mysql-1712733475-58588bcf5c-55r79   1/1     Running   0          17m

NAME                       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/mysql-1712733475   ClusterIP   10.108.240.154   <none>        3306/TCP   38m

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mysql-1712733475   1/1     1            1           38m

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/mysql-1712733475-58588bcf5c   1         1         1       38m
2、我们也可以 helm show chart 命令来了解 MySQL 这个 chart 包的一些特性:
[root@k8smaster002 mysql]# helm show chart stable/mysql
apiVersion: v1
appVersion: 5.7.30
deprecated: true
description: DEPRECATED - Fast, reliable, scalable, and easy to use open-source relational
  database system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 1.6.9

删除 Release

如果需要删除这个 release,也很简单,只需要使用 helm uninstallhelm delete 命令即可:

1. 使用 uninstall 命令会从 Kubernetes 中删除 release,也会删除与 release 相关的所有 Kubernetes 资源以及 release 历史记录。
[root@k8smaster002 mysql]# helm uninstall mysql-1712733475
release "mysql-1712733475" uninstalled

[root@k8smaster002 mysql]# helm ls
NAME                    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
mysql-1712735087        default         1               2024-04-10 15:44:48.054667469 +0800 CST deployed        mysql-1.6.9     5.7.30
2. 在删除的时候使用 --keep-history 参数,则会保留 release 的历史记录,该 release 的状态就是 UNINSTALLED
[root@k8smaster002 mysql]# helm uninstall mysql-1712735087 --keep-history
release "mysql-1712735087" uninstalled

[root@k8smaster002 mysql]# helm ls -a
NAME                    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
mysql-1712735087        default         1               2024-04-10 15:44:48.054667469 +0800 CST uninstalled     mysql-1.6.9     5.7.30 
# 状态为 uninstalled
3. 审查历史时甚至可以取消删除 release
`Usage:  helm rollback <RELEASE> [REVISION] [flags]`

[root@k8smaster002 mysql]# helm rollback mysql-1712735087 1
Rollback was a success! Happy Helming!

[root@k8smaster002 mysql]# helm ls
NAME                    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
mysql-1712735087        default         2               2024-04-10 16:14:28.241929672 +0800 CST deployed        mysql-1.6.9     5.7.30 

# rollback 后,又回到 deployed 状态

定制参数部署应用 mysql 应用:

传参方式安装一个 mysql 应用

1、上面我们都是直接使用的 helm install 命令安装的 chart 包,这种情况下只会使用 chart 的默认配置选项,但是更多的时候,是各种各样的需求,所以我们希望根据自己的需求来定制 chart 包的配置参数。
我们可以使用 `helm show values` 命令来查看一个 chart 包的所有可配置的参数选项
[root@k8smaster002 mysql]# helm show values stable/mysql
......
......

上面的所有参数都是可以用自己的数据来覆盖的,可以在安装的时候通过 YAML 格式的文件来传递这些参数。

2、查看 Helm Chart 的帮助文档
[root@k8smaster002 mysql]# helm show chart stable/mysql
apiVersion: v1
appVersion: 5.7.30
deprecated: true
description: DEPRECATED - Fast, reliable, scalable, and easy to use open-source relational
database system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 1.6.9
3、查看 Helm Chart 的配置选项和默认值并重定向到一个文件 values.yaml 并修改 values.yaml 文件中 storageClass 与 mysqlRootPassword
[root@k8smaster002 mysql]# helm show values stable/mysql > values.yaml
mysqlDatabase: helm
persistence:
  enabled: true  # 没有存储卷情况下,改为false
  storageClass: "nfs-client"
mysqlRootPassword: test123
4、安装的时候使用 -f values.yaml 安装应用并覆盖参数
[root@k8smaster002 mysql]# helm install my-mysql stable/mysql -f values.yaml

# 部署过程输出的信息:
WARNING: This chart is deprecated
NAME: my-mysql
LAST DEPLOYED: Wed Apr 10 14:54:43 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
my-mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default my-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 my-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/my-mysql 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
5、查看 helm 中安装了什么应用
[root@k8smaster002 mysql]# helm list
NAME            NAMESPACE       REVISION          STATUS          CHART           APP VERSION
my-mysql        default         1                 CST deployed    mysql-1.6.9     5.7.30 
6、查看部署的相关资源
[root@k8smaster002 mysql]# kubectl get all -l release=my-mysql -owide
NAME                           READY   STATUS    RESTARTS   AGE     IP              NODE       NOMINATED NODE   READINESS GATES
pod/my-mysql-b6999b8b5-vsqlk   1/1     Running   0          2m36s   10.244.214.72   linuxnbg   <none>           <none>

NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE     SELECTOR
service/my-mysql   ClusterIP   10.108.247.193   <none>        3306/TCP   2m36s   app=my-mysql

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
deployment.apps/my-mysql   1/1     1            1           2m36s   my-mysql     mysql:5.7.30   app=my-mysql,release=my-mysql

NAME                                 DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         SELECTOR
replicaset.apps/my-mysql-b6999b8b5   1         1         1       2m36s   my-mysql     mysql:5.7.30   app=my-mysql,pod-template-hash=b6999b8b5,release=my-mysql

安装 mysql 客户端并连接测试

1、部署 mariadb
yum install mariadb -y
2、查看 pod IP 地址 连接 mysql
[root@k8smaster002 mysql]# kubectl get pods -o wide -l release=my-mysql
NAME                       READY   STATUS    RESTARTS   AGE   IP              NODE       NOMINATED NODE   READINESS GATES
my-mysql-b6999b8b5-vsqlk   1/1     Running   0          14m   10.244.214.72   linuxnbg   <none>           <none>
[root@k8smaster002 mysql]# mysql -h 10.244.214.72 -u root -ptest123  -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5、查看 service IP 连接 mysql
[root@k8smaster002 mysql]# kubectl get svc -o wide -l release=my-mysql
NAME       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE   SELECTOR
my-mysql   ClusterIP   10.108.247.193   <none>        3306/TCP   19m   app=my-mysql
[root@k8smaster002 mysql]# mysql -h 10.108.247.193 -u root -ptest123  -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

升级和回滚

当新版本的 chart 包发布的时候,或者当你要更改 release 的配置的时候,你可以使用 helm upgrade 命令来操作。升级需要一个现有的 release,并根据提供的信息对其进行升级。因为 Kubernetes charts 可能很大而且很复杂,Helm 会尝试以最小的侵入性进行升级,它只会更新自上一版本以来发生的变化:

升级 mysql

1. 升级 mysql

1. 升级前查看版本
[root@k8smaster002 ~]# mysql -h 10.108.247.193 -u root -ptest123  -e "select version()"
+-----------+
| version() |
+-----------+
| 5.7.30    |
+-----------+
[root@k8smaster002 ~]#  kubectl get deployment my-mysql -o wide
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
my-mysql   1/1     1            1           75m   my-mysql     mysql:5.7.30   app=my-mysql,release=my-mysql
2. 修改配置并升级加一个 --set imageTag=5.7.31 参数设置为5.7.31版本
[root@k8smaster002 mysql]# helm upgrade my-mysql -f values.yaml --set imageTag=5.7.31 stable/mysql
WARNING: This chart is deprecated
Release "my-mysql" has been upgraded. Happy Helming!
# 升级过程中的输出:
NAME: my-mysql
LAST DEPLOYED: Wed Apr 10 17:55:08 2024
NAMESPACE: default
STATUS: deployed
REVISION: 2
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
my-mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default my-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 my-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/my-mysql 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
3. 升级后确认版本是否为 5.7.31 版本
[root@k8smaster002 mysql]# kubectl get deployment my-mysql -o wide
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
my-mysql   1/1     1            1           80m   my-mysql     mysql:5.7.31   app=my-mysql,release=my-mysql
[root@k8smaster002 mysql]# mysql -h 10.108.247.193 -u root -ptest123  -e "select version()"
+-----------+
| version() |
+-----------+
| 5.7.31    |
+-----------+

回滚 mysql

1. 回滚 mysql

1. 查看历史版本
[root@k8smaster002 mysql]# helm history my-mysql
REVISION        UPDATED                         STATUS          CHART           APP VERSION     DESCRIPTION     
1               Wed Apr 10 16:36:56 2024        superseded      mysql-1.6.9     5.7.30          Install complete
2               Wed Apr 10 17:55:08 2024        deployed        mysql-1.6.9     5.7.30          Upgrade complete
2. 使用 rollback mysql 来回滚版本
[root@k8smaster002 mysql]# helm rollback my-mysql 1
Rollback was a success! Happy Helming!
3. 查看回滚之后的版本
[root@k8smaster002 mysql]# kubectl get deployment my-mysql -o wide
NAME       READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
my-mysql   1/1     1            1           116m   my-mysql     mysql:5.7.30   app=my-mysql,release=my-mysql
4. 使用 rollback mysql 来回滚版本
[root@k8smaster002 mysql]# helm history my-mysql
REVISION        UPDATED                         STATUS          CHART           APP VERSION     DESCRIPTION     
1               Wed Apr 10 16:36:56 2024        superseded      mysql-1.6.9     5.7.30          Install complete
2               Wed Apr 10 17:55:08 2024        superseded      mysql-1.6.9     5.7.30          Upgrade complete
3               Wed Apr 10 18:32:38 2024        deployed        mysql-1.6.9     5.7.30          Rollback to 1 

更多安装方式

和yum命令类似

  • chart 仓库
  • 本地 chart 压缩包
[root@k8smaster002 mysql]# helm pull stable/mysql
[root@k8smaster002 mysql]# ls mysql-1.6.9.tgz
mysql-1.6.9.tgz
[root@k8smaster002 mysql]# helm install mysql2 mysql-1.6.9.tgz