首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我可以在pod中使用从init容器创建的configmap吗

我可以在pod中使用从init容器创建的configmap吗
EN

Stack Overflow用户
提问于 2018-04-25 08:58:06
回答 3查看 12K关注 0票数 12

我正在尝试将一个值从init容器“传递”到一个容器。由于configmap中的值是跨名称空间共享的,因此我想我可以将其用于此目的。这是我的job.yaml (带有伪造的信息):

代码语言:javascript
运行
复制
apiVersion: batch/v1
kind: Job
metadata:
  name: installer-test
spec:
  template:
    spec:
      containers:
      - name: installer-test
        image: installer-test:latest
        env:
        - name: clusterId
          value: "some_cluster_id"
        - name: in_artifactoryUrl
          valueFrom:
            configMapKeyRef:
              name: test-config
              key: artifactorySnapshotUrl
      initContainers:
      - name: artifactory-snapshot
        image: busybox
        command: ['kubectl', 'create configmap test-config --from-literal=artifactorySnapshotUrl=http://artifactory.com/some/url']
      restartPolicy: Never
  backoffLimit: 0

这似乎不起作用( edit :尽管此编辑注释后面的语句可能仍然正确,但这不起作用,因为kubectl不是busybox映像中的可识别命令),我假设pod只能从创建pod之前创建的configmap中读取值。有没有人遇到过在容器之间传递值的困难,你做了什么来解决这个问题?

我是不是应该在另一个pod中部署configmap,然后等到configmap存在时再部署这个pod?

(我知道我可以将文件写入卷,但除非绝对必要,否则我不愿这样做,因为这实质上意味着我们的docker镜像必须耦合到存在某些特定文件的环境中)

EN

Stack Overflow用户

发布于 2019-08-14 19:50:24

如果由于各种原因,您不想使用共享卷。如果你想创建一个configmap或者一个密码,这里有一个解决方案。

首先,您需要使用一个docker镜像,其中包含kubectl : gcr.io/cloud-builders/kubectl:latest。(docker镜像,其中包含Google提供的kubectl管理)。

然后这个(Init)容器需要足够的权限在Kubernetes集群上创建资源。好的,默认情况下,kubernetes在容器中注入一个名为" default“的默认服务帐户的标记,但我更喜欢更明确,然后添加以下行:

代码语言:javascript
运行
复制
...
      initContainers:
        - # Already true by default but if use it, prefer to make it explicit
          automountServiceAccountToken: true
          name: artifactory-snapshot

并将“编辑”角色添加到“默认”服务帐户:

代码语言:javascript
运行
复制
kubectl create rolebinding default-edit-rb --clusterrole=edit --serviceaccount=default:myapp --namespace=default

然后完成示例:

代码语言:javascript
运行
复制
apiVersion: batch/v1
kind: Job
metadata:
  name: installer-test
spec:
  template:
    spec:
      initContainers:
        - # Already true by default but if use it, prefer to make it explicit.
          automountServiceAccountToken: true
          name: artifactory-snapshot
          # You need to use docker image which contains kubectl
          image: gcr.io/cloud-builders/kubectl:latest
          command:
            - sh
            - -c
            # the "--dry-run -o yaml | kubectl apply -f -" is to make command idempotent
            - kubectl create configmap test-config --from-literal=artifactorySnapshotUrl=http://artifactory.com/some/url --dry-run -o yaml | kubectl apply -f -
      containers:
        - name: installer-test
          image: installer-test:latest
          env:
            - name: clusterId
              value: "some_cluster_id"
            - name: in_artifactoryUrl
              valueFrom:
                configMapKeyRef:
                  name: test-config
                  key: artifactorySnapshotUrl
票数 6
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50012601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档