我有一个application.yml (Spring)文件,它有大约70个字段,希望将这些字段移动到ConfigMap。在设置ConfigMap的过程中,我意识到所有70个字段都是扁平化的,例如: webservice.endpoint.transferfund将所有70个字段转换为扁平化将是一项痛苦的任务,有没有其他选择。
请提个建议。
下面的配置工作正常:
apiVersion: v1
kind: ConfigMap
metadata:
name: configmapname
namespace: default
data:
webservice.endpoint.transferfund: http://www.customer-service.app/api/tf
webservice.endpoint.getbalance: http://www.customer-service.app/api/balance
webservice.endpoint.customerinfo: http://www.customer-service.app/api/customerinfo下面的配置不起作用,尝试将其作为yml格式。
apiVersion: v1
kind: ConfigMap
metadata:
name: configmapname
namespace: default
data:
application.yaml: |-
webservice:
endpoint:
transferfund: http://www.customer-service.app/api/tf
getbalance: http://www.customer-service.app/api/balance
customerinfo: http://www.customer-service.app/api/customerinfo 在src/main/resources/application.yml中,有以下字段可以访问ConfigMap密钥:
webservice:
endpoint:
transferfund: ${webservice.endpoint.transferfund}
getbalance: ${webservice.endpoint.getbalance}
customerinfo: ${webservice.endpoint.customerinfo}更新:
ConfigMap描述:
C:\Users\deskktop>kubectl describe configmap configmapname
Name: configmapname
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
application.yaml:
----
webservice:
endpoint:
transferfund: http://www.customer-service.app/api/tf
getbalance: http://www.customer-service.app/api/balance
customerinfo: http://www.customer-service.app/api/customerinfo
Events: <none>部署脚本:(如上所示,configMapRef名称作为配置映射名称提供)
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: configmap-sample
spec:
replicas: 1
strategy:
type: RollingUpdate
template:
metadata:
labels:
app: configmap-sample
spec:
containers:
- name: configmap-sample
image: <<image>>
ports:
- name: http-api
containerPort: 9000
envFrom:
- configMapRef:
name: configmapname
resources:
limits:
memory: 1Gi
requests:
memory: 768Mi
env:
- name: JVM_OPTS
value: "-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1 -Xms768M" 发布于 2019-07-03 21:56:06
ConfigMap是配置设置的字典。它由字符串的键值对组成。Kubernetes然后将这些值添加到容器中。
在您的情况下,您必须使它们扁平化,因为Kubernetes不会理解它们。
您可以在有关Creating ConfigMap的文档中阅读到:
kubectl create configmap <map-name> <data-source>
其中是要分配给ConfigMap的名称,是从中提取数据的目录、文件或文字值。
数据源对应于ConfigMap中的键值对,其中
您可以使用kubectl describe或kubectl get来检索有关ConfigMap的信息。
编辑
您可以使用定义的密钥从文件创建ConfigMap。
Define the key to use when creating a ConfigMap from a file
语法可能如下所示:
kubectl create configmap my_configmap --from-file=<my-key-name>=<path-to-file>和ConfigMap migh如下所示:
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2019-07-03T18:54:22Z
name: my_configmap
namespace: default
resourceVersion: "530"
selfLink: /api/v1/namespaces/default/configmaps/my_configmap
uid: 05f8da22-d671-11e5-8cd0-68f728db1985
data:
<my-key-name>: |
key=value
key=value
key=value
key=value我还找到了Create Kubernetes ConfigMaps from configuration files。
功能
投影仪可以:
通过提取一些字段并在和
支持从源中提取像objects+arrays这样的复杂字段
发布于 2019-07-03 17:37:15
您需要将ConfigMap挂载为卷。否则,内容将存在于环境变量中。我在这里发布的示例来自https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: special-config
restartPolicy: Never发布于 2020-03-26 23:48:55
您提到过,您在Spring项目的上下文中使用了application.yaml。因此,如果您不关心是否使用.yaml或.property配置文件,您可以只使用属性文件,因为configMap生成支持它们。它与--from-env-file标志一起工作:
kubectl create configmap configmapname --from-env-file application.properties因此,在部署文件中,您可以直接访问密钥:
...
env:
- KEYNAME
valueFrom:
configMapKeyRef:
name: configmapname
key: KeyInPropertiesFilehttps://stackoverflow.com/questions/56853913
复制相似问题