gpt4 book ai didi

kubernetes - 如何在 kubernetes 的环境变量中设置 mountPath 的值?

转载 作者:行者123 更新时间:2023-12-02 04:19:50 30 4
gpt4 key购买 nike

我使用 configMap 来存储挂载路径值,但是当我调用我的 pod 时它不起作用,似乎无法将 mountPath 属性分配给环境变量。

这是我的代码:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
env:
- name: MOUNT_PATH
valueFrom:
configMapKeyRef:
name: my-configMap
key: MOUNT_PATH
volumeMounts:
- name: nginx-volume
mountPath: $(MOUNT_PATH)
volumes:
- name: nginx-volume
persistentVolumeClaim:
claimName: my-pvc

---


apiVersion: v1
kind: ConfigMap
metadata:
name: my-configMap
namespace: default
data:
MOUNT_PATH: "/opt/somepath"

最佳答案

您可以使用 subPath 实现您想要的效果如果您使用的是 k8s 1.17 或更高版本。

    env:
- name: MOUNT_PATH
valueFrom:
configMapKeyRef:
name: myconfigmap
key: MOUNT_PATH
volumeMounts:
- name: nginx-volume
mountPath: /opt
subPathExpr: $(MOUNT_PATH)
volumes:
- name: nginx-volume
persistentVolumeClaim:
claimName: my-pvc

---

apiVersion: v1
kind: ConfigMap
metadata:
name: myconfigmap
namespace: default
data:
MOUNT_PATH: "somepath"

容器内的结果:

# ls -l /opt
total 4
drwxrwxrwx 2 root root 4096 Oct 29 14:58 somepath

关于kubernetes - 如何在 kubernetes 的环境变量中设置 mountPath 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61195918/

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