Kubernetes 用一些环境变量替换configmap内容
我正在运行一个statefolset,其中我使用volumeClaimTemplates。那里一切都很好 我还有一个configmap,在这里我想用这个配置文件投影到的每个pod的pod名称替换一些条目;例如,如果configmap数据为:Kubernetes 用一些环境变量替换configmap内容,kubernetes,configmap,kubernetes-statefulset,Kubernetes,Configmap,Kubernetes Statefulset,我正在运行一个statefolset,其中我使用volumeClaimTemplates。那里一切都很好 我还有一个configmap,在这里我想用这个配置文件投影到的每个pod的pod名称替换一些条目;例如,如果configmap数据为: ThisHost=<hostname -s> OtherConfig1=1 OtherConfig1=2 ... 此主机= OtherConfig1=1 OtherConfig1=2 ... 然后,对于名为mypod-0的状态集pod,配置文
ThisHost=<hostname -s>
OtherConfig1=1
OtherConfig1=2
...
此主机=
OtherConfig1=1
OtherConfig1=2
...
然后,对于名为mypod-0
的状态集pod,配置文件应包含thishhost=mypod-0
和thishhost=mypod-1
的mypod-1
我该怎么做呢?主机名包含在pod内的环境变量中,默认情况下称为
HOSTNAME
。
如果您首先:
- 装载configmap并将其设置为
(这将在pod的文件系统中创建一个包含该文本的文件)thishhost=hostname-s
- 启动时向pod传递替换命令(类似于
)$sed's/hostname/$hostname/g'-i/path/to/configmapfile
apiVersion: v1
kind: Pod
metadata:
name: command-demo
labels:
purpose: demonstrate-command
spec:
containers:
- name: command-demo-container
image: debian
command: ["sed"]
args: ["'s/hostname/$HOSTNAME'", "-i", "/path/to/config/map/mount/point"]
restartPolicy: OnFailure
args的语法可能需要一些调整,但您已经明白了
如果有帮助,请告诉我 主机名包含在pod内的环境变量中,默认情况下称为
HOSTNAME
。
如果您首先:
- 装载configmap并将其设置为
(这将在pod的文件系统中创建一个包含该文本的文件)thishhost=hostname-s
- 启动时向pod传递替换命令(类似于
)$sed's/hostname/$hostname/g'-i/path/to/configmapfile
apiVersion: v1
kind: Pod
metadata:
name: command-demo
labels:
purpose: demonstrate-command
spec:
containers:
- name: command-demo-container
image: debian
command: ["sed"]
args: ["'s/hostname/$HOSTNAME'", "-i", "/path/to/config/map/mount/point"]
restartPolicy: OnFailure
args的语法可能需要一些调整,但您已经明白了
如果有帮助,请告诉我