简体   繁体   中英

ConfigMap data (yml format) - Kubernetes

I have an application.yml (Spring) file, which has almost 70 fields, want to move those fields to ConfigMap. In the process of setup ConfigMap, have realized all the 70 fields has be flatened example : webservice.endpoint.transferfund It's gonna be a painful task to convert all the 70 fields as flat, is there any alternative.

Please suggest.

Below Config is working:

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

Below config is not working, tried it as yml format.

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 

in src/main/resources/application.yml have below fields to access ConfigMap keys:

webservice:
  endpoint:
    transferfund: ${webservice.endpoint.transferfund}
    getbalance: ${webservice.endpoint.getbalance}
    customerinfo: ${webservice.endpoint.customerinfo}

Updated:

ConfigMap Description:

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>

Deployment script: (configMapRef name provided as configmap name as shown above)

---
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"   

A ConfigMap is a dictionary of configuration settings. It consists of key-value pairs of strings. Kubernetes then adds those values to your containers.

In your case you have to make them flat, because Kubernetes will not understand them.

You can read in the documentation about Creating ConfigMap that:

kubectl create configmap <map-name> <data-source>

where is the name you want to assign to the ConfigMap and is the directory, file, or literal value to draw the data from.

The data source corresponds to a key-value pair in the ConfigMap, where

  • key = the file name or the key you provided on the command line, and
  • value = the file contents or the literal value you provided on the command line.

You can use kubectl describe or kubectl get to retrieve information about a ConfigMap.

EDIT

You could create a ConfigMap from a file with defined key.

Define the key to use when creating a ConfigMap from a file

Syntax might look like this:

kubectl create configmap my_configmap --from-file=<my-key-name>=<path-to-file> And the ConfigMap migh look like the following:

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

Also I was able to find Create Kubernetes ConfigMaps from configuration files .

Functionality

The projector can:

  • Take raw files and stuff them into a ConfigMap
  • Glob files in your config repo, and stuff ALL of them in your configmap
  • Extract fields from your structured data (yaml/json)
  • Create new structured outputs from a subset of a yaml/json source by pulling out some fields and dropping others
  • Translate back and forth between JSON and YAML (convert a YAML source to a JSON output, etc)
  • Support for extracting complex fields like objects+arrays from sources, and not just scalars!

You need to mount the ConfigMap as Volume. Otherwise the content would live in environment variables. The example i post here is from 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

You mentioned, you're using the application.yaml in context of a Spring project. So if you don't care whether you use .yaml or .property configuration-files, you can just use property-files because configMap generation supports them. It works with the --from-env-file flag:

kubectl create configmap configmapname --from-env-file application.properties

So in your deployment-file you can directly access the keys:

...
env:
  - KEYNAME
    valueFrom:
       configMapKeyRef:
          name: configmapname
          key: KeyInPropertiesFile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM