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

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

我有一个 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"
    }]
}

我知道我可以通过 key/value 对配置映射和秘密创建的 --from-file 选项的属性文件。

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

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

如果我运行这个命令,它说configmap/demo-configmap创建了。但是我怎样才能在其他 pod 中引用这个 configmap 值呢?

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

例如,您创建的 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

您可以将 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

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

test.json

配置映射是键值对的容器。因此,如果您从包含 JSON 的文件创建一个 ConfigMap,它将以文件名作为键存储,并将 JSON 作为值存储。

要从 Pod 访问这样的 Config Map,您必须将它作为一个卷安装到您的 Pod 中:

How to mount config maps

不幸的是,hoque 所述的解决方案对我不起作用。在可能的情况下,该应用程序会以非常可疑的消息终止:

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

我可以看到 appsettings.json 已部署,但这里出了点问题。最后,this solution 对我有用(类似,但有一些额外的):

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: |
    {
     ...
    }

我已经遇到这个问题好几天了,因为我想将本地目录中的 json 配置文件 (config.production.json) 引用到 pod 容器内的特定位置 (/var/lib/ghost).以下配置对我有用。请注意 mountPathsubPath 键对我有用。下面的代码片段是为了便于阅读而缩短的连播内容 kind=deployment ---

 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