【发布时间】:2020-09-24 13:13:41
【问题描述】:
我希望 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.。
这是怎么发生的,即使我移除了 pod? mountPath 是容器路径,所以数据应该会消失。
我怎样才能在persistentVolume中给出我的本地路径?
感谢您的宝贵时间!
编辑:忘了告诉我已经看到了:How to create a mysql kubernetes service with a locally mounted data volume?
我搜索了很多,但没有运气
【问题讨论】:
-
我认为github issue 与
initialize specified but the data directory has files in it.问题有关。你能检查一下它是否适用于 mysql:5.6 吗?你能尝试在 mysql:5.7 中使用这个workaround 并检查它是否有效吗? -
@Jakub 似乎是
initialize specified but the data directory has files in it.工作的解决方法。但我遇到的主要问题是我无法定义本地持久卷来保存 mysql 的数据。我总是遇到以下错误:MountVolume.NewMounter initialization failed for volume "mysql-pv-volume" : path "C:\\mysql-volume" does not exist如果我编辑 mysql-pv.yaml 似乎路径没问题:“local: path: 'C:\mysql-volume'” 感谢您的回答,希望您能提供帮助我有这个问题我有:(
标签: mysql docker kubernetes persistent-volumes persistent-volume-claims