本文介绍了Kubernetes是否采用JSON格式作为输入文件来创建configmap和密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON格式的现有配置文件,如下所示

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

我知道我可以通过--from-file选项传递键/值对属性文件来进行configmap和秘密创建.

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

但是JSON格式的文件呢? Kubernetes是否将JSON格式的文件作为输入文件来创建configmap和secret?

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

如果运行此命令,则表示已创建configmap/demo-configmap.但是,如何在其他Pod中引用此configmap值?

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

推荐答案

使用--from-file创建configmap/secret时,默认情况下,文件名将是键名,文件内容将是值.

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.

例如,您创建的configmap就像

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

您可以将configmap作为卷挂载到您的Pod中.其中键名将是文件名,值将是文件的内容.喜欢

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

pod运行时,命令ls /etc/config/产生以下输出:

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

test.json

这篇关于Kubernetes是否采用JSON格式作为输入文件来创建configmap和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 10:57