当前位置:   article > 正文

helm安装mysql

helm 安装mysql

导航:

  1. 一、前提
  2. 1.版本信息-2.查看方法-3.查看k8s集群节点状态-4.配置docker仓库下载加速
  3. 二、安装部署helm
  4. 1.下载软件包-2.安装helm-3.初始化
  5. 三、使用helm安装mysql
  6. 1.搜索mysql的helm包-2.准备后端存储-3.准备docker image-4.配置集群内的kube-dns-5.安装mysql的helm包-6.查看mysql的有状态应用、pod、svc、pvc等k8s资源-7.客户端验证
  7. 四、故障排除
  8. 五、参考资料

一、前提

使用helm安装有状态应用mysql主备集群,首先确保你已经搭建好一个3节点的k8s集群(如何搭建k8s集群、k8s集群网络配置flannel请参考百度)。

1.版本信息
  1. k8s: v1.5.2
  2. docker: 1.13.1
  3. flannel:0.7.1
  4. helm: v3.0.0
2.查看方法
  1. kubectl version
  2. docker version
  3. flanneld --version
  4. helm version
3.查看k8s集群节点状态
  1. [root@master ~]# kubectl get node
  2. NAME STATUS AGE
  3. master Ready 4d
  4. node2 Ready 18h
  5. node3 Ready 4m
  6. [root@master ~]#
4.配置docker仓库下载加速

使用163的国内docker仓库,提高docker image的下载速度。

  1. [root@master ~]# cat /etc/docker/daemon.json
  2. {"registry-mirrors": ["http://hub-mirror.c.163.com"]}
  3. [root@master ~]# systemctl restart docker #重启docker服务生效
  4. [root@master ~]#

二、安装部署helm

使用helm的v3版本,v3版本不用配置服务端组件 Tiller。

1.下载软件包

下载helm的二进制包。
helm安装mysql
下载地址:
https://space.dingtalk.com/s/gwHOAF91cwLOABfU0gPaACBkNjc5YmMwMDUzZmQ0YWQwYjg2MmNhZWRiMGU3YmU1ZA 密码: 5kEK

2.安装helm

使用xshell或者winscp将安装包传输到linux中。

  1. [root@master ~]# tar -tvf helm-v3.0.0-alpha.1-linux-amd64.tar.gz #查看压缩包中的内容
  2. drwxr-xr-x circleci/circleci 0 2019-05-16 05:44 linux-amd64/
  3. -rwxr-xr-x circleci/circleci 39771936 2019-05-16 05:42 linux-amd64/helm
  4. -rw-r--r-- circleci/circleci 11373 2019-05-16 05:44 linux-amd64/LICENSE
  5. -rw-r--r-- circleci/circleci 3159 2019-05-16 05:44 linux-amd64/README.md
  6. [root@master ~]#

将二进制文件复制到$PATH路径下即可使用。

  1. [root@master ~]# which helm #查看helm二进制文件所在的路径,将helm复制到$PATH中即可直接使用,例如将helm放在/usr/bin/helm的路径
  2. /usr/bin/helm
  3. [root@master ~]# helm --help
  4. The Kubernetes package manager
  5. To begin working with Helm, run the 'helm init' command:
  6. $ helm init
  7. This will set up any necessary local configuration.
  8. Common actions from this point include:
  9. - helm search: search for charts
  10. - helm fetch: download a chart to your local directory to view
  11. - helm install: upload the chart to Kubernetes
  12. - helm list: list releases of charts
  13. Environment:
  14. $HELM_HOME set an alternative location for Helm files. By default, these are stored in ~/.helm
  15. $HELM_DRIVER set the backend storage driver. Values are: configmap, secret, memory
  16. $HELM_NO_PLUGINS disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.
  17. $KUBECONFIG set an alternative Kubernetes configuration file (default "~/.kube/config")
  18. Usage:
  19. helm [command]
  20. Available Commands:
  21. chart push, pull, tag, or remove Helm charts
  22. completion Generate autocompletions script for the specified shell (bash or zsh)
  23. create create a new chart with the given name
  24. dependency manage a chart's dependencies
  25. get download a named release
  26. help Help about any command
  27. history fetch release history
  28. home displays the location of HELM_HOME
  29. init initialize Helm client
  30. install install a chart
  31. lint examines a chart for possible issues
  32. list list releases
  33. package package a chart directory into a chart archive
  34. plugin add, list, or remove Helm plugins
  35. pull download a chart from a repository and (optionally) unpack it in local directory
  36. registry login to or logout from a registry
  37. repo add, list, remove, update, and index chart repositories
  38. rollback roll back a release to a previous revision
  39. search search for a keyword in charts
  40. show inspect a chart
  41. status displays the status of the named release
  42. template locally render templates
  43. test test a release or cleanup test artifacts
  44. uninstall uninstall a release
  45. upgrade upgrade a release
  46. verify verify that a chart at the given path has been signed and is valid
  47. version print the client version information
  48. Flags:
  49. --debug enable verbose output
  50. -h, --help help for helm
  51. --home string location of your Helm config. Overrides $HELM_HOME (default "/root/.helm")
  52. --kube-context string name of the kubeconfig context to use
  53. --kubeconfig string path to the kubeconfig file
  54. -n, --namespace string namespace scope for this request
  55. Use "helm [command] --help" for more information about a command.
  56. [root@master ~]#
3.初始化

使用helm前,需要初始化。

  1. [root@docker repository]# helm init --stable-repo-url=https://apphub.aliyuncs.com #配置helm使用的仓库为国内阿里云地址,国外的访问不了,而且比较慢
  2. Creating /root/.helm/repository/repositories.yaml
  3. Adding stable repo with URL: https://apphub.aliyuncs.com
  4. $HELM_HOME has been configured at /root/.helm.
  5. Happy Helming!
  6. [root@docker repository]# ls
  7. cache local repositories.yaml
  8. [root@docker repository]# cat repositories.yaml #helm初始化完后会有一个配置文件生成,下面是repositories.yaml的内容
  9. apiVersion: v1
  10. generated: "2019-08-25T07:08:38.917953914+08:00"
  11. repositories:
  12. - caFile: ""
  13. cache: /root/.helm/repository/cache/stable-index.yaml
  14. certFile: ""
  15. keyFile: ""
  16. name: stable
  17. password: ""
  18. url: https://apphub.aliyuncs.com
  19. username: ""

三、使用helm安装mysql

使用helm安装有状态应用mysql,使用到的docker镜像有:

  1. docker.io/mysql:latest
  2. docker.io/ist0ne/xtrabackup:latest

构建mysql的1主2从,主备集群,使用xtrabackup工具进行数据同步。

1.搜索mysql的helm包
  1. [root@docker ~]# helm search mysql #搜索mysql的helm包
  2. NAME CHART VERSION APP VERSION DESCRIPTION
  3. apphub/mysql 6.3.1 8.0.17 Chart to create a Highly available MySQL cluster
  4. apphub/mysqldump 2.6.0 2.4.1 A Helm chart to help backup MySQL databases usi...
  5. apphub/mysqlha 1.0.0 5.7.13 MySQL cluster with a single master and zero or ...
  6. apphub/prometheus-mysql-exporter 0.5.1 v0.11.0 A Helm chart for prometheus mysql exporter with...
  7. stable/mysql 6.3.1 8.0.17 Chart to create a Highly available MySQL cluster
  8. stable/mysqldump 2.6.0 2.4.1 A Helm chart to help backup MySQL databases usi...
  9. stable/mysqlha 1.0.0 5.7.13 MySQL cluster with a single master and zero or ...
  10. stable/prometheus-mysql-exporter 0.5.1 v0.11.0 A Helm chart for prometheus mysql exporter with...
  11. apphub/percona 1.1.0 5.7.17 free, fully compatible, enhanced, open source d...
  12. apphub/percona-xtradb-cluster 1.0.1 5.7.19 free, fully compatible, enhanced, open source d...
  13. apphub/phpmyadmin 3.0.1 4.9.0-1 phpMyAdmin is an mysql administration frontend
  14. stable/percona 1.1.0 5.7.17 free, fully compatible, enhanced, open source d...
  15. stable/percona-xtradb-cluster 1.0.1 5.7.19 free, fully compatible, enhanced, open source d...
  16. stable/phpmyadmin 3.0.1 4.9.0-1 phpMyAdmin is an mysql administration frontend
  17. apphub/mariadb 6.8.1 10.3.17 Fast, reliable, scalable, and easy to use open-...
  18. apphub/mariadb-galera 0.2.1 10.3.17 MariaDB Galera is a multi-master database clust...
  19. stable/mariadb 6.8.1 10.3.17 Fast, reliable, scalable, and easy to use open-...
  20. stable/mariadb-galera 0.2.1 10.3.17 MariaDB Galera is a multi-master database clust...
2.准备后端存储

由于mysql的helm包需要使用pv,而开源的k8s集群默认没有配置pv后端存储,因此需要手动添加nfs服务器(如何搭建nfs服务器请参考百度)为后端存储。
nfs配置:

  1. [root@master ~]# cat /etc/exports
  2. /nfsdata/pv001 *(rw,no_root_squash) #添加no_root_squash不压缩root权限,因为容器里会使用root权限写入数据,否则后面会有权限不足的报错
  3. /nfsdata/pv002 *(rw,no_root_squash)
  4. /nfsdata/pv003 *(rw,no_root_squash)
  5. [root@master ~]# ls -ld /nfsdata/ #配置目录的权限为nfsnobody
  6. drwxr-xr-x 5 nfsnobody nfsnobody 45 Aug 26 01:18 /nfsdata/
  7. [root@master ~]# ls -ll /nfsdata/
  8. total 0
  9. drwxr-xr-x 2 nfsnobody nfsnobody 6 Aug 26 01:18 pv001
  10. drwxr-xr-x 2 nfsnobody nfsnobody 6 Aug 26 01:18 pv002
  11. drwxr-xr-x 2 nfsnobody nfsnobody 6 Aug 26 01:18 pv003
  12. [root@master ~]#

创建pv的yaml文件:

  1. [root@master hrr]# cat pv.yml
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5. name: pv003
  6. spec:
  7. capacity:
  8. storage: 10Gi
  9. accessModes:
  10. - ReadWriteOnce #accessModes需要和pvc的模式一样
  11. nfs:
  12. path: /nfsdata/pv003
  13. server: 192.168.222.129
  14. [root@master hrr]#

创建pv:

  1. [root@docker hrr]# kubectl create -f pv.yml
  2. persistentvolume "pv003" created
  3. [root@docker hrr]#

查看pv:

  1. [root@master hrr]# kubectl get pv #Available表示没有被PVC使用
  2. NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
  3. pv001 10Gi RWO Retain Available 26s
  4. pv002 10Gi RWO Retain Available 14s
  5. pv003 10Gi RWO Retain Available 3s
  6. [root@master hrr]#
  7. [root@master hrr]# kubectl get pv #Bound表示被PVC使用
  8. NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
  9. pv001 10Gi RWO Retain Bound default/data-mysql-mysqlha-0 32s
  10. pv002 10Gi RWO Retain Bound default/data-mysql-mysqlha-1 17s
  11. pv003 10Gi RWO Retain Bound default/data-mysql-mysqlha-2 4s
  12. [root@master hrr]#
3.准备docker image

在安装mysql的helm包时需要mysql与xtrabackup的docker镜像。

  1. [root@docker docker]# docker pull docker.io/ist0ne/xtrabackup #下载xtrabackup镜像
  2. Using default tag: latest
  3. Trying to pull repository docker.io/ist0ne/xtrabackup ...
  4. latest: Pulling from docker.io/ist0ne/xtrabackup
  5. 386a066cd84a: Pull complete
  6. 40f175e652e1: Pull complete
  7. 5eb6b5905d55: Pull complete
  8. Digest: sha256:e5e5519b874363f1bf35541cef2caed5803645937e29834817be51b30aabc479
  9. Status: Downloaded newer image for docker.io/ist0ne/xtrabackup:latest
  10. [root@docker docker]#
  11. [root@docker docker]# docker pull docker.io/mysql #下载mysql镜像

查看本地的镜像:

  1. [root@master hrr]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. 192.168.222.129:5000/nginx latest 5a3221f0137b 9 days ago 126 MB
  4. docker.io/nginx latest 5a3221f0137b 9 days ago 126 MB
  5. docker.io/mysql latest 62a9f311b99c 11 days ago 445 MB
  6. mysql 5.7.13 62a9f311b99c 11 days ago 445 MB
  7. docker.io/ist0ne/xtrabackup latest c415dbd7af07 2 years ago 265 MB
  8. registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup 1.0 c415dbd7af07 2 years ago 265 MB
  9. 192.168.222.129:5000/pod-infrastructure latest f9d5de079539 5 years ago 240 kB
  10. docker.io/kubernetes/pause latest f9d5de079539 5 years ago 240 kB
  11. [root@master hrr]#

将镜像打tag,用于后面创建mysql的helm包:

  1. [root@docker hrr]# docker tag docker.io/mysql:latest mysql:5.7.13
  2. [root@docker docker]# docker tag docker.io/ist0ne/xtrabackup:latest registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0

将docker镜像导出,用于导入到其它k8s节点中:

  1. [root@master ~]# docker save mysql:5.7.13 -o mysql5713.tgz #将docker镜像导出,名为mysql5713.tgz
  2. [root@master ~]# docker save registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0 -o xtrabackup10.tgz
  3. [root@master ~]# scp mysql5713.tgz xtrabackup10.tgz root@192.168.222.130:
  4. root@192.168.222.130's password:
  5. mysql5713.tgz 100% 429MB 35.7MB/s 00:12
  6. xtrabackup10.tgz 100% 260MB 43.4MB/s 00:06
  7. [root@master ~]#

在其它k8s节点中导入docker镜像:

  1. [root@node2 ~]# docker load -i mysql5713.tgz #将导出的docker镜像mysql5713.tgz导入
  2. 8fa655db5360: Loading layer [==================================================>] 58.48 MB/58.48 MB
  3. f5741d086b76: Loading layer [==================================================>] 338.4 kB/338.4 kB
  4. 9f77b78f01a7: Loading layer [==================================================>] 10.44 MB/10.44 MB
  5. 7f33ce1066af: Loading layer [==================================================>] 4.472 MB/4.472 MB
  6. 1dd5f3e365cf: Loading layer [==================================================>] 1.536 kB/1.536 kB
  7. 65430c57aee2: Loading layer [==================================================>] 46.15 MB/46.15 MB
  8. bb1ef34119b2: Loading layer [==================================================>] 32.77 kB/32.77 kB
  9. 995fa4bb8afe: Loading layer [==================================================>] 3.584 kB/3.584 kB
  10. bdebd19e878f: Loading layer [==================================================>] 329.8 MB/329.8 MB
  11. 79a46f1eb9d0: Loading layer [==================================================>] 5.632 kB/5.632 kB
  12. 5b96d5043191: Loading layer [==================================================>] 9.728 kB/9.728 kB
  13. 647229410363: Loading layer [==================================================>] 1.536 kB/1.536 kB
  14. Loaded image: mysql:5.7.13
  15. [root@node2 ~]# docker load -i xtrabackup10.tgz
  16. fe4c16cbf7a4: Loading layer [==================================================>] 128.9 MB/128.9 MB
  17. 82d548d175dd: Loading layer [==================================================>] 9.216 kB/9.216 kB
  18. f85c58969eb0: Loading layer [==================================================>] 144 MB/144 MB
  19. Loaded image: registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0
  20. [root@node2 ~]#

node3做同样的操作。

4.配置集群内的kube-dns
5.安装mysql的helm包
  1. [root@master ~]# helm install mysql apphub/mysqlha
  2. NAME: mysql
  3. LAST DEPLOYED: 2019-08-25 09:43:33.16471802 +0800 CST m=+0.898633546
  4. NAMESPACE: default
  5. STATUS: deployed
  6. NOTES:
  7. The MySQL cluster is comprised of 3 MySQL pods: 1 master and 2 slaves. Each instance is accessible within the cluster through:
  8. <pod-name>.mysql-mysqlha
  9. `mysql-mysqlha-0.mysql-mysqlha` is designated as the master and where all writes should be executed against. Read queries can be executed against the `mysql-mysqlha-readonly` service which distributes connections across all MySQL pods.
  10. To connect to your database:
  11. 1. Obtain the root password:
  12. kubectl get secret --namespace default mysql-mysqlha -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo
  13. 2. Run a pod to use as a client:
  14. kubectl run mysql-client --image=mysql:5.7.13 -it --rm --restart='Never' --namespace default -- /bin/sh
  15. 3. To connect to Master service (read/write):
  16. mysql -h mysql-mysqlha-0.mysql-mysqlha -u root -p
  17. 4. To connect to slave service (read-only):
  18. mysql -h mysql-mysqlha-readonly -u root -p
  19. [root@master ~]#

安装完后,在本地会有mysql的helm包:

  1. [root@master archive]# pwd
  2. /root/.helm/cache/archive
  3. [root@master archive]# ls
  4. guestbook-0.2.0.tgz mysqlha-1.0.0.tgz
  5. [root@master archive]#
  6. [root@master archive]# tar -tvf mysqlha-1.0.0.tgz
  7. -rwxr-xr-x 0/0 378 2019-08-22 13:20 mysqlha/Chart.yaml
  8. -rwxr-xr-x 0/0 2561 2019-08-22 13:20 mysqlha/values.yaml
  9. -rwxr-xr-x 0/0 1216 2019-08-22 13:20 mysqlha/templates/NOTES.txt
  10. -rwxr-xr-x 0/0 961 2019-08-22 13:20 mysqlha/templates/_helpers.tpl
  11. -rwxr-xr-x 0/0 756 2019-08-22 13:20 mysqlha/templates/configmap.yaml
  12. -rwxr-xr-x 0/0 967 2019-08-22 13:20 mysqlha/templates/secret.yaml
  13. -rwxr-xr-x 0/0 10905 2019-08-22 13:20 mysqlha/templates/statefulset.yaml
  14. -rwxr-xr-x 0/0 1203 2019-08-22 13:20 mysqlha/templates/svc.yaml
  15. -rwxr-xr-x 0/0 56 2019-08-22 13:20 mysqlha/OWNERS
  16. -rwxr-xr-x 0/0 6724 2019-08-22 13:20 mysqlha/README.md
  17. [root@master archive]#

