0


使用Helm部署apphub仓库的MySQL数据库服务(二)

使用Helm部署apphub仓库的MySQL数据库服务

文章目录

1.查看MySQL Chart包相关属性

1.1.添加阿里云chart仓库地址

如果使用之前添加的chart仓库安装mysql报错内容为

Error: unable to build kubernetes objects from release manifest: unable to recognize "": no matches for kind "Deployment" in version "extensions/v1beta1"

那么就添加一下下面的这个阿里云chart仓库。

之前提到的chart仓库已经不再维护可以使用https://apphub.aliyuncs.com这个chart仓库。

[root@k8s-master1 ~]# helm repo add apphub https://apphub.aliyuncs.com
"apphub" has been added to your repositories

1.2.搜索mysql chart包

[root@k8s-master1 ~]# helm search repo mysql
NAME                                CHART VERSION    APP VERSION    DESCRIPTION                                       
aliyun/mysql                        0.3.5                           Fast, reliable, scalable, and easy to use open-...
apphub/mysql                        6.8.0            8.0.19         Chart to create a Highly available MySQL cluster  
apphub/mysqldump                    2.6.0            2.4.1          A Helm chart to help backup MySQL databases usi...
apphub/mysqlha                      1.0.0            5.7.13         MySQL cluster with a single master and zero or ...
apphub/prometheus-mysql-exporter    0.5.2            v0.11.0        A Helm chart for prometheus mysql exporter with...
aliyun/percona                      0.3.0                           free, fully compatible, enhanced, open source d...
aliyun/percona-xtradb-cluster       0.0.2            5.7.19         free, fully compatible, enhanced, open source d...
apphub/percona                      1.2.0            5.7.17         free, fully compatible, enhanced, open source d...
apphub/percona-xtradb-cluster       1.0.3            5.7.19         free, fully compatible, enhanced, open source d...
apphub/phpmyadmin                   4.2.12           5.0.1          phpMyAdmin is an mysql administration frontend    
aliyun/gcloud-sqlproxy              0.2.3                           Google Cloud SQL Proxy                            
aliyun/mariadb                      2.1.6            10.1.31        Fast, reliable, scalable, and easy to use open-...
apphub/mariadb                      7.3.9            10.3.22        Fast, reliable, scalable, and easy to use open-...
apphub/mariadb-galera               0.8.1            10.4.12        MariaDB Galera is a multi-master database clust...

#我们将使用apphub/mysql这个chart包安装mysql

1.3.查看mysql chart包中的变量信息

[root@k8s-master1 ~]# helm show values apphub/mysql | less

[root@k8s-master1 ~]# helm show values apphub/mysql > value.yaml

使用show values命令可以查询出一个chart包中包含部署应用所需的环境变量属性,其中有镜像的版本、健康检查、资源配额以及pv/pvc、StorageClass各种资源等等的属性变量,这些属性相当于一个变量模板,在deployment资源文件中肯定会有很多个属性需要动态获取,而获取那些变量属性就是从values中记录的。

在这里插入图片描述

2.使用Helm部署MySQL服务

[root@k8s-master1 ~]# helm install db apphub/mysql
NAME: db
LAST DEPLOYED: Wed Jul 21 13:51:04 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Please be patient while the chart is being deployed
········

部署mysql输出的信息,红框中是告诉我们怎么测试mysql服务。
在这里插入图片描述

3.查看mysql的部署状态

helm部署出来的mysql一共起了2个pod,一个master一个slave

1.查看helm部署mysql的状态
[root@k8s-master1 ~]# helm list
NAME    NAMESPACE    REVISION    UPDATED                                    STATUS      CHART          APP VERSION
db      default      1           2021-07-21 13:51:04.998554127 +0800 CST    deployed    mysql-6.8.0    8.0.19   
#部署在default命名空间下

2.查看mysql服务的状态
[root@k8s-master1 ~]# kubectl get all 
NAME                           READY   STATUS    RESTARTS   AGE
pod/db-mysql-master-0          0/1     Pending   0          50m
pod/db-mysql-slave-0           0/1     Pending   0          50m

NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/db-mysql         ClusterIP   10.110.223.245   <none>        3306/TCP   50m
service/db-mysql-slave   ClusterIP   10.110.83.52     <none>        3306/TCP   50m
service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP    2d

NAME                                  READY   AGE
statefulset.apps/db-mysql-master      0/1     50m
statefulset.apps/db-mysql-slave       0/1     50m
#可以看到mysql的pod资源一直处于pending状态

4.解决部署的MySQL服务一直处于Pending状态

4.1.分析处于Pending状态的原因

[root@k8s-master1 ~]# kubectl describe pod db-mysql-master-0
······
Events:
  Type     Reason            Age   From               Message
  ----     ------            ----  ----               -------
  Warning  FailedScheduling  52m   default-scheduler  0/4 nodes are available: 4 pod has unbound immediate PersistentVolumeClaims.
  Warning  FailedScheduling  52m   default-scheduler  0/4 nodes are available: 4 pod has unbound immediate PersistentVolumeClaims.

[root@k8s-master1 ~]# kubectl describe pod db-mysql-slave-0
·····
Events:
  Type     Reason            Age   From               Message
  ----     ------            ----  ----               -------
  Warning  FailedScheduling  52m   default-scheduler  0/4 nodes are available: 4 pod has unbound immediate PersistentVolumeClaims.
  Warning  FailedScheduling  52m   default-scheduler  0/4 nodes are available: 4 pod has unbound immediate PersistentVolumeClaims.

通过查看pod资源的详细信息,我们发现两个pod都处于pengding状态的原因是因为pvc没有被绑定导致的,我们再查下pvc的状态。

#pvc的确没有被绑定
[root@k8s-master1 ~]# kubectl get pvc
NAME                                          STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS       AGE
data-db-mysql-master-0                        Pending                                                                                           88m
data-db-mysql-slave-0                         Pending                                                                                           88m

查看完pvc的信息后,可以断定pvc是由于没有pv绑定才导致一直处于pending状态。

我们需要查看下该pvc是通过pv创建的还是通过StorageClass创建,才能完成pvc的绑定,如果我们之间随便创建一个pv让其绑定,虽然能绑定上,但是资源配置文件中,如果不是pvc绑定的pv,而是pvc绑定的StorageClass,还是会处于pending状态,如何判断pvc是怎样创建的,只需要查看部署资源的yaml文件是statefulset控制器还是deployment控制器,如果是statefulset控制器部署的资源,很有可能是StorageClass创建的pvc,主要也是看yaml中是否存在volumeClaimTemplates配置项,如果是StorageClass创建的pvc,我们也就需要创建一个StorageClass。

1.将mysql的chart包拉取到本地
#--untar参数的意思是将chart拉取后并解压
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# helm pull apphub/mysql --untar

2.资源部署的yaml文件主要在template目录
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# tree mysql/
mysql/
├── Chart.yaml
├── ci
│   └── values-production.yaml
├── files
│   └── docker-entrypoint-initdb.d
│       └── README.md
├── README.md
├── templates
│   ├── _helpers.tpl
│   ├── initialization-configmap.yaml
│   ├── master-configmap.yaml
│   ├── master-statefulset.yaml
│   ├── master-svc.yaml
│   ├── NOTES.txt
│   ├── secrets.yaml
│   ├── servicemonitor.yaml
│   ├── slave-configmap.yaml
│   ├── slave-statefulset.yaml
│   └── slave-svc.yaml
├── values-production.yaml
└── values.yaml
#可以看到mysql确实是使用statefulset控制器部署的

3.查看statefulset控制器yaml文件中是否是用StorageClass创建的pvc
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# vim mysql/templates/master-statefulset.yaml 

在大概256行的位置可以看到volumeClaimTemplates,这个配置参数就是使用StorageClass动态创建pvc,metadata中的name: data是说明pvc的名称为data。

到此位置我们也分析出了mysql不能正常启动的原因:

没有为pvc自动分配存储的StorageClass,我们只需要创建一个StorageClass并在values.yaml中指定使用哪个StorageClass即可解决问题


