17

I have an existing configuration file in JSON format, something like below

{
    "maxThreadCount": 10,
    "trackerConfigs": [{
            "url": "https://example1.com/",
            "username": "username",
            "password": "password",
            "defaultLimit": 1
        },
        {
            "url": "https://example2.com/",
            "username": "username",
            "password": "password",
            "defaultLimit": 1
        }
    ],
    "repoConfigs": [{
        "url": "https://github.com/",
        "username": "username",
        "password": "password",
        "type": "GITHUB"
    }],
    "streamConfigs": [{
        "url": "https://example.com/master.json",
        "type": "JSON"
    }]
}

I understand that I am allowed to pass key/value pair properties file with --from-file option for configmap and secret creation.

But How about JSON formatted file ? Does Kubernetes take JSON format file as input file to create configmap and secret as well?

$ kubectl create configmap demo-configmap --from-file=example.json

If I run this command, it said configmap/demo-configmap created. But how can I refer this configmap values in other pod ?

user1684651
  • 390
  • 1
  • 8
  • 21

4 Answers4

39

When you create configmap/secret using --from-file, by default the file name will be the key name and content of the file will be the value.

For example, You created configmap will be like

apiVersion: v1
data:
  test.json: |
    {
        "maxThreadCount": 10,
        "trackerConfigs": [{
                "url": "https://example1.com/",
                "username": "username",
                "password": "password",
                "defaultLimit": 1
            },
            {
                "url": "https://example2.com/",
                "username": "username",
                "password": "password",
                "defaultLimit": 1
            }
        ],
        "repoConfigs": [{
            "url": "https://github.com/",
            "username": "username",
            "password": "password",
            "type": "GITHUB"
        }],
        "streamConfigs": [{
            "url": "https://example.com/master.json",
            "type": "JSON"
        }]
    }
kind: ConfigMap
metadata:
  creationTimestamp: "2020-05-07T09:03:55Z"
  name: demo-configmap
  namespace: default
  resourceVersion: "5283"
  selfLink: /api/v1/namespaces/default/configmaps/demo-configmap
  uid: ce566b36-c141-426e-be30-eb843ab20db6

You can mount the configmap into your pod as volume. where the key name will be the file name and value will be the content of the file. like following

apiVersion: v1
kind: Pod
metadata:
  name: 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:
        name: demo-configmap
  restartPolicy: Never

When the pod runs, the command ls /etc/config/ produces the output below:

test.json

hoque
  • 5,735
  • 1
  • 19
  • 29
  • Thanks for the help, since I have mounted the file at location /etc/config/test.json. I'd like to read the content of this file from my java app. I can define an env key/value pair, the value is /etc/config/test.json, and call System.getenv().get("key"); to read it. but I am wondering if there is a way that I can define and call it with System.getProperty() ? so I don't have to change from System.getenv().get() to System.getProperty() in my java app code. – user1684651 May 08 '20 at 07:23
  • you can pass the configmap as env variable. like this https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables – hoque May 08 '20 at 08:20
  • 1
    Yes, but it's still an Environment variable, I have to change my java app code to System.getenv().get() to have it.I'd like to set a Java system property, so I can just call System.getProperty() without updating my app code – user1684651 May 08 '20 at 09:12
2

Config maps are a container for key value pairs. So, if you create a ConfigMap from a file containing JSON, this will be stored with the file name as key and the JSON as value.

To access such a Config Map from a Pod, you would have to mount it into your Pod as a volume:

How to mount config maps

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
1

Unfortunately the solution as stated from hoque did not work for me. In may case that app terminated with a very suspect message:

Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
  * You intended to execute a .NET program:
      The application 'myapp.dll' does not exist.
  * You intended to execute a .NET SDK command:
      It was not possible to find any installed .NET SDKs.
      Install a .NET SDK from:
        https://aka.ms/dotnet-download

I could see that appsettings.json was deployed but something has gone wrong here. In the end, this solution has worked for me (similar, but with some extras):

spec:
  containers:
    - name: webapp
      image: <my image>
      volumeMounts:
      - name: appconfig
        # "mountPath: /app" only doesn't work (app crashes)
        mountPath: /app/appsettings.json
        subPath: appsettings.json
  volumes:
    - name: appconfig
      configMap:
        name: my-config-map
        # Required since "mountPath: /app" only doesn't work (app crashes)
        items:
        - key: appsettings.json
          path: appsettings.json

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config-map
  labels:
    app: swpq-task-02-team5
data:
  appsettings.json: |
    {
     ...
    }
Willy K.
  • 397
  • 2
  • 14
0

I have had this issue for a couple of days as I wanted to refer a json config file (config.production.json) from my local directory into a specific location inside the containers for pod (/var/lib/ghost). The below config worked for me. Please note the mountPath and subPath keys that did the trick for me. The snippet below is of a pod kind=deployment shortened for ease of reading ---

 spec:
      volumes:
        - name: configmap-volume
          configMap:
            name: configmap
      containers:
        - env:
            - name: url
              value: https://www.example.com
          volumeMounts:
            - name: configmap-volume
              mountPath: /var/lib/ghost/config.production.json
              subPath: config.production.json
Stefan
  • 1,697
  • 15
  • 31
wiqram
  • 11
  • 1