如何使用本地持久卷创建 mysql 服务以将数据存储在 windows 本地机器上

How to create mysql service using local persistent volume to store the data on windows local machine

我希望 mysql pod 在我重新启动计算机时不会删除所有 mysql 数据。

我应该能够将数据存储在我的机器中,所以当我重新启动计算机并且 mysql pod 再次启动时,数据库仍然存在。

这是我的 yaml:

存储-class.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

mysql-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  volumeMode: Filesystem
  storageClassName: local-storage
  local:
    path: "C:\mysql-volume" #2 \ for escape characters right?
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop
  #hostPath:
   # path: /mysql-volume
    #type: DirectoryOrCreate
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    type: local
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi

mysql-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
  - protocol: TCP
    port: 3306
    nodePort: 30001
  selector:        
    app: mysql
  type: NodePort
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql-custom-img-here
        imagePullPolicy: IfNotPresent
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-root-password
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-user
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-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

尝试之后,我得到的第一个错误是:

MountVolume.NewMounter initialization failed for volume "mysql-pv-volume" : path "C:\mysql-volume" does not exist

自从我使用 windows,我想这是正确的路径吧?我使用 2 "" 作为转义字符,也许问题出在路径中,但不确定。如果是,我如何在我的 windows 机器上给出我的本地路径?

然后我将 local: path: 更改为 /opt 并出现以下错误:

initialize specified but the data directory has files in it.

日志:

2020-09-24 12:53:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.31-1debian10 started.
2020-09-24 12:53:00+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-09-24 12:53:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.31-1debian10 started.
2020-09-24 12:53:00+00:00 [Note] [Entrypoint]: Initializing database files
2020-09-24T12:53:00.271130Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-09-24T12:53:00.271954Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2020-09-24T12:53:00.271981Z 0 [ERROR] Aborting

但是如果我将 mountPath: /var/lib/mysql 更改为例如 mountPath: /var/lib/mysql-test

能用,但不如预期(重启电脑后保存数据)。

即使删除了 PV、PVC 和 MYSQL deployment/service,同样的错误仍然出现。 我什至使用 docker 命令删除了卷,并将我的 mysql 自定义图像更改为 'mysql:5.7' 以防万一,但出现了相同的 initialize specified but the data directory has files in it.

即使我移除了 pod,那是怎么发生的? mountPath是容器路径,所以数据应该消失了。

我如何在 persistentVolume 中提供我的本地路径?

感谢您的宝贵时间!

编辑:忘记告诉我我已经看到了这个:How to create a mysql kubernetes service with a locally mounted data volume?

我搜索了很多,但没有成功

我终于解决了问题...

@Jakub

回答了initialize specified but the data directory has files in it.的问题

MountVolume.NewMounter initialization failed for volume "mysql-pv-volume" : path "C:\mysql-volume" does not exist ....我什至不敢相信因为这个愚蠢的问题而花费的时间...

正确的路径是:path: /c/mysql-volume之后,一切如期进行!

我将此作为社区 Wiki 答案发布,以提高知名度。


如果您对 initialize specified but the data directory has files in it 有疑问,github issue 可以帮助您。


TLDR

  • 在您的容器中使用 --ignore-db-dir=lost+found
  • 使用旧版本 mysql,例如 mysql:5.6

@alexpls 和@aklinkert

提供了关于 github 的答案

I had this issue with Kubernetes and MySQL 5.7.15 as well. Adding the suggestion from @yosifki to my container's definition got things working.

Here's an extract of my working YAML definition:

name: mysql-master
image: mysql:5.7
args:
  - "--ignore-db-dir=lost+found"

The exact same configuration is working for MySQL Version 5.6 with.