从kubectl创建嵌套的ConfigMap

huangapple go评论82阅读模式
英文:

Create nested ConfigMap from kubectl

问题

我有一个配置需要从kubectl应用,但我不太明白它是什么:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. data:
  4. localHosting.v1: |
  5. host: "localhost:1234"
  6. containerHost: "container-name:5555"

我无法弄清楚如何使用 kubectl create configmap 命令来创建这个 ConfigMap。我以为 ConfigMaps 只是键值对,而 --from-literal 标志的使用似乎期望的是键值对,但这个配置似乎有额外的嵌套层级。

我知道通常你可以创建一个YAML文件或使用heredoc将整个YAML文件传递给kubectl,但在我的情况下这不是一个很好的解决方案。是否有一种方法可以构建 kubectl create configmap 命令来执行这个操作?

英文:

I have a config I need to apply from kubectl, but I don't quite understand what it is:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. data:
  4. localHosting.v1: |
  5. host: "localhost:1234"
  6. containerHost: "container-name:5555"

I can't figure out how to make this configmap using a kubectl create configmap invocation though. I thought that ConfigMaps were just key-value pairs, and the usage of the --from-literal flag seems to expect that, but this config seems to have an additional level of nesting.

I know you could typically create a YAML file or pipe in the whole YAML file using a heredoc, but that's not a very good solution in my situation. Is there a way to craft kubectl create configmap command to do this?

答案1

得分: 2

They are just key-value pairs. In your example, localHosting.v1 is the key, and the string...

  1. host: "localhost:1234"
  2. containerHost: "container-name:5555"

...is the value. You can create this from the command line by running something like:

  1. kubectl create cm myconfigmap '--from-literal=localHosting.v1=host: "localhost:1234" containerHost: "container-name:5555"'

Which results in:

  1. $ kubectl get cm myconfigmap -o yaml
  2. apiVersion: v1
  3. data:
  4. localHosting.v1: |
  5. host: "localhost:1234"
  6. containerHost: "container-name:5555"
  7. kind: ConfigMap
  8. metadata:
  9. creationTimestamp: "2023-07-14T02:28:14Z"
  10. name: myconfigmap
  11. namespace: sandbox
  12. resourceVersion: "57813424"
  13. uid: a126c38f-6836-4875-9a1b-7291910b7490

As with any other shell command, we use single quotes (') to quote a multi-line string value.

英文:

> I thought that ConfigMaps were just key-value pairs...

They are just key-value pairs. In your example, localHosting.v1 is the key, and the string...

  1. host: "localhost:1234"
  2. containerHost: "container-name:5555"

...is the value. You can create this from the command line by running something like:

  1. kubectl create cm myconfigmap '--from-literal=localHosting.v1=host: "localhost:1234"
  2. containerHost: "container-name:5555
  3. '

Which results in:

  1. $ kubectl get cm myconfigmap -o yaml
  2. apiVersion: v1
  3. data:
  4. localHosting.v1: |
  5. host: "localhost:1234"
  6. containerHost: "container-name:5555
  7. kind: ConfigMap
  8. metadata:
  9. creationTimestamp: "2023-07-14T02:28:14Z"
  10. name: myconfigmap
  11. namespace: sandbox
  12. resourceVersion: "57813424"
  13. uid: a126c38f-6836-4875-9a1b-7291910b7490

As with any other shell command, we use single quotes (') to quote a multi-line string value.

huangapple
  • 本文由 发表于 2023年7月14日 09:19:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76684148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定