Kubernetes 是否以 JSON 格式作为输入文件来创建 configmap 和 secret?

use*_*651 5 kubernetes kubernetes-pod kubernetes-secrets configmap

我有一个 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"
    }]
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用 --from-file 选项传递键/值对属性文件,用于配置映射和秘密创建。

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

$ kubectl create configmap demo-configmap --from-file=example.json
Run Code Online (Sandbox Code Playgroud)

如果我运行此命令,它会显示已创建 configmap/demo-configmap。但是如何在其他 pod 中引用此 configmap 值?

hoq*_*que 13

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

例如,您创建的 configmap 将类似于

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

test.json