From d1a2b4fd0c697ca968424a55396e276455742875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=A4=A9=E5=9E=9A?= Date: Wed, 20 Dec 2023 15:49:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=83=A8=E7=BD=B2MySQL8?= =?UTF-8?q?=E3=80=81MariaDB10=E5=8D=95=E6=9C=BA=E7=89=88=E5=8F=8A=E4=B8=BB?= =?UTF-8?q?=E4=BB=8E=E9=9B=86=E7=BE=A4=E7=89=88=E7=9B=B8=E5=85=B3=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/mariadb/mariadb-10-cluster.yaml | 508 +++++++++++++++++++++++ examples/mariadb/mariadb-10-single.yaml | 220 ++++++++++ examples/mysql/mysql-8-cluster.yaml | 10 +- examples/mysql/mysql-8-single.yaml | 6 +- 4 files changed, 736 insertions(+), 8 deletions(-) create mode 100644 examples/mariadb/mariadb-10-cluster.yaml create mode 100644 examples/mariadb/mariadb-10-single.yaml diff --git a/examples/mariadb/mariadb-10-cluster.yaml b/examples/mariadb/mariadb-10-cluster.yaml new file mode 100644 index 0000000..2fff615 --- /dev/null +++ b/examples/mariadb/mariadb-10-cluster.yaml @@ -0,0 +1,508 @@ +--- + +# 创建 Namespace +apiVersion: v1 +kind: Namespace +metadata: + name: hty1024-db + +--- + +# 创建 StorageClass +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: storage-local-mariadb + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: storageClass +provisioner: kubernetes.io/no-provisioner +reclaimPolicy: Retain +volumeBindingMode: WaitForFirstConsumer + +--- + +# 创建 PersistentVolume +## master +apiVersion: v1 +kind: PersistentVolume +metadata: + name: hty1024-db-mariadb-pv-master + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: persistentVolume + app.k8s.hty1024.com/role: master +spec: + capacity: + storage: 100Gi + volumeMode: Filesystem + accessModes: + - ReadWriteOnce + persistentVolumeReclaimPolicy: Retain + storageClassName: storage-local-mariadb + local: + path: /app/mariadb/data/master + nodeAffinity: + required: + nodeSelectorTerms: + - matchExpressions: + - key: node.k8s.hty1024.com/type + operator: In + values: + - app + +--- + +## slave +apiVersion: v1 +kind: PersistentVolume +metadata: + name: hty1024-db-mariadb-pv-slave + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: persistentVolume + app.k8s.hty1024.com/role: slave +spec: + capacity: + storage: 100Gi + volumeMode: Filesystem + accessModes: + - ReadWriteOnce + persistentVolumeReclaimPolicy: Retain + storageClassName: storage-local-mariadb + local: + path: /app/mariadb/data/slave + nodeAffinity: + required: + nodeSelectorTerms: + - matchExpressions: + - key: node.k8s.hty1024.com/type + operator: In + values: + - app + +--- + +# 创建 PersistentVolumeClaim +## master +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: hty1024-db-mariadb-pvc-master + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: persistentVolumeClaim + app.k8s.hty1024.com/role: master +spec: + resources: + requests: + storage: 100Gi + volumeMode: Filesystem + accessModes: + - ReadWriteOnce + storageClassName: storage-local-mariadb + +--- + +## slave +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: hty1024-db-mariadb-pvc-slave + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: persistentVolumeClaim + app.k8s.hty1024.com/role: slave +spec: + resources: + requests: + storage: 100Gi + volumeMode: Filesystem + accessModes: + - ReadWriteOnce + storageClassName: storage-local-mariadb + +--- + +# 创建 ConfigMap +## master +### my.cnf +apiVersion: v1 +kind: ConfigMap +metadata: + name: hty1024-db-mariadb-cm-master + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: configMap + app.k8s.hty1024.com/role: master +data: + my.cnf: | + [mysqld] + max_connections = 1000 + character-set-server = utf8mb4 + collation-server = utf8mb4_unicode_ci + + server-id = 1 + report_host = master + + log_bin = master-bin + log_error = master-bin.err + binlog_format = mixed + binlog_expire_logs_seconds = 604800 + max_binlog_size = 1G + + binlog-ignore-db = mysql + binlog-ignore-db = sys + binlog-ignore-db = information_schema + binlog-ignore-db = performance_schema + +--- + +### init.sql +apiVersion: v1 +kind: ConfigMap +metadata: + name: hty1024-db-mariadb-cm-master-init + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: configMap + app.k8s.hty1024.com/role: master +data: + init.sql: | + create user 'replication'@'%' identified by '123456'; + grant replication slave, replication client on *.* to 'replication'@'%'; + show grants for 'replication'@'%'; + flush privileges; + +--- + +## slave +### my.cnf +apiVersion: v1 +kind: ConfigMap +metadata: + name: hty1024-db-mariadb-cm-slave + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: configMap + app.k8s.hty1024.com/role: slave +data: + my.cnf: | + [mysqld] + max_connections = 1000 + character-set-server = utf8mb4 + collation-server = utf8mb4_unicode_ci + + server-id = 2 + report_host = slave + + replicate_ignore_db = mysql + replicate_ignore_db = sys + replicate_ignore_db = information_schema + replicate_ignore_db = performance_schema + +--- + +### init.sql +apiVersion: v1 +kind: ConfigMap +metadata: + name: hty1024-db-mariadb-cm-slave-init + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: configMap + app.k8s.hty1024.com/role: slave +data: + init.sql: | + change master to master_host='hty1024-db-mariadb-service-master.hty1024-db', master_port=3306, master_user='replication', master_password='123456', master_log_file='master-bin.000002', master_log_pos=343; + set global sql_slave_skip_counter=1; + start slave; + +--- + +# 创建 Secret(自定义 MySQL 用户密码) +apiVersion: v1 +kind: Secret +metadata: + name: hty1024-db-mariadb-secret + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: secret +type: Opaque +stringData: + root.key: | + 12345678 + replication.key: | + 123456 +immutable: true + +--- + +# 创建 Service +## master +apiVersion: v1 +kind: Service +metadata: + name: hty1024-db-mariadb-service-master + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: service + app.k8s.hty1024.com/role: master +spec: + type: ClusterIP + ports: + - port: 3306 + targetPort: 3306 + selector: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + app.k8s.hty1024.com/role: master + +--- + +## slave +apiVersion: v1 +kind: Service +metadata: + name: hty1024-db-mariadb-service-slave + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: service + app.k8s.hty1024.com/role: slave +spec: + type: ClusterIP + ports: + - port: 3306 + targetPort: 3306 + selector: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + app.k8s.hty1024.com/role: slave + +--- + +# 创建 StatefulSet +## master +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: hty1024-db-mariadb-statefulset-master + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: statefulSet + app.k8s.hty1024.com/role: master +spec: + replicas: 1 + minReadySeconds: 30 + selector: + matchLabels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + app.k8s.hty1024.com/role: master + template: + metadata: + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + app.k8s.hty1024.com/role: master + spec: + terminationGracePeriodSeconds: 60 + volumes: + - name: localtime + hostPath: + path: /etc/localtime + - name: hty1024-db-mariadb-data-master + persistentVolumeClaim: + claimName: hty1024-db-mariadb-pvc-master + - name: hty1024-db-mariadb-conf-master + configMap: + name: hty1024-db-mariadb-cm-master + items: + - key: my.cnf + path: my.cnf + - name: hty1024-db-mariadb-conf-master-init + configMap: + name: hty1024-db-mariadb-cm-master-init + items: + - key: init.sql + path: init.sql + - name: hty1024-db-mariadb-pwd + secret: + secretName: hty1024-db-mariadb-secret + containers: + - name: mariadb + image: mariadb:10.11.6 + ports: + - name: tcp + containerPort: 3306 + volumeMounts: + - name: localtime + mountPath: /etc/localtime + readOnly: true + - name: hty1024-db-mariadb-data-master + mountPath: /var/lib/mysql + - name: hty1024-db-mariadb-conf-master + mountPath: /etc/mysql/conf.d/ + - name: hty1024-db-mariadb-conf-master-init + mountPath: /docker-entrypoint-initdb.d/ + - name: hty1024-db-mariadb-pwd + mountPath: /tmp/secret-volume + readOnly: true + env: + - name: MYSQL_ROOT_PASSWORD_FILE + value: "/tmp/secret-volume/root.key" + - name: MYSQL_REPLICATION_MODE + value: "master" + - name: MYSQL_REPLICATION_USER + value: "replication" + - name: MYSQL_REPLICATION_PASSWORD_FILE + value: "/tmp/secret-volume/replication.key" + nodeSelector: + node.k8s.hty1024.com/type: app + +--- + +## slave +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: hty1024-db-mariadb-statefulset-slave + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: statefulSet + app.k8s.hty1024.com/role: slave +spec: + replicas: 1 + minReadySeconds: 30 + selector: + matchLabels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + app.k8s.hty1024.com/role: slave + template: + metadata: + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + app.k8s.hty1024.com/role: slave + spec: + terminationGracePeriodSeconds: 60 + volumes: + - name: localtime + hostPath: + path: /etc/localtime + - name: hty1024-db-mariadb-data-slave + persistentVolumeClaim: + claimName: hty1024-db-mariadb-pvc-slave + - name: hty1024-db-mariadb-conf-slave + configMap: + name: hty1024-db-mariadb-cm-slave + items: + - key: my.cnf + path: my.cnf + - name: hty1024-db-mariadb-conf-slave-init + configMap: + name: hty1024-db-mariadb-cm-slave-init + items: + - key: init.sql + path: init.sql + - name: hty1024-db-mariadb-pwd + secret: + secretName: hty1024-db-mariadb-secret + containers: + - name: mariadb + image: mariadb:10.11.6 + ports: + - name: tcp + containerPort: 3306 + volumeMounts: + - name: localtime + mountPath: /etc/localtime + readOnly: true + - name: hty1024-db-mariadb-data-slave + mountPath: /var/lib/mysql + - name: hty1024-db-mariadb-conf-slave + mountPath: /etc/mysql/conf.d/ + - name: hty1024-db-mariadb-conf-slave-init + mountPath: /docker-entrypoint-initdb.d/ + - name: hty1024-db-mariadb-pwd + mountPath: /tmp/secret-volume + readOnly: true + env: + - name: MYSQL_ROOT_PASSWORD_FILE + value: "/tmp/secret-volume/root.key" + - name: MYSQL_REPLICATION_MODE + value: "slave" + nodeSelector: + node.k8s.hty1024.com/type: app diff --git a/examples/mariadb/mariadb-10-single.yaml b/examples/mariadb/mariadb-10-single.yaml new file mode 100644 index 0000000..e9f47fb --- /dev/null +++ b/examples/mariadb/mariadb-10-single.yaml @@ -0,0 +1,220 @@ +--- + +# 创建 Namespace +apiVersion: v1 +kind: Namespace +metadata: + name: hty1024-db + +--- + +# 创建 StorageClass +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: storage-local-mariadb + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: storageClass +provisioner: kubernetes.io/no-provisioner +reclaimPolicy: Retain +volumeBindingMode: WaitForFirstConsumer + +--- + +# 创建 PersistentVolume +apiVersion: v1 +kind: PersistentVolume +metadata: + name: hty1024-db-mariadb-pv + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: persistentVolume +spec: + capacity: + storage: 100Gi + volumeMode: Filesystem + accessModes: + - ReadWriteOnce + persistentVolumeReclaimPolicy: Retain + storageClassName: storage-local-mariadb + local: + path: /app/mariadb/data + nodeAffinity: + required: + nodeSelectorTerms: + - matchExpressions: + - key: node.k8s.hty1024.com/type + operator: In + values: + - app + +--- + +# 创建 PersistentVolumeClaim +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: hty1024-db-mariadb-pvc + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: persistentVolumeClaim +spec: + resources: + requests: + storage: 100Gi + volumeMode: Filesystem + accessModes: + - ReadWriteOnce + storageClassName: storage-local-mariadb + +--- + +# 创建 ConfigMap(自定义 my.cnf 文件) +apiVersion: v1 +kind: ConfigMap +metadata: + name: hty1024-db-mariadb-cm + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: configMap +data: + my.cnf: | + [mysqld] + max_connections = 1000 + character-set-server = utf8mb4 + collation-server = utf8mb4_unicode_ci + +--- + +# 创建 Secret(自定义 MySQL 用户密码) +apiVersion: v1 +kind: Secret +metadata: + name: hty1024-db-mariadb-secret + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: secret +type: Opaque +stringData: + root.key: | + 12345678 +immutable: true + +--- + +# 创建 Service +apiVersion: v1 +kind: Service +metadata: + name: hty1024-db-mariadb-service + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: service +spec: + type: ClusterIP + ports: + - port: 3306 + targetPort: 3306 + selector: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + +--- + +# 创建 StatefulSet +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: hty1024-db-mariadb-statefulset + namespace: hty1024-db + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: statefulSet +spec: + replicas: 1 + minReadySeconds: 30 + selector: + matchLabels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + template: + metadata: + labels: + app.k8s.hty1024.com/env: prod + app.k8s.hty1024.com/type: db + app.k8s.hty1024.com/name: mariadb + app.k8s.hty1024.com/version: 10.11.6 + app.k8s.hty1024.com/resources: pod + spec: + terminationGracePeriodSeconds: 60 + volumes: + - name: localtime + hostPath: + path: /etc/localtime + - name: hty1024-db-mariadb-data + persistentVolumeClaim: + claimName: hty1024-db-mariadb-pvc + - name: hty1024-db-mariadb-conf + configMap: + name: hty1024-db-mariadb-cm + items: + - key: my.cnf + path: my.cnf + - name: hty1024-db-mariadb-pwd + secret: + secretName: hty1024-db-mariadb-secret + containers: + - name: mariadb + image: mariadb:10.11.6 + ports: + - name: tcp + containerPort: 3306 + volumeMounts: + - name: localtime + mountPath: /etc/localtime + readOnly: true + - name: hty1024-db-mariadb-data + mountPath: /var/lib/mysql + - name: hty1024-db-mariadb-conf + mountPath: /etc/mysql/conf.d/ + - name: hty1024-db-mariadb-pwd + mountPath: /tmp/secret-volume + readOnly: true + env: + - name: MYSQL_ROOT_PASSWORD_FILE + value: "/tmp/secret-volume/root.key" + nodeSelector: + node.k8s.hty1024.com/type: app + diff --git a/examples/mysql/mysql-8-cluster.yaml b/examples/mysql/mysql-8-cluster.yaml index fc4d09d..10e8353 100644 --- a/examples/mysql/mysql-8-cluster.yaml +++ b/examples/mysql/mysql-8-cluster.yaml @@ -40,14 +40,14 @@ metadata: app.k8s.hty1024.com/role: master spec: capacity: - storage: 20Gi + storage: 100Gi volumeMode: Filesystem accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: storage-local-mysql local: - path: /app/mysql/data/matser + path: /app/mysql/data/master nodeAffinity: required: nodeSelectorTerms: @@ -73,7 +73,7 @@ metadata: app.k8s.hty1024.com/role: slave spec: capacity: - storage: 20Gi + storage: 100Gi volumeMode: Filesystem accessModes: - ReadWriteOnce @@ -109,7 +109,7 @@ metadata: spec: resources: requests: - storage: 20Gi + storage: 100Gi volumeMode: Filesystem accessModes: - ReadWriteOnce @@ -133,7 +133,7 @@ metadata: spec: resources: requests: - storage: 20Gi + storage: 100Gi volumeMode: Filesystem accessModes: - ReadWriteOnce diff --git a/examples/mysql/mysql-8-single.yaml b/examples/mysql/mysql-8-single.yaml index 3580d0f..e047db7 100644 --- a/examples/mysql/mysql-8-single.yaml +++ b/examples/mysql/mysql-8-single.yaml @@ -38,7 +38,7 @@ metadata: app.k8s.hty1024.com/resources: persistentVolume spec: capacity: - storage: 20Gi + storage: 100Gi volumeMode: Filesystem accessModes: - ReadWriteOnce @@ -72,7 +72,7 @@ metadata: spec: resources: requests: - storage: 20Gi + storage: 100Gi volumeMode: Filesystem accessModes: - ReadWriteOnce @@ -116,7 +116,7 @@ metadata: type: Opaque stringData: root.key: | - 123456 + 12345678 immutable: true ---