Docker Desktop, Kubernetes and MySql: Unable to mount volume on /var/lib/mysql/
<p>I need to mount the /var/lib/mysql directory as a persistent volume so that the contents of the database are not lost every time the computer is restarted (I use Docker Desktop and Windows).</p>
<p>但是启动Pod时,出现以下错误:</p>
<pre class="brush:php;toolbar:false;">2023-03-07 21:10:15 00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.32-1.el8 started.
find: '/var/lib/mysql/': Operation not permitted
find: '/var/lib/mysql': Operation not permitted
chown: changing ownership of '/var/lib/mysql/': Operation not permitted
chown: changing ownership of '/var/lib/mysql': Operation not permitted</pre>
<p>这是部署文件:</p>
<pre class="brush:php;toolbar:false;">kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 500M
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
storageClassName: local-storage
local:
path: /c/data/db
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- docker-desktop
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 500M
---
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
selector:
app: mysql
ports:
- port: 3306
targetPort: 3306
---
apiVersion: v1
kind: Service
metadata:
name: mysql-lb
spec:
type: LoadBalancer
selector:
app: mysql
ports:
- port: 3306
targetPort: 3306
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app: mysql
name: mysql
spec:
serviceName: mysql
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:latest
name: mysql
ports:
- containerPort: 3306
resources: {}
volumeMounts:
- name: mysql-volume
mountPath: /var/lib/mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
key: MYSQL_ROOT_PASSWORD
name: mysql-env
- name: MYSQL_ROOT_USERNAME
valueFrom:
configMapKeyRef:
key: MYSQL_ROOT_USERNAME
name: mysql-env
volumes:
- name: mysql-volume
persistentVolumeClaim:
claimName: mysql-pvc</pre>
<p>如果我对卷使用另一个装载路径,pod 会启动,但当然,每次重新启动计算机时数据库内容都会丢失。</p>
<p>I also tried changing the owner of the directory by executing the chown command before mounting the volume: </p>
<pre class="brush:php;toolbar:false;">containers:
- image: mysql:latest
name: mysql
ports:
- containerPort: 3306
resources: {}
command:
- chown
- -R
- 999:999
- /var/lib/mysql
volumeMounts:
- name: mysql-volume
mountPath: /var/lib/mysql</pre>
<p>But when starting the pod I get the following error:
<code>chown: Unable to read directory '/var/lib/mysql': Operation not allowed</code></p>
<p>Thanks for your help. </p>