在这里插入图片描述

4.2.解决MySQL处于Pending的问题

1.创建StorageClass为MySQL服务提供动态存储

1.编写资源yaml文件
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# vim mysql-storageclass.yaml apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:name: helm-storageclass
  labels:app: mysql
    heritage: Helm
provisioner: nfs-storage-01reclaimPolicy: Retain

2.创建资源
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# kubectl apply -f mysql-storageclass.yaml
storageclass.storage.k8s.io/helm-storageclass configured

2.调整values.yaml文件指定使用哪个StorageClass

由于apphub仓库中的mysql部署完是一个主从集群,因此在values中分别对master、slave都有相应的环境变量,所以既要在master变量中指定StorageClass也要在slave中指定StorageClass。

#文件的262行是master服务的StorageClass配置master:
········
  persistence:enabled:truemountPath: /bitnami/mysql
    storageClass:"helm-storageclass"#其他参数都有,只需要打开storageClass的注释,填写我们使用的sc即可annotations:{}accessModes:- ReadWriteOnce
    size: 8Gi
#文件的400行是slave服务的StorageClass配置slave:
········
  persistence:enabled:truemountPath: /bitnami/mysql
    storageClass:"helm-storageclass"annotations:{}accessModes:- ReadWriteOnce
    size: 8Gi

在这里插入图片描述

4.3.重新使用Helm部署MySQL服务

由于我们改了chart包中的values变量信息,因此需要将原来部署mysql删除,重新指定我们pull下来修改配置以后的chart包路径,才能正常的启动服务,到此mysql服务处于pending状态的问题已经解决。

1.删除原来部署的mysql服务
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# helm uninstall db-apphub
release "db-apphub" uninstalled

2.删除产生的pvc
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# kubectl delete pvc data-db-apphub-mysql-master-0
persistentvolumeclaim "data-db-apphub-mysql-master-0" deleted
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# kubectl delete pvc data-db-apphub-mysql-slave-0
persistentvolumeclaim "data-db-apphub-mysql-slave-0" deleted

3.指定我们修改后的chart目录安装mysql
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# helm install db-apphub mysql/

4.查看部署的状态
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# helm list
NAME         NAMESPACE    REVISION    UPDATED                                    STATUS      CHART          APP VERSION
db           default      1           2021-07-21 17:02:16.911916854 +0800 CST    deployed    mysql-0.3.5               
db-apphub    default      1           2021-07-22 13:51:16.968872145 +0800 CST    deployed    mysql-6.8.0    8.0.19     

[root@k8s-master1 ~/helm/deploy/mysql/apphub]# kubectl get pod 
NAME                        READY   STATUS    RESTARTS   AGE
db-apphub-mysql-master-0    1/1     Running   0          2m13s
db-apphub-mysql-slave-0     1/1     Running   0          2m13s
db-mysql-744b95d594-ttz7l   1/1     Running   0          20h
nginx-pv-d4799b498-dv876    1/1     Running   0          2d4h
nginx-storage-stat-0        1/1     Running   0          2d4h
nginx-storage-stat-1        1/1     Running   0          2d4h
nginx-storage-stat-2        1/1     Running   0          2d4h

在这里插入图片描述

5.进入MySQL容器验证数据库服务

1.获取密码
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# kubectl get secret --namespace default db-apphub-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode
fgrYl2ljFi

2.进入mysql容器
[root@k8s-master1 ~/helm/deploy/mysql/apphub]# kubectl exec -it db-apphub-mysql-master-0  bash

3.进入mysql系统
I have no name!@db-apphub-mysql-master-0:/$ mysql -uroot -pfgrYl2ljFi
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| my_database        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.03 sec)
标签: mysql 数据库 dba

本文转载自: https://blog.csdn.net/weixin_44953658/article/details/123145502
版权归原作者 Jiangxl~ 所有, 如有侵权,请联系我们删除。

“使用Helm部署apphub仓库的MySQL数据库服务(二)”的评论:

还没有评论