509 lines
13 KiB
YAML
509 lines
13 KiB
YAML
---
|
||
|
||
# 创建 Namespace
|
||
apiVersion: v1
|
||
kind: Namespace
|
||
metadata:
|
||
name: hty1024-db
|
||
|
||
---
|
||
|
||
# 创建 StorageClass
|
||
apiVersion: storage.k8s.io/v1
|
||
kind: StorageClass
|
||
metadata:
|
||
name: storage-local-mysql
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql-pv-master
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql
|
||
local:
|
||
path: /app/mysql/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-mysql-pv-slave
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql
|
||
local:
|
||
path: /app/mysql/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-mysql-pvc-master
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
app.k8s.hty1024.com/resources: persistentVolumeClaim
|
||
app.k8s.hty1024.com/role: master
|
||
spec:
|
||
resources:
|
||
requests:
|
||
storage: 100Gi
|
||
volumeMode: Filesystem
|
||
accessModes:
|
||
- ReadWriteOnce
|
||
storageClassName: storage-local-mysql
|
||
|
||
---
|
||
|
||
## slave
|
||
apiVersion: v1
|
||
kind: PersistentVolumeClaim
|
||
metadata:
|
||
name: hty1024-db-mysql-pvc-slave
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
app.k8s.hty1024.com/resources: persistentVolumeClaim
|
||
app.k8s.hty1024.com/role: slave
|
||
spec:
|
||
resources:
|
||
requests:
|
||
storage: 100Gi
|
||
volumeMode: Filesystem
|
||
accessModes:
|
||
- ReadWriteOnce
|
||
storageClassName: storage-local-mysql
|
||
|
||
---
|
||
|
||
# 创建 ConfigMap
|
||
## master
|
||
### my.cnf
|
||
apiVersion: v1
|
||
kind: ConfigMap
|
||
metadata:
|
||
name: hty1024-db-mysql-cm-master
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql-cm-master-init
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql-cm-slave
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql-cm-slave-init
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
app.k8s.hty1024.com/resources: configMap
|
||
app.k8s.hty1024.com/role: slave
|
||
data:
|
||
init.sql: |
|
||
change master to master_host='hty1024-db-mysql-service-master.hty1024-db', master_port=3306, master_user='replication', master_password='123456', master_log_file='master-bin.000003', master_log_pos=157, get_master_public_key=1;
|
||
set global sql_slave_skip_counter=1;
|
||
start slave;
|
||
|
||
---
|
||
|
||
# 创建 Secret(自定义 MySQL 用户密码)
|
||
apiVersion: v1
|
||
kind: Secret
|
||
metadata:
|
||
name: hty1024-db-mysql-secret
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql-service-master
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
app.k8s.hty1024.com/resources: pod
|
||
app.k8s.hty1024.com/role: master
|
||
|
||
---
|
||
|
||
## slave
|
||
apiVersion: v1
|
||
kind: Service
|
||
metadata:
|
||
name: hty1024-db-mysql-service-slave
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
app.k8s.hty1024.com/resources: pod
|
||
app.k8s.hty1024.com/role: slave
|
||
|
||
---
|
||
|
||
# 创建 StatefulSet
|
||
## master
|
||
apiVersion: apps/v1
|
||
kind: StatefulSet
|
||
metadata:
|
||
name: hty1024-db-mysql-statefulset-master
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql-data-master
|
||
persistentVolumeClaim:
|
||
claimName: hty1024-db-mysql-pvc-master
|
||
- name: hty1024-db-mysql-conf-master
|
||
configMap:
|
||
name: hty1024-db-mysql-cm-master
|
||
items:
|
||
- key: my.cnf
|
||
path: my.cnf
|
||
- name: hty1024-db-mysql-conf-master-init
|
||
configMap:
|
||
name: hty1024-db-mysql-cm-master-init
|
||
items:
|
||
- key: init.sql
|
||
path: init.sql
|
||
- name: hty1024-db-mysql-pwd
|
||
secret:
|
||
secretName: hty1024-db-mysql-secret
|
||
containers:
|
||
- name: mysql
|
||
image: mysql:8.0.39
|
||
ports:
|
||
- name: tcp
|
||
containerPort: 3306
|
||
volumeMounts:
|
||
- name: localtime
|
||
mountPath: /etc/localtime
|
||
readOnly: true
|
||
- name: hty1024-db-mysql-data-master
|
||
mountPath: /var/lib/mysql
|
||
- name: hty1024-db-mysql-conf-master
|
||
mountPath: /etc/mysql/conf.d/
|
||
- name: hty1024-db-mysql-conf-master-init
|
||
mountPath: /docker-entrypoint-initdb.d/
|
||
- name: hty1024-db-mysql-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-mysql-statefulset-slave
|
||
namespace: hty1024-db
|
||
labels:
|
||
app.k8s.hty1024.com/env: prod
|
||
app.k8s.hty1024.com/type: db
|
||
app.k8s.hty1024.com/name: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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: mysql
|
||
app.k8s.hty1024.com/version: 8.0.39
|
||
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-mysql-data-slave
|
||
persistentVolumeClaim:
|
||
claimName: hty1024-db-mysql-pvc-slave
|
||
- name: hty1024-db-mysql-conf-slave
|
||
configMap:
|
||
name: hty1024-db-mysql-cm-slave
|
||
items:
|
||
- key: my.cnf
|
||
path: my.cnf
|
||
- name: hty1024-db-mysql-conf-slave-init
|
||
configMap:
|
||
name: hty1024-db-mysql-cm-slave-init
|
||
items:
|
||
- key: init.sql
|
||
path: init.sql
|
||
- name: hty1024-db-mysql-pwd
|
||
secret:
|
||
secretName: hty1024-db-mysql-secret
|
||
containers:
|
||
- name: mysql
|
||
image: mysql:8.0.39
|
||
ports:
|
||
- name: tcp
|
||
containerPort: 3306
|
||
volumeMounts:
|
||
- name: localtime
|
||
mountPath: /etc/localtime
|
||
readOnly: true
|
||
- name: hty1024-db-mysql-data-slave
|
||
mountPath: /var/lib/mysql
|
||
- name: hty1024-db-mysql-conf-slave
|
||
mountPath: /etc/mysql/conf.d/
|
||
- name: hty1024-db-mysql-conf-slave-init
|
||
mountPath: /docker-entrypoint-initdb.d/
|
||
- name: hty1024-db-mysql-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
|