Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Kubernetes take JSON format as input file to create configmap and secret?

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 ?

like image 314
user1684651 Avatar asked May 07 '20 08:05

user1684651


People also ask

Does Kubernetes use JSON?

Kubectl uses JSONPath expressions to filter on specific fields in the JSON object and format the output.

How do I create a config map from a file?

You can use kubectl create configmap to create a ConfigMap from multiple files in the same directory. When you are creating a ConfigMap based on a directory, kubectl identifies files whose basename is a valid key in the directory and packages each of those files into the new ConfigMap.

How does ConfigMap work in Kubernetes?

A ConfigMap is an API object that lets you store configuration for other objects to use. Unlike most Kubernetes objects that have a spec , a ConfigMap has data and binaryData fields. These fields accept key-value pairs as their values. Both the data field and the binaryData are optional.


1 Answers

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

like image 104
hoque Avatar answered Sep 20 '22 15:09

hoque