1. Windows Docerk Desktop设置File sharing目录

  2. 创建Secret

kubectl create secret generic mysql-password  --from-literal=password='Yourpassword' --namespace=Yournamespace
  1. 创建Persistent Volume 和 PersistentVolumeClaim
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-volume
  namespace: fish119
  labels:
    type: local
spec:
  storageClassName: hostpath
  capacity:
    storage: 256Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /host_mnt/D/docker/mysql #Windows使用hostPath方式挂载本地目录,路径前加/host_mnt,并做相应修改即可挂载
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  namespace: fish119
  # labels:
  #   app: mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 256Mi
  1. 创建Service 和 Deployment
apiVersion: v1
kind: Service  
metadata:
  name: mysql
  namespace: fish119
  labels:
    app: mysql
spec:
  type: LoadBalancer
  selector:
    app: mysql
  ports:
    - name: mysql-tcp-3306-3306
      port: 3306
      protocol: TCP
      targetPort: 3306
status:
  loadBalancer:
    ingress:
      - hostname: localhost
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: fish119
  labels:
    app: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - image: mysql:5.7
          imagePullPolicy: IfNotPresent
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-password
                  key: password
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pv-claim
  1. 完整yaml文件地址:
    github