1、创建ConfigMap:
创建一个名为mysql-config的ConfigMap,用于存储MySQL配置文件
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
skip-host-cache
skip-name-resolve
log-bin
#default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default_time_zone='+08:00'
max_connections =1000
lowercase-table-names=1
2、创建PersistentVolume (PV) 和 PersistentVolumeClaim (PVC):
创建PV和PVC来提供持久化存储。
mysql-pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
3、 创建MySQL Pod:
使用ConfigMap和PVC来创建MySQL Pod。
mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
env:
- name: MYSQL_ROOT_PASSWORD
value: "handhand123"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-config-volume
mountPath: /etc/mysql/conf.d
- name: mysql-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-config-volume
configMap:
name: mysql-config
items:
- key: my.cnf
path: my.cnf
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc
kubectl apply -f mysql-config.yaml
kubectl apply -f mysql-pvc.yaml
kubectl apply -f mysql.yaml
秘钥也可以用秘钥替换(上面配置是直接写死的) kubectl create secret generic mysql-secret --from-literal=password=你的密码