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

时间:2018-04-25 00:58:06

标签: kubernetes

我正试图通过"从init容器到容器的值。由于configmap中的值是在命名空间中共享的,因此我认为我可以将其用于此目的。这是我的job.yaml(带有伪造的信息):

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

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

我应该在另一个pod中部署configmap并等待部署此配置,直到configmap存在?

(我知道我可以将文件写入卷,但我不想走这条路,除非它绝对必要,因为它实质上意味着我们的码头图像必须耦合到某个特定的环境文件存在)

3 个答案:

答案 0 :(得分:5)

您可以创建EmptyDir卷,并将此卷装入两个容器。与persistent volume不同,EmptyDir没有可移植性问题。

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"
        volumeMounts:
        - name: tmp
          mountPath: /tmp/artifact
      initContainers:
      - name: artifactory-snapshot
        image: busybox
        command: ['/bin/sh', '-c', 'cp x /tmp/artifact/x']
        volumeMounts:
        - name: tmp
          mountPath: /tmp/artifact
      restartPolicy: Never
      volumes:
      - name: tmp
        emptyDir: {}
  backoffLimit: 0

答案 1 :(得分:3)

首先,kubectl是二进制文件。在您使用该命令之前,它已下载到您的计算机中。但是,在您的POD中,kubectl二进制文件不存在。因此,您无法使用busybox映像中的kubectl命令。

此外,kubectl使用保存在您计算机中的某些凭据(可能在~/.kube路径中)。因此,如果您尝试在图像中使用kubectl,则由于缺少凭据而失败。

对于您的方案,我将建议与@ccshih相同,使用卷共享。 以下是init-containercontainer之间的official doc about volume sharing

这里使用的yaml是,

apiVersion: v1
kind: Pod
metadata:
  name: init-demo
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
    volumeMounts:
    - name: workdir
      mountPath: /usr/share/nginx/html
  # These containers are run during pod initialization
  initContainers:
  - name: install
    image: busybox
    command:
    - wget
    - "-O"
    - "/work-dir/index.html"
    - http://kubernetes.io
    volumeMounts:
    - name: workdir
      mountPath: "/work-dir"
  dnsPolicy: Default
  volumes:
  - name: workdir
    emptyDir: {}

此处init-containers将文件保存在卷中,稍后文件在容器内可用。自己尝试本教程以便更好地理解。

答案 2 :(得分:0)

由于各种原因,您不想使用共享量。您想创建一个configmap或一个秘密,这是一个解决方案。

首先,您需要使用包含kubectl的docker映像:例如gcr.io/cloud-builders/kubectl:latest。 (包含由Google管理的kubectl的docker映像。)

然后,此(初始)容器需要足够的权限才能在Kubernetes集群上创建资源。好的,默认情况下,kubernetes将默认服务帐户的令牌注入到容器中,该令牌的名称为“ default”,但是我希望更明确一些,然后添加以下行:

...
      initContainers:
        - # Already true by default but if use it, prefer to make it explicit
          automountServiceAccountToken: true
          name: artifactory-snapshot

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

kubectl create rolebinding default-edit-rb --clusterrole=edit --serviceaccount=default:myapp --namespace=default

然后是完整的示例:

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