我已经在配置目录中定义了应用程序属性文件。配置目录位于以下结构中。
config
application.properties
application-test.properties
application-dev.properties
application-prod.properties
我已如下定义configmap
{{ range $path, $bytes := .Files.Glob "config/*" }}
{{ base $path }}: '{{- $.Files.Get $path | nindent 2 | upper | replace "." "_" }}'
{{- end }}
我们正在使用envFrom属性通过运行容器中的环境变量使用ConfigMap。 (这在我的部署yaml文件中)
spec:
containers:
- envFrom:
- configMapRef:
name: nginx-configmap
运行helm install
后,我看到生成了configmap。
kubectl describe cm sample-configmap
Data
====
----
SERVER_PORT = 8080 SERVER_NAME = LOCALHOST SERVER_GREETING = GREETING-SERVICE
----
在configmap中,内容不是作为单个字符串而不是键/值对生成的。
即使在容器内部,值也不存储为键/值对。它们存储为字符串。
kubectl exec -it <pod> sh
/data # env | grep application.properties
application.properties= SERVER_PORT = 8080 SERVER_NAME = LOCALHOST SERVER_GREETING = GREETING-SERVICE SAMPLE_GROUPA_PARAM1 = VALUE1BASE SAMPLE_GROUPA_PARAM2 = VALUE2BASE SAMPLE_HIGHERPARAM = VALUEHIGHERBASE
/data # echo $application.properties
.properties
/data # echo $SERVER_PORT
我也尝试过
data:
{{- (.Files.Glob "config/*").AsConfig | nindent 2 | upper | replace "." "_" }}
观察到相同的问题。
是否可以在容器内部的env中将application.properties的内容公开为键/值对?
答案 0 :(得分:1)
问题是您读取文件和configmap配置的方式。 假设您的文件位于 config 文件夹中,如下所示(请注意,整数应包含在带引号的字符串中):
config.ini
SERVER_PORT = "8080"
SERVER_NAME = LOCALHOST
SERVER_GREETING = GREETING-SERVICE
SAMPLE_GROUPA_PARAM1 = VALUE1BASE
SAMPLE_GROUPA_PARAM2 = VALUE2BASE
SAMPLE_HIGHERPARAM = VALUEHIGHERBASE
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
data:
{{ range $path, $bytes := .Files.Glob "config/*" }}
{{- $.Files.Get $path | indent 2 | upper | replace "." "_" | replace " = " ": " }}
{{- end }}
pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
输出:
$ kubectl logs test-pod
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
SAMPLE_GROUPA_PARAM1=VALUE1BASE
SAMPLE_GROUPA_PARAM2=VALUE2BASE
HOSTNAME=test-pod
...
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
SERVER_GREETING=GREETING-SERVICE
SERVER_PORT=8080
SERVER_NAME=LOCALHOST
请参见Configure all key-value pairs in a ConfigMap as container environment variables