我正在尝试将一个值从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这似乎不起作用( edit :尽管此编辑注释后面的语句可能仍然正确,但这不起作用,因为kubectl不是busybox映像中的可识别命令),我假设pod只能从创建pod之前创建的configmap中读取值。有没有人遇到过在容器之间传递值的困难,你做了什么来解决这个问题?
我是不是应该在另一个pod中部署configmap,然后等到configmap存在时再部署这个pod?
(我知道我可以将文件写入卷,但除非绝对必要,否则我不愿这样做,因为这实质上意味着我们的docker镜像必须耦合到存在某些特定文件的环境中)
发布于 2019-08-14 19:50:24
如果由于各种原因,您不想使用共享卷。如果你想创建一个configmap或者一个密码,这里有一个解决方案。
首先,您需要使用一个docker镜像,其中包含kubectl : gcr.io/cloud-builders/kubectl:latest。(docker镜像,其中包含Google提供的kubectl管理)。
然后这个(Init)容器需要足够的权限在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: artifactorySnapshotUrlhttps://stackoverflow.com/questions/50012601
复制相似问题