gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:28:55 31 4
gpt4 key购买 nike

我在使mysql容器正确启动方面遇到问题。但总而言之,即使NFS安装在容器中,并且使用nfs动态预配器,mysql容器也不会启动并抛出mkdir: cannot create directory '/var/lib/mysql/': File exists错误,并且看起来工作正常。

我从https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client在我的K8群集上安装了Dyanamic NFS设置程序。它们在说明上显示的测试声明和测试 Pane 。

现在运行mysql,我从这里获取代码片段:

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

pv由动态预配器自动创建

得到错误...
$ 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

此错误使容器无法启动...

我去删除了部署,并添加了 command: [ "/bin/sh", "-c", "sleep 100000" ],这样容器就会启动...

进入容器后,我检查了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

如果我创建一个“本地” 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"

并创建了mysql部署后,mysql pod可以正常启动。

因此,在这一点上,使用动态配置(可能仅在NFS上进行配置),mysql容器不起作用。

有人有什么建议吗?

最佳答案

我不确定这是什么原因,所以这里有几个选择。

首先,您可以尝试设置 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

您可以通过在容器内键入 idgid来找到正确的组ID和用户。
或者只是使用 kubectl exec -it <pod-name> bash

其次,尝试使用 subPath
volumeMounts:
- name: mysql-persistent-storage
mountPath: "/var/lib/mysql"
subPath: mysql

如果那行不通,我将使用创建目录的 initContainer在另一个Pod上测试NFS。
我可能会使用 this guide重做整个nfs。

关于mysql - 由NFS动态预配器支持时,mysql容器不会在kubernetes上启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60567774/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com