将mysqlha-1.0.0.tgz解压,查看文件内容:

  1. [root@master mysqlha]# ls
  2. Chart.yaml OWNERS README.md templates values.yaml
  3. [root@master mysqlha]# cat README.md
  4. # MySQL - Single Master, Multiple Slaves
  5. [MySQL](https://MySQL.org) is one of the most popular database servers in the world. Notable users include Wikipedia, Facebook and Google.
  6. ## Introduction
  7. This chart bootstraps a single master and multiple slave MySQL deployment on a [Kubernetes](http://kubernetes.io) cluster using the [Helm](https://helm.sh) package manager. Largely inspired by this [tutorial](https://kubernetes.io/docs/tutorials/stateful-application/run-replicated-stateful-application/), further work was made to 'production-ize' the example.
  8. ## Prerequisites
  9. - Kubernetes 1.6+
  10. - PV provisioner support in the underlying infrastructure
  11. ## Installing the Chart
  12. To install the chart with the release name `my-release`:
  13. $ helm install --name my-release <helm-repo>/mysqlha
  14. The command deploys MySQL cluster on the Kubernetes cluster in the default configuration. The [configuration](#configuration) section lists the parameters that can be configured during installation.
  15. ### Uninstall
  16. To uninstall/delete the `my-release` deployment:
  17. $ helm delete my-release
  18. ## Configuration
  19. The following table lists the configurable parameters of the MySQL chart and their default values.
  20. | Parameter | Description | Default |
  21. | ----------------------------------------- | ------------------------------------------------- | -------------------------------------- |
  22. | `mysqlImage` | `mysql` image and tag. | `mysql:5.7.13` |
  23. | `xtraBackupImage` | `xtrabackup` image and tag. | `gcr.io/google-samples/xtrabackup:1.0` |
  24. | `imagePullPolicy` | Image pull policy. | `IfNotPresent` |
  25. | `nameOverride` | `String to partially override mysqlha.fullname template with a string (will prepend the release name)` | `nil` |
  26. | `fullnameOverride` | `String to fully override mysqlha.fullname template with a string` | `nil` |
  27. | `replicaCount` | Number of MySQL replicas | 3 |
  28. | `mysqlRootPassword` | Password for the `root` user. | Randomly generated |
  29. | `mysqlUser` | Username of new user to create. | `nil` |
  30. | `mysqlPassword` | Password for the new user. | Randomly generated |
  31. | `mysqlReplicationUser` | Username for replication user | `repl` |
  32. | `mysqlReplicationPassword` | Password for replication user. | Randomly generated |
  33. | `mysqlDatabase` | Name of the new Database to create | `nil` |
  34. | `configFiles.master.cnf` | Master configuration file | See `values.yaml` |
  35. | `configFiles.slave.cnf` | Slave configuration file | See `values.yaml` |
  36. | `podAnnotations` | Pod annotations | `nil` |
  37. | `schedulerName` | Name of the k8s scheduler (other than default) | `nil` |
  38. | `persistence.enabled` | Create a volume to store data | true |
  39. | `persistence.size` | Size of persistent volume claim | 10Gi |
  40. | `persistence.storageClass` | Type of persistent volume claim | `nil` |
  41. | `persistence.accessModes` | Persistent volume access modes | `[ReadWriteOnce]` |
  42. | `persistence.annotations` | Persistent volume annotations | `{}` |
  43. | `resources` | CPU/Memory resource requests/limits | Memory: `128Mi`, CPU: `100m` |
  44. | `metrics.enabled` | Start a side-car prometheus exporter | false |
  45. | `metrics.image` | Exporter image | `prom/mysqld-exporter` |
  46. | `metrics.imageTag` | Exporter image | `v0.10.0` |
  47. | `metrics.imagePullPolicy` | Exporter image pull policy | `IfNotPresent` |
  48. | `metrics.resources` | Exporter resource requests/limit | See `values.yaml` |
  49. | `metrics.livenessProbe.initialDelaySeconds` | Delay before metrics liveness probe is initiated | 15 |
  50. | `metrics.livenessProbe.timeoutSeconds` | When the probe times out | 5 |
  51. | `metrics.readinessProbe.initialDelaySeconds` | Delay before metrics readiness probe is initiated | 5 |
  52. | `metrics.readinessProbe.timeoutSeconds` | When the probe times out | 1 |
  53. Specify each parameter using the `--set key=value[,key=value]` argument to `helm install`. For example,
  54. ## Persistence
  55. The [MySQL](https://hub.docker.com/_/mysql/) image stores the MySQL data and configurations at the `/var/lib/mysql` path of the container.
  56. By default persistence is enabled, and a PersistentVolumeClaim is created and mounted in that directory. As a result, a persistent volume will need to be defined:
  57. # https://kubernetes.io/docs/user-guide/persistent-volumes/#azure-disk
  58. kind: StorageClass
  59. apiVersion: storage.k8s.io/v1
  60. metadata:
  61. name: fast
  62. provisioner: kubernetes.io/azure-disk
  63. parameters:
  64. skuName: Premium_LRS
  65. location: westus
  66. In order to disable this functionality you can change the values.yaml to disable persistence and use an emptyDir instead.

查看values.yaml文件:

  1. [root@master mysqlha]# cat values.yaml
  2. ## mysql image version
  3. ## ref: https://hub.docker.com/r/library/mysql/tags/
  4. ##
  5. mysqlImage: mysql:5.7.13 #前面下载的mysql镜像
  6. xtraBackupImage: registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0 #前面下载的xtrabackup镜像
  7. ## Specify an imagePullPolicy (Required)
  8. ## It's recommended to change this to 'Always' if the image tag is 'latest'
  9. ## ref: http://kubernetes.io/docs/user-guide/images/#updating-images
  10. ##
  11. imagePullPolicy: IfNotPresent
  12. ## String to partially override orangehrm.fullname template (will maintain the release name)
  13. ##
  14. # nameOverride: ""
  15. ## String to fully override orangehrm.fullname template
  16. ##
  17. # fullnameOverride: ""
  18. mysqlha:
  19. replicaCount: 3
  20. ## Password for MySQL root user
  21. ##
  22. # mysqlRootPassword: ## Default: random 10 character string
  23. ## Username/password for MySQL replication user
  24. ##
  25. mysqlReplicationUser: repl
  26. # mysqlReplicationPassword:
  27. ## Create a database user
  28. ##
  29. # mysqlUser:
  30. # mysqlPassword: ## Default: random 10 character string
  31. ## Allow unauthenticated access, uncomment to enable
  32. ##
  33. # mysqlAllowEmptyPassword: true
  34. ## Create database with name and grant all permissions to user on startup, if needed
  35. # mysqlDatabase:
  36. ## Configuration files for the master and slaves
  37. ##
  38. configFiles:
  39. master.cnf: |
  40. # Apply this config only on the master.
  41. [mysqld]
  42. log-bin
  43. skip_name_resolve
  44. slave.cnf: |
  45. # Apply this config only on slaves.
  46. [mysqld]
  47. super-read-only
  48. skip_name_resolve
  49. podAnnotations: {}
  50. ## Use an alternate scheduler, e.g. "stork".
  51. ## ref: https://kubernetes.io/docs/tasks/administer-cluster/configure-multiple-schedulers/
  52. ##
  53. # schedulerName:
  54. ## Enable persistence using Persistent Volume Claims
  55. ## ref: http://kubernetes.io/docs/user-guide/persistent-volumes/
  56. ##
  57. persistence:
  58. enabled: true
  59. ## If defined, storageClassName: <storageClass>
  60. ## If set to "-", storageClassName: "", which disables dynamic provisioning
  61. ## If undefined (the default) or set to null, no storageClassName spec is
  62. ## set, choosing the default provisioner. (gp2 on AWS, azure-disk on
  63. ## Azure, standard on GKE, AWS & OpenStack)
  64. ##
  65. # storageClass: "-"
  66. accessModes:
  67. - ReadWriteOnce
  68. size: 10Gi
  69. annotations: {}
  70. resources:
  71. requests:
  72. cpu: 100m
  73. memory: 128Mi
  74. metrics:
  75. enabled: false
  76. image: prom/mysqld-exporter
  77. imageTag: v0.10.0
  78. annotations: {}
  79. livenessProbe:
  80. initialDelaySeconds: 15
  81. timeoutSeconds: 5
  82. readinessProbe:
  83. initialDelaySeconds: 5
  84. timeoutSeconds: 1
  85. resources:
  86. requests:
  87. cpu: 100m
  88. memory: 100Mi
  89. [root@master mysqlha]#
6.查看mysql的有状态应用、pod、svc、pvc等k8s资源
  1. [root@master hrr]# kubectl get statefulset
  2. NAME DESIRED CURRENT AGE
  3. mysql-mysqlha 3 1 18s
  4. [root@master hrr]# kubectl get svc
  5. NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. kubernetes 10.254.0.1 <none> 443/TCP 4d
  7. mysql-mysqlha None <none> 3306/TCP 27s
  8. mysql-mysqlha-readonly 10.254.54.188 <none> 3306/TCP 27s
  9. [root@master hrr]# kubectl get pod
  10. NAME READY STATUS RESTARTS AGE
  11. mysql-mysqlha-0 2/2 Running 0 45s
  12. mysql-mysqlha-1 2/2 Running 0 24s
  13. mysql-mysqlha-2 1/2 Running 0 4s
  14. [root@master hrr]# kubectl get pv
  15. NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
  16. pv001 10Gi RWO Retain Bound default/data-mysql-mysqlha-0 2m
  17. pv002 10Gi RWO Retain Bound default/data-mysql-mysqlha-2 2m
  18. pv003 10Gi RWO Retain Bound default/data-mysql-mysqlha-1 1m
  19. [root@master hrr]# kubectl get pvc
  20. NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
  21. data-mysql-mysqlha-0 Bound pv001 10Gi RWO 51s
  22. data-mysql-mysqlha-1 Bound pv003 10Gi RWO 51s
  23. data-mysql-mysqlha-2 Bound pv002 10Gi RWO 51s
  24. [root@master hrr]# kubectl get configmap
  25. NAME DATA AGE
  26. mysql-mysqlha 4 54s
  27. [root@master hrr]# kubectl get secret
  28. NAME TYPE DATA AGE
  29. mysql-mysqlha Opaque 2 51m
  30. mysql.v1 helm.sh/release 1 51m
  31. [root@master hrr]#
  32. [root@master hrr]# kubectl get pod -owide
  33. NAME READY STATUS RESTARTS AGE IP NODE
  34. mysql-mysqlha-0 2/2 Running 0 1m 172.17.93.2 node3
  35. mysql-mysqlha-1 2/2 Running 0 41s 172.17.78.2 node2
  36. mysql-mysqlha-2 2/2 Running 0 21s 172.17.86.2 master

查看第一个节点的docker容器

  1. [root@master hrr]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 41ec983706b8 registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0 "/bin/bash -c 'set..." 25 seconds ago Up 24 seconds k8s_xtrabackup.516a4a61_mysql-mysqlha-2_default_387e4cdb-c772-11e9-83c0-000c296be0a4_72aba107
  4. 691066dcf870 mysql:5.7.13 "docker-entrypoint..." 25 seconds ago Up 24 seconds k8s_mysql.b9eef278_mysql-mysqlha-2_default_387e4cdb-c772-11e9-83c0-000c296be0a4_894896de
  5. d83f031101ef 192.168.222.129:5000/pod-infrastructure:latest "/pause" 26 seconds ago Up 25 seconds k8s_POD.62b22cd9_mysql-mysqlha-2_default_387e4cdb-c772-11e9-83c0-000c296be0a4_c46314b7

查看第二个节点的docker容器

  1. [root@node2 ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 211a5deed23f registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0 "/bin/bash -c 'set..." 52 seconds ago Up 51 seconds k8s_xtrabackup.516a4a61_mysql-mysqlha-1_default_2c7419c8-c772-11e9-83c0-000c296be0a4_982995a3
  4. 51d9e7cd3d1d mysql:5.7.13 "docker-entrypoint..." 52 seconds ago Up 51 seconds k8s_mysql.b9eef278_mysql-mysqlha-1_default_2c7419c8-c772-11e9-83c0-000c296be0a4_3acd8bf2
  5. 71bb21654249 192.168.222.129:5000/pod-infrastructure:latest "/pause" 52 seconds ago Up 51 seconds k8s_POD.62b22cd9_mysql-mysqlha-1_default_2c7419c8-c772-11e9-83c0-000c296be0a4_cce640c3
  6. [root@node2 ~]#

查看第三个节点的docker容器

  1. [root@node3 ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. d159b422bcdf registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0 "/bin/bash -c 'set..." About a minute ago Up About a minute k8s_xtrabackup.516a4a61_mysql-mysqlha-0_default_1fbcfe77-c772-11e9-83c0-000c296be0a4_63f2a879
  4. 6aae997c995d mysql:5.7.13 "docker-entrypoint..." About a minute ago Up About a minute k8s_mysql.b9eef278_mysql-mysqlha-0_default_1fbcfe77-c772-11e9-83c0-000c296be0a4_7dee904b
  5. 40695f86aaf7 192.168.222.129:5000/pod-infrastructure:latest "/pause" About a minute ago Up About a minute k8s_POD.62b22cd9_mysql-mysqlha-0_default_1fbcfe77-c772-11e9-83c0-000c296be0a4_58c67c1e
  6. [root@node3 ~]#

查看pvc里的数据

  1. [root@master hrr]# ll /nfsdata/pv001
  2. total 4
  3. drwxr-xr-x 6 polkitd input 4096 Aug 26 03:54 mysql
  4. [root@master hrr]# ll /nfsdata/pv001/mysql/
  5. total 177164
  6. -rw-r----- 1 polkitd input 56 Aug 26 03:54 auto.cnf
  7. -rw-r----- 1 polkitd input 3091158 Aug 26 03:54 binlog.000001
  8. -rw-r----- 1 polkitd input 155 Aug 26 03:54 binlog.000002
  9. -rw-r----- 1 polkitd input 32 Aug 26 03:54 binlog.index
  10. -rw------- 1 polkitd input 1676 Aug 26 03:54 ca-key.pem
  11. -rw-r--r-- 1 polkitd input 1112 Aug 26 03:54 ca.pem
  12. -rw-r--r-- 1 polkitd input 1112 Aug 26 03:54 client-cert.pem
  13. -rw------- 1 polkitd input 1680 Aug 26 03:54 client-key.pem
  14. -rw-r----- 1 polkitd input 5419 Aug 26 03:54 ib_buffer_pool
  15. -rw-r----- 1 polkitd input 12582912 Aug 26 03:54 ibdata1
  16. -rw-r----- 1 polkitd input 50331648 Aug 26 03:54 ib_logfile0
  17. -rw-r----- 1 polkitd input 50331648 Aug 26 03:54 ib_logfile1
  18. -rw-r----- 1 polkitd input 12582912 Aug 26 03:55 ibtmp1
  19. drwxr-x--- 2 polkitd input 187 Aug 26 03:54 #innodb_temp
  20. drwxr-x--- 2 polkitd input 143 Aug 26 03:54 mysql
  21. -rw-r----- 1 polkitd input 29360128 Aug 26 03:54 mysql.ibd
  22. drwxr-x--- 2 polkitd input 8192 Aug 26 03:54 performance_schema
  23. -rw------- 1 polkitd input 1680 Aug 26 03:54 private_key.pem
  24. -rw-r--r-- 1 polkitd input 452 Aug 26 03:54 public_key.pem
  25. -rw-r--r-- 1 polkitd input 1112 Aug 26 03:54 server-cert.pem
  26. -rw------- 1 polkitd input 1680 Aug 26 03:54 server-key.pem
  27. drwxr-x--- 2 polkitd input 28 Aug 26 03:54 sys
  28. -rw-r----- 1 polkitd input 12582912 Aug 26 03:54 undo_001
  29. -rw-r----- 1 polkitd input 10485760 Aug 26 03:54 undo_002

查看svc

  1. [root@master hrr]# kubectl describe svc mysql-mysqlha
  2. Name: mysql-mysqlha
  3. Namespace: default
  4. Labels: app=mysql-mysqlha
  5. chart=mysqlha-1.0.0
  6. heritage=Helm
  7. release=mysql
  8. Selector: app=mysql-mysqlha
  9. Type: ClusterIP
  10. IP: None
  11. Port: mysql-mysqlha 3306/TCP
  12. Endpoints: 172.17.78.2:3306,172.17.86.2:3306,172.17.93.2:3306
  13. Session Affinity: None
  14. No events.
  15. [root@master hrr]# kubectl describe svc mysql-mysqlha-readonly
  16. Name: mysql-mysqlha-readonly
  17. Namespace: default
  18. Labels: app=mysql-mysqlha
  19. chart=mysqlha-1.0.0
  20. heritage=Helm
  21. release=mysql
  22. Selector: app=mysql-mysqlha
  23. Type: ClusterIP
  24. IP: 10.254.54.188
  25. Port: mysql-mysqlha 3306/TCP
  26. Endpoints: 172.17.78.2:3306,172.17.86.2:3306,172.17.93.2:3306
  27. Session Affinity: None
  28. No events.
  29. [root@master hrr]#

查看有状态应用stateful

  1. [root@master hrr]# kubectl get statefulset -oyaml
  2. apiVersion: v1
  3. items:
  4. - apiVersion: apps/v1beta1
  5. kind: StatefulSet
  6. metadata:
  7. creationTimestamp: 2019-08-25T19:54:18Z
  8. generation: 1
  9. labels:
  10. app: mysql-mysqlha
  11. chart: mysqlha-1.0.0
  12. heritage: Helm
  13. release: mysql
  14. name: mysql-mysqlha
  15. namespace: default
  16. resourceVersion: "94760"
  17. selfLink: /apis/apps/v1beta1/namespaces/default/statefulsets/mysql-mysqlha
  18. uid: 1fa6f230-c772-11e9-83c0-000c296be0a4
  19. spec:
  20. replicas: 3
  21. selector:
  22. matchLabels:
  23. app: mysql-mysqlha
  24. serviceName: mysql-mysqlha
  25. template:
  26. metadata:
  27. creationTimestamp: null
  28. labels:
  29. app: mysql-mysqlha
  30. spec:
  31. containers:
  32. - env:
  33. - name: MYSQL_DATABASE
  34. - name: MYSQL_ROOT_PASSWORD
  35. valueFrom:
  36. secretKeyRef:
  37. key: mysql-root-password
  38. name: mysql-mysqlha
  39. - name: MYSQL_REPLICATION_USER
  40. value: repl
  41. - name: MYSQL_REPLICATION_PASSWORD
  42. valueFrom:
  43. secretKeyRef:
  44. key: mysql-replication-password
  45. name: mysql-mysqlha
  46. image: mysql:5.7.13
  47. imagePullPolicy: IfNotPresent
  48. livenessProbe:
  49. exec:
  50. command:
  51. - /bin/sh
  52. - -c
  53. - mysqladmin ping -h 127.0.0.1 -u root -p${MYSQL_ROOT_PASSWORD}
  54. failureThreshold: 3
  55. initialDelaySeconds: 30
  56. periodSeconds: 10
  57. successThreshold: 1
  58. timeoutSeconds: 5
  59. name: mysql
  60. ports:
  61. - containerPort: 3306
  62. name: mysql
  63. protocol: TCP
  64. readinessProbe:
  65. exec:
  66. command:
  67. - /bin/sh
  68. - -c
  69. - MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"
  70. - mysql -h 127.0.0.1 -u root -e "SELECT 1"
  71. failureThreshold: 3
  72. initialDelaySeconds: 10
  73. periodSeconds: 10
  74. successThreshold: 1
  75. timeoutSeconds: 1
  76. resources:
  77. requests:
  78. cpu: 100m
  79. memory: 128Mi
  80. terminationMessagePath: /dev/termination-log
  81. volumeMounts:
  82. - mountPath: /var/lib/mysql
  83. name: data
  84. subPath: mysql
  85. - mountPath: /etc/mysql/conf.d
  86. name: conf
  87. - args:
  88. - -c
  89. - |
  90. set -ex
  91. echo "Waiting for mysqld to be ready (accepting connections)"
  92. until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 5; done
  93. # Create replication user
  94. cd /mnt/scripts
  95. # file exists and is not empty with -s
  96. if [[ -s create-replication-user.sh ]]; then
  97. ls -la
  98. ./create-replication-user.sh
  99. fi
  100. cd /var/lib/mysql
  101. # Determine binlog position of cloned data, if any.
  102. if [[ -f xtrabackup_slave_info ]]; then
  103. # XtraBackup already generated a partial "CHANGE MASTER TO" query
  104. # because we're cloning from an existing slave.
  105. cp xtrabackup_slave_info change_master_to.sql.in
  106. elif [[ -f xtrabackup_binlog_info ]]; then
  107. # We're cloning directly from master. Parse binlog position.
  108. [[ $(cat xtrabackup_binlog_info) =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
  109. echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
  110. MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
  111. fi
  112. # Check if we need to complete a clone by starting replication.
  113. if [[ -f change_master_to.sql.in ]]; then
  114. # In case of container restart, attempt this at-most-once.
  115. cp change_master_to.sql.in change_master_to.sql.orig
  116. mysql -h 127.0.0.1 --verbose<<EOF
  117. STOP SLAVE IO_THREAD;
  118. $(<change_master_to.sql.orig),
  119. MASTER_HOST='mysql-mysqlha-0.mysql-mysqlha',
  120. MASTER_USER='${MYSQL_REPLICATION_USER}',
  121. MASTER_PASSWORD='${MYSQL_REPLICATION_PASSWORD}',
  122. MASTER_CONNECT_RETRY=10;
  123. START SLAVE;
  124. EOF
  125. fi
  126. # Start a server to send backups when requested by peers.
  127. exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
  128. "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=${MYSQL_REPLICATION_USER} --password=${MYSQL_REPLICATION_PASSWORD}"
  129. command:
  130. - /bin/bash
  131. env:
  132. - name: MYSQL_PWD
  133. valueFrom:
  134. secretKeyRef:
  135. key: mysql-root-password
  136. name: mysql-mysqlha
  137. - name: MYSQL_REPLICATION_USER
  138. value: repl
  139. - name: MYSQL_REPLICATION_PASSWORD
  140. valueFrom:
  141. secretKeyRef:
  142. key: mysql-replication-password
  143. name: mysql-mysqlha
  144. image: registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0
  145. imagePullPolicy: IfNotPresent
  146. name: xtrabackup
  147. ports:
  148. - containerPort: 3307
  149. name: xtrabackup
  150. protocol: TCP
  151. resources:
  152. requests:
  153. cpu: 100m
  154. memory: 100Mi
  155. terminationMessagePath: /dev/termination-log
  156. volumeMounts:
  157. - mountPath: /var/lib/mysql
  158. name: data
  159. subPath: mysql
  160. - mountPath: /etc/mysql/conf.d
  161. name: conf
  162. - mountPath: /mnt/scripts
  163. name: scripts
  164. dnsPolicy: ClusterFirst
  165. restartPolicy: Always
  166. securityContext: {}
  167. terminationGracePeriodSeconds: 30
  168. volumes:
  169. - emptyDir: {}
  170. name: conf
  171. - configMap:
  172. defaultMode: 420
  173. name: mysql-mysqlha
  174. name: config-map
  175. - emptyDir: {}
  176. name: scripts
  177. volumeClaimTemplates:
  178. - metadata:
  179. creationTimestamp: null
  180. name: data
  181. spec:
  182. accessModes:
  183. - ReadWriteOnce
  184. resources:
  185. requests:
  186. storage: 10Gi
  187. status:
  188. phase: Pending
  189. status:
  190. replicas: 3
  191. kind: List
  192. metadata: {}
  193. resourceVersion: ""
  194. selfLink: ""
  195. [root@master hrr]#
7.客户端验证

创建mysql-client容器,参考前面“ helm install mysql apphub/mysqlha”中的

  1. [root@master hrr]# kubectl run mysql-client --image=mysql:5.7.13 -it --rm --restart='Never' --namespace default -- /bin/sh
  2. Waiting for pod default/mysql-client to be running, status is Pending, pod ready: false If you don't see a command prompt, try pressing enter.
  3. Error attaching, falling back to logs: error dialing backend: dial tcp: lookup node3 on 192.168.222.2:53: no such host
  4. Error from server: Get https://node3:10250/containerLogs/default/mysql-client/mysql-client: dial tcp: lookup node3 on 192.168.222.2:53: no such host
  5. [root@master hrr]#
  6. [root@master hrr]# kubectl get pod -owide
  7. NAME READY STATUS RESTARTS AGE IP NODE
  8. mysql-client 1/1 Running 0 48s 172.17.93.3 node3
  9. mysql-mysqlha-0 2/2 Running 0 54m 172.17.93.2 node3
  10. mysql-mysqlha-1 2/2 Running 0 53m 172.17.78.2 node2
  11. mysql-mysqlha-2 2/2 Running 0 53m 172.17.86.2 master
  12. [root@master hrr]#

在node3上查看

  1. [root@node3 ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 8261abe363b0 mysql:5.7.13 "docker-entrypoint..." 56 seconds ago Up 55 seconds k8s_mysql-client.d32e2e5_mysql-client_default_90905092-c779-11e9-83c0-000c296be0a4_0d4f202f
  4. 1fac37badaa8 192.168.222.129:5000/pod-infrastructure:latest "/pause" 56 seconds ago Up 56 seconds k8s_POD.cace165_mysql-client_default_90905092-c779-11e9-83c0-000c296be0a4_a0f9ab22
  5. d159b422bcdf registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0 "/bin/bash -c 'set..." 54 minutes ago Up 54 minutes k8s_xtrabackup.516a4a61_mysql-mysqlha-0_default_1fbcfe77-c772-11e9-83c0-000c296be0a4_63f2a879
  6. 6aae997c995d mysql:5.7.13 "docker-entrypoint..." 54 minutes ago Up 54 minutes k8s_mysql.b9eef278_mysql-mysqlha-0_default_1fbcfe77-c772-11e9-83c0-000c296be0a4_7dee904b
  7. 40695f86aaf7 192.168.222.129:5000/pod-infrastructure:latest "/pause" 54 minutes ago Up 54 minutes k8s_POD.62b22cd9_mysql-mysqlha-0_default_1fbcfe77-c772-11e9-83c0-000c296be0a4_58c67c1e
  8. [root@node3 ~]#

写入数据

  1. [root@node3 ~]# docker exec -ti 8261abe363b0 sh
  2. # mysql -h172.17.93.2 -uroot
  3. ERROR 1045 (28000): Access denied for user 'root'@'172.17.93.3' (using password: NO)
  4. # mysql -h172.17.93.2 -uroot -pghScMZT1bTJN
  5. mysql: [Warning] Using a password on the command line interface can be insecure.
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 1018
  8. Server version: 8.0.17 MySQL Community Server - GPL
  9. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  10. Oracle is a registered trademark of Oracle Corporation and/or its
  11. affiliates. Other names may be trademarks of their respective
  12. owners.
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14. mysql> show databases;
  15. +--------------------+
  16. | Database |
  17. +--------------------+
  18. | information_schema |
  19. | mysql |
  20. | performance_schema |
  21. | sys |
  22. +--------------------+
  23. 4 rows in set (0.01 sec)
  24. mysql> CREATE DATABASE test;
  25. Query OK, 1 row affected (0.02 sec)
  26. mysql> CREATE TABLE test.messages (message VARCHAR(250));
  27. Query OK, 0 rows affected (0.03 sec)
  28. mysql> INSERT INTO test.messages VALUES ('hello');
  29. Query OK, 1 row affected (0.03 sec)
  30. mysql> show databases;
  31. +--------------------+
  32. | Database |
  33. +--------------------+
  34. | information_schema |
  35. | mysql |
  36. | performance_schema |
  37. | sys |
  38. | test |
  39. +--------------------+
  40. 5 rows in set (0.00 sec)
  41. mysql> use test;
  42. Reading table information for completion of table and column names
  43. You can turn off this feature to get a quicker startup with -A
  44. Database changed
  45. mysql> select * from messages;
  46. +---------+
  47. | message |
  48. +---------+
  49. | hello |
  50. +---------+
  51. 1 row in set (0.01 sec)
  52. mysql> exit
  53. Bye
  54. #

连接svc查看,这个是read-only的,可以看到是轮询的,查询了2次才到有test中的库去

  1. # mysql -h10.254.54.188 -uroot -pghScMZT1bTJN
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 1058
  5. Server version: 8.0.17 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> show databases;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | information_schema |
  16. | mysql |
  17. | performance_schema |
  18. | sys |
  19. +--------------------+
  20. 4 rows in set (0.00 sec)
  21. mysql> exit
  22. Bye
  23. # mysql -h10.254.54.188 -uroot -pghScMZT1bTJN
  24. mysql: [Warning] Using a password on the command line interface can be insecure.
  25. Welcome to the MySQL monitor. Commands end with ; or \g.
  26. Your MySQL connection id is 1054
  27. Server version: 8.0.17 MySQL Community Server - GPL
  28. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  29. Oracle is a registered trademark of Oracle Corporation and/or its
  30. affiliates. Other names may be trademarks of their respective
  31. owners.
  32. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  33. mysql> show databases;
  34. +--------------------+
  35. | Database |
  36. +--------------------+
  37. | information_schema |
  38. | mysql |
  39. | performance_schema |
  40. | sys |
  41. +--------------------+
  42. 4 rows in set (0.01 sec)
  43. mysql> exit
  44. Bye
  45. # mysql -h10.254.54.188 -uroot -pghScMZT1bTJN
  46. mysql: [Warning] Using a password on the command line interface can be insecure.
  47. Welcome to the MySQL monitor. Commands end with ; or \g.
  48. Your MySQL connection id is 1072
  49. Server version: 8.0.17 MySQL Community Server - GPL
  50. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  51. Oracle is a registered trademark of Oracle Corporation and/or its
  52. affiliates. Other names may be trademarks of their respective
  53. owners.
  54. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  55. mysql> show databases;
  56. +--------------------+
  57. | Database |
  58. +--------------------+
  59. | information_schema |
  60. | mysql |
  61. | performance_schema |
  62. | sys |
  63. | test |
  64. +--------------------+
  65. 5 rows in set (0.00 sec)
  66. mysql> exit
  67. Bye
  68. #

四、故障排除

1.helm init报错

故障现象:

  1. [root@docker ~]# helm init
  2. Creating /root/.helm/repository/repositories.yaml
  3. Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com
  4. Error: https://kubernetes-charts.storage.googleapis.com is not a valid chart repository or cannot be reached: Get https://kubernetes-charts.storage.googleapis.com/index.yaml: net/http: TLS handshake timeout
  5. [root@docker ~]#

可能原因:
可能国内访问不了https://kubernetes-charts.storage.googleapis.com/index.yaml,并且网速慢。
解决方法:
在初始化时,使用--stable-repo-url参数,将仓库地址改为阿里云的helm仓库地址。
helm init --stable-repo-url=https://apphub.aliyuncs.com

2.Pending、PersistentVolumeClaim is not bound

故障现象:

  1. [root@docker ~]# kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. mysql-mysqlha-0 0/2 Pending 0 35s
  4. nginx 1/1 Running 0 2m
  5. [root@docker ~]#
  6. [root@docker ~]# kubectl describe pod mysql-mysqlha-0
  7. ……
  8. Events:
  9. FirstSeen LastSeen Count From SubObjectPath Type Reason Message
  10. --------- -------- ----- ---- ------------- -------- ------ -------
  11. 1m 4s 8 {default-scheduler } Warning FailedScheduling SchedulerPredicates failed due to PersistentVolumeClaim is not bound: "data-mysql-mysqlha-0", which is unexpected.

可能原因:
第一次使用helm安装mysql时,没有提前创建pv,导致报pvc没有bound的错。
解决方法:
查看pvc的模式,创建pv,创建过程请参考前面。

3.RunContainerError、Failed to pull image "mysql:5.7.13"

故障现象:

  1. [root@docker hrr]# kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. mysql-mysqlha-0 0/2 RunContainerError 0 19s
  4. nginx 1/1 Running 1 1h
  5. [root@docker hrr]#
  6. [root@docker ~]# kubectl describe pod mysql-mysqlha-0
  7. ……
  8. Events:
  9. FirstSeen LastSeen Count From SubObjectPath Type Reason Message
  10. --------- -------- ----- ---- ------------- -------- ------ -------
  11. 18m 5m 49 {default-scheduler } Warning FailedScheduling SchedulerPredicates failed due to PersistentVolumeClaim is not bound: "data-mysql-mysqlha-0", which is unexpected.
  12. 4m 4m 1 {default-scheduler } Normal Scheduled Successfully assigned mysql-mysqlha-0 to master
  13. 4m 4m 1 {kubelet master} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  14. 4m 4m 1 {kubelet master} spec.containers{mysql} Normal Pulling pulling image "mysql:5.7.13"
  15. 10s 10s 1 {kubelet master} spec.containers{mysql} Warning Failed Failed to pull image "mysql:5.7.13": Get https://registry-1.docker.io/v2/library/mysql/manifests/5.7.13: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fmysql%3Apull&service=registry.docker.io: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
  16. 10s 10s 1 {kubelet master} spec.containers{xtrabackup} Normal Pulling pulling image "registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0"
  17. 9s 9s 1 {kubelet master} spec.containers{xtrabackup} Warning Failed Failed to pull image "registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0": image pull failed for registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0, this may be because there are no credentials on this request. details: (repository registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup not found: does not exist or no pull access)
  18. 9s 9s 1 {kubelet master} Warning FailedSync Error syncing pod, skipping: [failed to "StartContainer" for "mysql" with ErrImagePull: "Get https://registry-1.docker.io/v2/library/mysql/manifests/5.7.13: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fmysql%3Apull&service=registry.docker.io: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
  19. , failed to "StartContainer" for "xtrabackup" with ErrImagePull: "image pull failed for registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0, this may be because there are no credentials on this request. details: (repository registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup not found: does not exist or no pull access)"
  20. ]
  21. 8s 8s 1 {kubelet master} spec.containers{mysql} Normal BackOff Back-off pulling image "mysql:5.7.13"
  22. 8s 8s 1 {kubelet master} spec.containers{xtrabackup} Normal BackOff Back-off pulling image "registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0"
  23. 8s 8s 1 {kubelet master} Warning FailedSync Error syncing pod, skipping: [failed to "StartContainer" for "mysql" with ImagePullBackOff: "Back-off pulling image \"mysql:5.7.13\""
  24. , failed to "StartContainer" for "xtrabackup" with ImagePullBackOff: "Back-off pulling image \"registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0\""
  25. ]
  26. [root@docker hrr]#

可能原因:
下载不到docker image,本地没有mysql镜像。
解决方法:
手动下载mysql镜像,将镜像docker tag为配置中的名字,如何手动下载,请参考上面。

4.CrashLoopBackOff、permission denied

故障现象:

  1. [root@docker docker]# kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. mysql-mysqlha-0 0/2 RunContainerError 0 9s
  4. nginx 1/1 Running 2 1h
  5. [root@docker docker]#
  6. [root@docker docker]# kubectl describe pod mysql-mysqlha-0
  7. ……
  8. Events:
  9. FirstSeen LastSeen Count From SubObjectPath Type Reason Message
  10. --------- -------- ----- ---- ------------- -------- ------ -------
  11. 15s 15s 1 {default-scheduler } Normal Scheduled Successfully assigned mysql-mysqlha-0 to master
  12. 14s 14s 1 {kubelet master} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  13. 14s 3s 3 {kubelet master} spec.containers{mysql} Normal Pulled Container image "mysql:5.7.13" already present on machine
  14. 14s 3s 3 {kubelet master} spec.containers{xtrabackup} Normal Pulled Container image "registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0" already present on machine
  15. 14s 3s 3 {kubelet master} Warning FailedSync Error syncing pod, skipping: [failed to "StartContainer" for "mysql" with RunContainerError: "GenerateRunContainerOptions: mkdir /var/lib/kubelet/pods/18f319d5-c6d2-11e9-baf7-000c296be0a4/volumes/kubernetes.io~nfs/pv001/mysql: permission denied"
  16. , failed to "StartContainer" for "xtrabackup" with RunContainerError: "GenerateRunContainerOptions: mkdir /var/lib/kubelet/pods/18f319d5-c6d2-11e9-baf7-000c296be0a4/volumes/kubernetes.io~nfs/pv001/mysql: permission denied"
  17. ]
  18. [root@docker docker]#
  19. [root@docker docker]# kubectl get pod
  20. NAME READY STATUS RESTARTS AGE
  21. mysql-mysqlha-0 1/2 CrashLoopBackOff 2 5m
  22. nginx 1/1 Running 2 1h
  23. [root@docker docker]#
  24. Events:
  25. FirstSeen LastSeen Count From SubObjectPath Type Reason Message
  26. --------- -------- ----- ---- ------------- -------- ------ -------
  27. 5m 5m 1 {default-scheduler } Normal Scheduled Successfully assigned mysql-mysqlha-0 to master
  28. 1m 1m 1 {kubelet master} Warning FailedSync Error syncing pod, skipping: [failed to "StartContainer" for "xtrabackup" with RunContainerError: "GenerateRunContainerOptions: mkdir /var/lib/kubelet/pods/18f319d5-c6d2-11e9-baf7-000c296be0a4/volumes/kubernetes.io~nfs/pv001/mysql: permission denied"
  29. , failed to "StartContainer" for "mysql" with RunContainerError: "GenerateRunContainerOptions: mkdir /var/lib/kubelet/pods/18f319d5-c6d2-11e9-baf7-000c296be0a4/volumes/kubernetes.io~nfs/pv001/mysql: permission denied"
  30. ]
  31. 5m 44s 23 {kubelet master} Warning FailedSync Error syncing pod, skipping: [failed to "StartContainer" for "mysql" with RunContainerError: "GenerateRunContainerOptions: mkdir /var/lib/kubelet/pods/18f319d5-c6d2-11e9-baf7-000c296be0a4/volumes/kubernetes.io~nfs/pv001/mysql: permission denied"
  32. , failed to "StartContainer" for "xtrabackup" with RunContainerError: "GenerateRunContainerOptions: mkdir /var/lib/kubelet/pods/18f319d5-c6d2-11e9-baf7-000c296be0a4/volumes/kubernetes.io~nfs/pv001/mysql: permission denied"
  33. ]
  34. 32s 32s 1 {kubelet master} spec.containers{mysql} Normal Started Started container with docker id d2f5e2c645b8
  35. 32s 32s 1 {kubelet master} spec.containers{xtrabackup} Normal Started Started container with docker id 6f2fb901a587
  36. 5m 32s 25 {kubelet master} spec.containers{xtrabackup} Normal Pulled Container image "registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0" already present on machine
  37. 32s 32s 1 {kubelet master} spec.containers{mysql} Normal Created Created container with docker id d2f5e2c645b8; Security:[seccomp=unconfined]
  38. 32s 32s 1 {kubelet master} spec.containers{xtrabackup} Normal Created Created container with docker id 6f2fb901a587; Security:[seccomp=unconfined]
  39. 30s 30s 1 {kubelet master} spec.containers{mysql} Normal Created Created container with docker id 104f64db6e8c; Security:[seccomp=unconfined]
  40. 30s 30s 1 {kubelet master} spec.containers{mysql} Normal Started Started container with docker id 104f64db6e8c
  41. 28s 26s 3 {kubelet master} Warning FailedSync Error syncing pod, skipping: failed to "StartContainer" for "mysql" with CrashLoopBackOff: "Back-off 10s restarting failed container=mysql pod=mysql-mysqlha-0_default(18f319d5-c6d2-11e9-baf7-000c296be0a4)"
  42. 5m 11s 27 {kubelet master} spec.containers{mysql} Normal Pulled Container image "mysql:5.7.13" already present on machine
  43. 5m 11s 5 {kubelet master} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  44. 11s 11s 1 {kubelet master} spec.containers{mysql} Normal Created Created container with docker id ec8f0827294b; Security:[seccomp=unconfined]
  45. 11s 11s 1 {kubelet master} spec.containers{mysql} Normal Started Started container with docker id ec8f0827294b
  46. 28s 7s 5 {kubelet master} spec.containers{mysql} Warning BackOff Back-off restarting failed docker container
  47. 10s 7s 2 {kubelet master} Warning FailedSync Error syncing pod, skipping: failed to "StartContainer" for "mysql" with CrashLoopBackOff: "Back-off 20s restarting failed container=mysql pod=mysql-mysqlha-0_default(18f319d5-c6d2-11e9-baf7-000c296be0a4)"
  48. [root@docker docker]#
  49. [root@docker docker]# docker ps -a
  50. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  51. 24a5e40cfbe6 mysql:5.7.13 "docker-entrypoint..." About a minute ago Exited (1) About a minute ago k8s_mysql.b9eef278_mysql-mysqlha-0_default_18f319d5-c6d2-11e9-baf7-000c296be0a4_f37f9154
  52. 6f2fb901a587 registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0 "/bin/bash -c 'set..." 5 minutes ago Up 5 minutes k8s_xtrabackup.516a4a61_mysql-mysqlha-0_default_18f319d5-c6d2-11e9-baf7-000c296be0a4_4c633d64
  53. 61a82b9ad65a 192.168.222.129:5000/pod-infrastructure:latest "/pause" 10 minutes ago Up 10 minutes k8s_POD.62b22cd9_mysql-mysqlha-0_default_18f319d5-c6d2-11e9-baf7-000c296be0a4_9f7337a5
  54. dc6071e8f613 192.168.222.129:5000/nginx "nginx -g 'daemon ..." 26 minutes ago Up 26 minutes k8s_nginx.3556dff8_nginx_default_30a915ea-c6c5-11e9-bbd7-000c296be0a4_9d257b60
  55. 15929311253f 192.168.222.129:5000/pod-infrastructure:latest "/pause" 26 minutes ago Up 26 minutes k8s_POD.cace165_nginx_default_30a915ea-c6c5-11e9-bbd7-000c296be0a4_d9ad1e0f
  56. 48f464ad9c9b 192.168.222.129:5000/pod-infrastructure:latest "/pause" 37 minutes ago Exited (2) 26 minutes ago k8s_POD.cace165_nginx_default_30a915ea-c6c5-11e9-bbd7-000c296be0a4_eb3bebdb
  57. c01c41763b06 192.168.222.129:5000/nginx "nginx -g 'daemon ..." About an hour ago Exited (0) 37 minutes ago k8s_nginx.3556dff8_nginx_default_30a915ea-c6c5-11e9-bbd7-000c296be0a4_0848bc8b
  58. [root@docker docker]# docker logs 24a5e40cfbe6
  59. chown: changing ownership of '/var/lib/mysql/': Operation not permitted
  60. [root@docker docker]#

可能原因:
docker对pvc没有权限,由于这里使用的是nfs作后端存储,所以可能是nfs目录的权限没放开。
解决方法:
将nfs目录权限设置为chown nfsnobody:nfsnobody -R /nfsdata,并且/etc/exportfs中设置no_root_squash,不压缩root权限,即在docker可以使用任何用户写入数据,具体操作步骤请参考上面。

5.MissingClusterDNS

故障现象:

  1. [root@master ~]# kubectl describe pod mysql-mysqlha-0
  2. ……
  3. Events:
  4. FirstSeen LastSeen Count From SubObjectPath Type Reason Message
  5. --------- -------- ----- ---- ------------- -------- ------ -------
  6. 56m 56m 1 {default-scheduler } Normal Scheduled Successfully assigned mysql-mysqlha-0 to master
  7. 56m 56m 3 {kubelet master} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  8. 56m 56m 1 {kubelet master} spec.containers{mysql} Normal Pulled Container image "mysql:5.7.13" already present on machine
  9. 56m 56m 1 {kubelet master} spec.containers{mysql} Normal Created Created container with docker id 31013a7822ae; Security:[seccomp=unconfined]
  10. 56m 56m 1 {kubelet master} spec.containers{mysql} Normal Started Started container with docker id 31013a7822ae
  11. 56m 56m 1 {kubelet master} spec.containers{xtrabackup} Normal Pulled Container image "registry.cn-hangzhou.aliyuncs.com/kubeapps/gcr-xtrabackup:1.0" already present on machine
  12. 56m 56m 1 {kubelet master} spec.containers{xtrabackup} Normal Created Created container with docker id a5f2f49c1eef; Security:[seccomp=unconfined]
  13. 56m 56m 1 {kubelet master} spec.containers{xtrabackup} Normal Started Started container with docker id a5f2f49c1eef

可能原因:
没有配置kube-dns。
解决方法:
请参考上面配置kube-dns。

故障现象:
可能原因:
解决方法:

五、参考资料

K8s Helm Charts 一键安装云原生应用指南:
https://mp.weixin.qq.com/s/qeLA2F4vNGUDYVdQSHIWtQ
mysql有状态服务部署:
https://segmentfault.com/a/1190000012244714
记k8s的一次pv&pvc创建过程:
https://staight.github.io/2018/08/18/%E8%AE%B0k8s%E7%9A%84%E4%B8%80%E6%AC%A1pv&pvc%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/
nfs挂载Permission denied问题解决:
https://blog.csdn.net/weixin_42256397/article/details/97283989

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/523646
推荐阅读
相关标签
  

闽ICP备14008679号