您可以使用起始容器預處理一個配置模板從配置圖。預處理步驟可以將局部變量注入配置文件。擴展的配置被寫入init容器和主應用程序容器之間共享的emptyDir。這是一個如何做到這一點的例子。
首先,使用佔位符爲您要擴展的任何字段創建配置圖。我用sed
和和臨時名稱來代替。你也可以看中並使用jinja2或任何你喜歡的東西。只要將你想要的任何預處理器放入init容器映像即可。你可以使用任何你想要的配置文件的文件格式。我只是在這裏使用TOML來表明它不一定是YAML。我把它叫做「.tpl」,因爲它是不準備使用:它有一個字符串,_HOSTNAME_
,需要進行擴展。
$ cat config.toml.tpl
[blah]
blah=_HOSTNAME_
otherkey=othervalue
$ kubectl create configmap cm --from-file=config.toml.tpl
configmap "cm" created
現在寫與安裝在卷的配置圖,並將其擴展並寫入到另一個卷的起始容器吊艙,與主容器共享:
$ cat personalized-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod-5
labels:
app: myapp
annotations:
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The app is running and my config-map is && cat /etc/config/config.toml && sleep 3600']
volumeMounts:
- name: config-volume
mountPath: /etc/config
initContainers:
- name: expander
image: busybox
command: ['sh', '-c', 'cat /etc/config-templates/config.toml.tpl | sed "s/_HOSTNAME_/$MY_NODE_NAME/" > /etc/config/config.toml']
volumeMounts:
- name: config-tpl-volume
mountPath: /etc/config-templates
- name: config-volume
mountPath: /etc/config
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
volumes:
- name: config-tpl-volume
configMap:
name: cm
- name: config-volume
emptyDir:
$ kubctl create -f personalized-pod.yaml
$ sleep 10
$ kubectl logs myapp-pod
The app is running and my config-map is
[blah]
blah=gke-k0-default-pool-93916cec-p1p6
otherkey=othervalue
我在此一裸露的莢作爲例子。您可以將這種類型的窗格嵌入到DaemonSet的窗格模板中。
在這裏,Downward API is used to set the MY_NODE_NAME Environment Variable,因爲節點名稱不容易從容器內獲得。
請注意,由於某些原因,您無法將spec.nodeName
轉換爲文件,只是一個env var。
如果您只需要Env Var中的主機名,那麼您可以跳過init容器。
由於Init Container只運行一次,所以您不應該更新configMap並期望它被重新擴展。如果你需要更新,你可以做兩件事情之一: