【雲原生】MySQL on k8s 環境部署

語言: CN / TW / HK

一、概述

MySQL是一個關係型資料庫管理系統,由瑞典MySQL AB 公司開發,屬於 Oracle 旗下產品。MySQL 是最流行的關係型資料庫管理系統之一,在 WEB 應用方面,MySQL是最好的 RDBMS (Relational Database Management System,關係資料庫管理系統) 應用軟體之一。這裡主要講mysql部署在k8s上,mysql部署在k8s上的優勢主要有以下幾點:

  • 資源隔離
  • 動態彈性擴縮容
  • 環境一致性
  • 運維方便

官方文件:https://docs.oracle.com/en-us/iaas/mysql-database/doc/getting-started.html MySQL 原理介紹也可以參考我這篇文章:MySQL原理介紹

在這裡插入圖片描述

二、開始部署(一主兩從)

在這裡插入圖片描述

1)新增源

bash helm repo add bitnami https://charts.bitnami.com/bitnami helm pull bitnami/mysql tar -xf mysql-9.3.3.tgz

2)修改配置

  • 修改mysql/values.yaml

```bash ...

image: registry: myharbor.com repository: bigdata/mysql tag: 8.0.30-debian-11-r15

...

architecture: replication

...

primary: persistence: enabled: true size: 10Gi storageClass: "mysql-local-storage" # 目錄需要提前在宿主機上建立 local: - name: mysql-0 host: "local-168-182-110" path: "/opt/bigdata/servers/mysql/data/data1" service: type: NodePort nodePorts: mysql: "30306"

secondary: replicaCount: 2 persistence: enabled: true size: 10Gi storageClass: "mysql-local-storage" # 目錄需要提前在宿主機上建立 local: - name: mysql-1 host: "local-168-182-111" path: "/opt/bigdata/servers/mysql/data/data1" - name: mysql-2 host: "local-168-182-112" path: "/opt/bigdata/servers/mysql/data/data1" service: type: NodePort nodePorts: mysql: "30307"

...

metrics: ## @param metrics.enabled Start a side-car prometheus exporter ## enabled: true image: registry: myharbor.com repository: bigdata/mysqld-exporter tag: 0.14.0-debian-11-r33 `` - 新增mysql/templates/pv.yaml`

```bash {{- range .Values.primary.persistence.local }}


apiVersion: v1 kind: PersistentVolume metadata: name: {{ .name }} labels: name: {{ .name }} spec: storageClassName: {{ $.Values.primary.persistence.storageClass }} capacity: storage: {{ $.Values.primary.persistence.size }} accessModes: - ReadWriteOnce local: path: {{ .path }} nodeAffinity: required: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - {{ .host }}


{{- end }}

{{- range .Values.secondary.persistence.local }}

apiVersion: v1 kind: PersistentVolume metadata: name: {{ .name }} labels: name: {{ .name }} spec: storageClassName: {{ $.Values.secondary.persistence.storageClass }} capacity: storage: {{ $.Values.secondary.persistence.size }} accessModes: - ReadWriteOnce local: path: {{ .path }} nodeAffinity: required: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - {{ .host }}


{{- end }} `` - 新增mysql/templates/storage-class.yaml`

bash kind: StorageClass apiVersion: storage.k8s.io/v1 metadata: name: {{ .Values.primary.persistence.storageClass }} provisioner: kubernetes.io/no-provisioner

3)開始安裝

```bash

建立持久化目錄

mkdir -p /opt/bigdata/servers/mysql/data/data1

先準備好映象

docker pull docker.io/bitnami/mysql:8.0.30-debian-11-r15 docker tag docker.io/bitnami/mysql:8.0.30-debian-11-r15 myharbor.com/bigdata/mysql:8.0.30-debian-11-r15 docker push myharbor.com/bigdata/mysql:8.0.30-debian-11-r15

mysqld-exporter

docker pull docker.io/bitnami/mysqld-exporter:0.14.0-debian-11-r33 docker tag docker.io/bitnami/mysqld-exporter:0.14.0-debian-11-r33 myharbor.com/bigdata/mysqld-exporter:0.14.0-debian-11-r33 docker push myharbor.com/bigdata/mysqld-exporter:0.14.0-debian-11-r33

開始安裝

helm install mysql ./mysql -n mysql --create-namespace ``` NOTES

```bash NAME: mysql LAST DEPLOYED: Mon Sep 19 23:57:18 2022 NAMESPACE: mysql STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: CHART NAME: mysql CHART VERSION: 9.3.3 APP VERSION: 8.0.30

Please be patient while the chart is being deployed

Tip:

Watch the deployment status using the command: kubectl get pods -w --namespace mysql

Services:

echo Primary: mysql-primary.mysql.svc.cluster.local:3306 echo Secondary: mysql-secondary.mysql.svc.cluster.local:3306

Execute the following to get the administrator credentials:

echo Username: root MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace mysql mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d)

To connect to your database:

  1. Run a pod that you can use as a client:

    kubectl run mysql-client --rm --tty -i --restart='Never' --image myharbor.com/bigdata/mysql:8.0.30-debian-11-r15 --namespace mysql --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash

  2. To connect to primary service (read/write):

    mysql -h mysql-primary.mysql.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"

  3. To connect to secondary service (read-only):

    mysql -h mysql-secondary.mysql.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"

To access the MySQL Prometheus metrics from outside the cluster execute the following commands:

kubectl port-forward --namespace mysql svc/mysql-metrics 9104:9104 &
curl http://127.0.0.1:9104/metrics

``` 在這裡插入圖片描述 檢視pod狀態

bash kubectl get pods,svc -n mysql -owide 在這裡插入圖片描述

4)測試驗證

【溫馨提示】從庫(slave)是隻讀的。就是簡單的讀寫測試,還有就是pod掛了,能否正常拉起等等。這個測試驗證比較簡單。這裡就不一步步的演示了。

5)Prometheus監控

Prometheus:https://prometheus.k8s.local/targets?search=mysql 在這裡插入圖片描述 可以通過命令檢視採集資料

bash kubectl get --raw http://10.244.0.74:9104/metrics kubectl get --raw http://10.244.1.125:9104/metrics kubectl get --raw http://10.244.2.178:9104/metrics Grafana:https://grafana.k8s.local/ 賬號:admin,密碼通過下面命令獲取

bash kubectl get secret --namespace grafana grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo 匯入grafana模板,叢集資源監控:7362 官方模組下載地址:https://grafana.com/grafana/dashboards/ 在這裡插入圖片描述

6)解除安裝

```bash helm uninstall mysql -n mysql

kubectl delete pod -n mysql kubectl get pod -n mysql |awk 'NR>1{print $1}' --force kubectl patch ns mysql -p '{"metadata":{"finalizers":null}}' kubectl delete ns mysql --force ```

這裡只是實現了mysql 的主從,沒有實現高可用,官方目前沒有出mysql ok k8s高可用的實現方案,雖然網上也有高可用的實現方案,但是我們公司沒真正去落地使用,所以不予評價網上的高可用的方案,有興趣的小夥伴可以去試試,其實mysql在大資料領域一般只是作為元資料儲存,主掛了,影響不是很大。目前也在研究mysql on k8s 的高可用實現方案,如果後期有mysql on k8s高可用真正落地到生產環境中使用,到時候再來分享一下。MySQL on k8s 環境部署就先這裡了,有疑問的小夥伴歡迎給我留言~