1. mysql-configmap.yaml 相关配置

apiVersion: v1
kind: ConfigMap
metadata:
  name: model-db-config
  namespace: mysql
  labels:
    app: model-db
data:
  my.cnf: |-
    [client]
    default-character-set=utf8mb4
    [mysql]
    default-character-set=utf8mb4
    [mysqld] 
    character-set-server = utf8mb4  
    collation-server = utf8mb4_unicode_ci  
    init_connect='SET NAMES utf8mb4'  
    skip-character-set-client-handshake = true  
    max_connections=2000
    secure_file_priv=/var/lib/mysql
    bind-address=0.0.0.0
    symbolic-links=0
    #8.0注释掉
    #sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

2. mysql-pv.yaml 定义存储(相当于磁盘)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: model-db-pv
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 5Gi  
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  hostPath:
    path: /data/k8s/mysql8
  volumeMode: Filesystem

3. mysql-pvc.yaml 定义持久化(相当于对磁盘格式化分区)

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: model-db-pv-claim
  namespace: mysql
  labels:
    app: model-mysql
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: nfs
  resources:
    requests:
      storage: 5Gi

4. mysql.yaml(定义Mysql服务)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: model-db
  namespace: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: model-mysql
  template:
    metadata:
      labels:
        app: model-mysql
    spec:
      containers:
      - args:
        - --datadir
        - /var/lib/mysql/datadir
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: root
          - name: MYSQL_USER
            value: user
          - name: MYSQL_PASSWORD
            value: user
        image: mysql:8.0
        name: model-db-container
        ports:
        - containerPort: 3306
          name: dbapi
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: model-db-storage
        - name: config
          mountPath: /etc/mysql/conf.d/my.cnf
          subPath: my.cnf
      volumes:
      - name: model-db-storage
        persistentVolumeClaim:
          claimName: model-db-pv-claim
      - name: config      
        configMap:
          name: model-db-config
      - name: localtime
        hostPath:
          type: File
          path: /etc/localtime

5. mysql-svc.yaml(暴露Mysql)

apiVersion: v1
kind: Service
metadata:
  labels:
    app: model-mysql
  name: model-db-svc
  namespace: mysql
spec:
  type: NodePort
  ports:
  - name: http
    port: 3306
    nodePort: 30336
    protocol: TCP
    targetPort: 3306
  selector:
    app: model-mysql

6.start.sh

kubectl delete -f mysql-svc.yaml
kubectl delete -f mysql.yaml
kubectl delete -f mysql-configmap.yaml
kubectl delete -f mysql-pvc.yaml
kubectl delete -f mysql-pv.yaml

kubectl apply -f mysql-pv.yaml
kubectl apply -f mysql-pvc.yaml
kubectl apply -f mysql-configmap.yaml
kubectl apply -f mysql.yaml
kubectl apply -f mysql-svc.yaml

最终暴露地址为 minikube的子网ip:30336

可minikube service list 查看

7. 注意事项

1. MySQL 8.0 Public Key Retrieval is not allowed 错误的解决方法

useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true

2. minikube的hostpath的根目录为/var/lib/docker/volumes/minikube/_data 默认情况下

即hostpath  = /data/k8s/mysql 实际路径为 /var/lib/docker/volumes/minikube/_data/data/k8s/mysql
3. minikube service list 实际可访问地址, kubectl get svc -A 为容器的集群的Ip