kubernetes-yaml/examples/mariadb/mariadb-10-cluster.yaml

509 lines
13 KiB
YAML
Raw Normal View History

---
# 创建 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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
image: mariadb:11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
app.k8s.hty1024.com/version: 11.5.2
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
2024-10-11 01:03:00 +08:00
image: mariadb:11.5.2
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