Kubernetes是否以JSON格式作为输入文件来创建configmap和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"
}]
}

我知道我可以通过带有--from file选项的键/值对属性文件来创建configmap和secret。

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

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

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

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

例如,您创建的配置映射将类似

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访问这样的配置映射,您必须将其作为卷安装到您的Pod中:

如何装载配置映射

不幸的是,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已经部署,但这里出现了问题。最终,这个解决方案对我起到了作用(类似,但有一些额外的功能(:

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

最新更新