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

问题描述

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

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

这是我的Yaml:

storage-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部署/服务,该错误仍然不断出现。 我什至使用docker命令删除了这些卷,并将我的MysqL自定义映像更改为“ MysqL:5.7”,以防万一,但是出现了相同的initialize specified but the data directory has files in it.

即使我卸下了豆荚,怎么办? 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上有答案

我在Kubernetes和MySQL 5.7.15中也遇到了这个问题。将@yosifki的建议添加到我的容器的定义中,一切正常。

以下是我的有效YAML定义的摘录:

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

完全相同的配置适用于MySQL版本5.6。