【发布时间】:2018-08-02 07:58:22
【问题描述】:
我想通过 pvc 在 kubernetes 中使用 glusterfs 持久化数据文件,我挂载了目录,它会工作,但是当我尝试挂载文件时,它会失败,因为文件被挂载到目录类型, k8s如何挂载数据文件?
【问题讨论】:
我想通过 pvc 在 kubernetes 中使用 glusterfs 持久化数据文件,我挂载了目录,它会工作,但是当我尝试挂载文件时,它会失败,因为文件被挂载到目录类型, k8s如何挂载数据文件?
【问题讨论】:
如何在 k8s 中挂载数据文件?
这通常是特定于应用程序的,有多种方法可以做到这一点,但主要是您想了解subPath。
一般来说,您可以选择:
conf/interpreter.json 看起来像是一个很好的例子......注意事项:
如果您使用 ConfigMaps,那么您必须使用 subPath 引用单个文件才能挂载它,即使您在 ConfigMap 中只有一个文件。像这样的:
containers:
- volumeMounts:
- name: my-config
mountPath: /my-app/my-config.json
subPath: config.json
volumes:
- name: my-config
configMap:
name: cm-my-config-map-example
ConfigMap 将单个example.sh 脚本文件挂载到容器的/bin 目录的完整示例。您可以调整此示例以满足您将具有任何权限的任何文件放置在任何所需文件夹中的需要。将my-namespace 替换为任何所需的(或完全删除default 之一)
配置图:
kind: ConfigMap
apiVersion: v1
metadata:
namespace: my-namespace
name: cm-example-script
data:
example-script.sh: |
#!/bin/bash
echo "Yaaaay! It's an example!"
部署:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: my-namespace
name: example-deployment
labels:
app: example-app
spec:
selector:
matchLabels:
app: example-app
strategy:
type: Recreate
template:
metadata:
labels:
app: example-app
spec:
containers:
- image: ubuntu:16.04
name: example-app-container
stdin: true
tty: true
volumeMounts:
- mountPath: /bin/example-script.sh
subPath: example-script.sh
name: example-script
volumes:
- name: example-script
configMap:
name: cm-example-script
defaultMode: 0744
test.txt 文件挂载到容器的/bin 目录的完整示例(文件已存在于卷的根目录中)。但是,如果您希望使用持久卷而不是 configMap 进行挂载,这里是另一个以大致相同的方式挂载的示例(test.txt 挂载在 /bin/test.txt 中)...请注意两点:@987654336 @ 必须存在于 PV 上,并且我使用 statefulset 只是为了使用自动配置的 pvc 运行,您可以进行相应调整...
apiVersion: apps/v1
kind: StatefulSet
metadata:
namespace: my-namespace
name: ss-example-file-mount
spec:
serviceName: svc-example-file-mount
replicas: 1
selector:
matchLabels:
app: example-app
template:
metadata:
labels:
app: example-app
spec:
containers:
- image: ubuntu:16.04
name: example-app-container
stdin: true
tty: true
volumeMounts:
- name: persistent-storage-example
mountPath: /bin/test.txt
subPath: test.txt
volumeClaimTemplates:
- metadata:
name: persistent-storage-example
spec:
storageClassName: sc-my-storage-class-for-provisioning-pv
accessModes: [ ReadWriteOnce ]
resources:
requests:
storage: 2Gi
【讨论】: