我试图从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
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用(编辑:虽然此编辑注释后面的语句可能仍然是正确的,这是行不通的,因为kubectl不是busybox图像中的可识别命令),我假设pod只能读取来自在创建pod之前创建的配置映射.有没有其他人遇到过在容器之间传递值的困难,你做了什么来解决这个问题?
我应该在另一个pod中部署configmap并等待部署此配置,直到configmap存在?
(我知道我可以将文件写入卷,但我宁愿不去那条路线,除非它绝对必要,因为它实质上意味着我们的docker镜像必须耦合到存在某些特定文件的环境)
您可以创建一个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
Run Code Online (Sandbox Code Playgroud)
如果您出于各种原因,不想使用共享卷。如果你想创建一个 configmap 或一个秘密,这里有一个解决方案。
首先,您需要使用包含 kubectl 的 docker 镜像:例如 gcr.io/cloud-builders/kubectl:latest。(包含由 Google 管理的 kubectl 的 docker 镜像)。
然后这个(init)容器需要足够的权限来在 Kubernetes 集群上创建资源。好的,默认情况下,kubernetes 在容器中注入一个名为“default”的默认服务帐户的令牌,但我更喜欢更明确,然后添加以下行:
...
initContainers:
- # Already true by default but if use it, prefer to make it explicit
automountServiceAccountToken: true
name: artifactory-snapshot
Run Code Online (Sandbox Code Playgroud)
并将“编辑”角色添加到“默认”服务帐户:
...
initContainers:
- # Already true by default but if use it, prefer to make it explicit
automountServiceAccountToken: true
name: artifactory-snapshot
Run Code Online (Sandbox Code Playgroud)
然后完成示例:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3085 次 |
| 最近记录: |