简体   繁体   English

当由 NFS 动态配置器支持时,mysql 容器不会在 kubernetes 上启动

[英]mysql container won't start on kubernetes when backed by NFS Dynamic provisioner

I'm having issues getting the mysql container starting properly.我在正确启动 mysql 容器时遇到问题。 But to sum it up, with the nfs dynamic provisioner the mysql container won't start and throws an error of mkdir: cannot create directory '/var/lib/mysql/': File exists even though the NFS mount is in the container, and appears to be functioning properly.但总而言之,使用 nfs 动态配置器,mysql 容器将无法启动并抛出mkdir: cannot create directory '/var/lib/mysql/': File exists错误mkdir: cannot create directory '/var/lib/mysql/': File exists即使 NFS 挂载在容器中, mkdir: cannot create directory '/var/lib/mysql/': File exists ,并且似乎运行正常。

I installed Dyanamic NFS provisioner installed on my K8 cluster from here https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client .我从这里https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client安装了安装在我的 K8 集群上的 Dyanamic NFS 配置器。 The test claim and test pod they show on the instructions work.他们在说明中显示的测试声明和测试吊舱有效。

Now to run mysql, I took the code snippets from here:现在为了运行 mysql,我从这里获取了代码片段:

https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/ https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/

kubectl apply mysql-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: managed-nfs-storage  <--- THIS MATCHES MY NFS STORAGECLASS
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

kubectl apply -f mysql-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
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:5.6
        name: mysql
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: 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
kubectl get pv,pvc
NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                          STORAGECLASS          REASON   AGE
persistentvolume/mysql-pv-volume                            20Gi       RWO            Retain           Bound    default/mysql-pv-claim         managed-nfs-storage            5m16s

NAMESPACE      NAME                                    STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS          AGE
default        persistentvolumeclaim/mysql-pv-claim    Bound    mysql-pv-volume                            20Gi       RWO            managed-nfs-storage   5m27s

The pv was created automatically by the dynamic provisioner pv 是由动态供应商自动创建的

Get the error...得到错误...

$ kubectl logs mysql-7d7fdd478f-l2m8h
2020-03-05 18:26:21+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.6.47-1debian9 started.
2020-03-05 18:26:21+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-03-05 18:26:21+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.6.47-1debian9 started.
mkdir: cannot create directory '/var/lib/mysql/': File exists

This error stops the container from starting...此错误会阻止容器启动...

I went and deleted the deployment and added command: [ "/bin/sh", "-c", "sleep 100000" ] so the container would start...我去删除了部署并添加了command: [ "/bin/sh", "-c", "sleep 100000" ]这样容器就会启动......

After getting into the container, I checked the NFS mount is properly mounted and is writable...进入容器后,我检查了 NFS 挂载是否正确挂载并且是可写的...

# df -h | grep mysql
nfs1.example.com:/k8/default-mysql-pv-claim-pvc-0808d1bd-69ca-4ff5-825a-b846b1133e3a  1.0T  1.6G 1023G   1% /var/lib/mysql

If I create a "local" pv如果我创建一个“本地”光伏

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

and created the mysql deployment, the mysql pod starts up without issue.并创建了 mysql 部署,mysql pod 启动没有问题。

So at this point, with dynamic provisioning (potentially just on NFS?) the mysql container doesn't work.所以在这一点上,使用动态配置(可能只是在 NFS 上?)mysql 容器不起作用。

Anyone have any suggestions?有人有什么建议吗?

I'm not exactly sure what is the cause of this so here is few options.我不确定这是什么原因,所以这里有几个选择。

First you could try setting securityContext , because volume might be mounted without proper permissions.首先,您可以尝试设置securityContext ,因为可能会在没有适当权限的情况下安装卷。

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo
spec:
  securityContext:
    runAsUser: 1000
    runAsGroup: 3000
    fsGroup: 2000
  volumes:
  - name: sec-ctx-vol
    emptyDir: {}
  containers:
  - name: sec-ctx-demo
    image: busybox
    command: [ "sh", "-c", "sleep 1h" ]
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
    securityContext:
      allowPrivilegeEscalation: false

You can find out the proper group id and user by typing id and gid inside the container.您可以通过在容器内键入idgid来找出正确的组 ID 和用户。 Or just using kubectl exec -it <pod-name> bash .或者只是使用kubectl exec -it <pod-name> bash

Second, try using subPath其次,尝试使用subPath

volumeMounts:
- name: mysql-persistent-storage
  mountPath: "/var/lib/mysql"
  subPath: mysql

If that won't work I would test the NFS on another pod with initContainer that is creating a directory.如果这不起作用,我将使用正在创建目录的initContainer在另一个 pod 上测试 NFS。 And I would redo the whole nfs maybe using this guide .我可能会使用本指南重做整个 nfs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM