Docker Desktop、Kubernetes 和 MySql:无法在 /var/lib/mysql/ 上挂载卷
P粉627427202
P粉627427202 2023-09-01 21:06:53
[MySQL讨论组]

我需要将 /var/lib/mysql 目录挂载为持久卷,以便每次电脑重新启动时数据库的内容都不会丢失(我使用 Docker 桌面和 Windows)。

但是启动Pod时,出现以下错误:

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

这是部署文件:

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

如果我对卷使用另一个装载路径,pod 会启动,但当然,每次重新启动计算机时数据库内容都会丢失。

我还尝试通过在安装卷之前执行 chown 命令来更改目录的所有者:

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

但是启动 pod 时出现以下错误: chown:无法读取目录“/var/lib/mysql”:不允许操作

感谢您的帮助。

P粉627427202
P粉627427202

全部回复(1)
P粉103739566

添加

securityContext:
    runAsUser: 999
    allowPrivilegeEscalation: false

在 StatefulSet 组件上

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号