使用环境变量
您的应用将数据作为环境变量获取会很方便。
来自ConfigMap 的环境变量
对于非敏感数据,您可以将变量存储在 ConfigMap 中,然后使用 ConfigMap 数据定义容器环境变量。
Example from Kubernetes docs:
首先创建ConfigMap。档案configmaps.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data:
log_level: INFO
创建 ConfigMap:
kubectl create -f ./configmaps.yaml
然后在Pod规范中定义环境变量pod-multiple-configmap-env-variable.yaml:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: env-config
key: log_level
restartPolicy: Never
创建Pod:
kubectl create -f ./pod-multiple-configmap-env-variable.yaml
现在在您的控制器中,您可以读取这些环境变量SPECIAL_LEVEL_KEY(这将为您提供来自special-configConfigMap 的special.how 值)和LOG_LEVEL(这将为您提供来自env-config 的log_level 值ConfigMap):
例如:
specialLevelKey := os.Getenv("SPECIAL_LEVEL_KEY")
logLevel := os.Getenv("LOG_LEVEL")
fmt.Println("SPECIAL_LEVEL_KEY:", specialLevelKey)
fmt.Println("LOG_LEVEL:", logLevel)
来自Secret 的环境变量
如果您的数据很敏感,您可以将其存储在Secret 中,然后将Secret 用作环境变量。
To create a Secret manually:
您首先需要使用base64 对字符串进行编码。
# encode username
$ echo -n 'admin' | base64
YWRtaW4=
# encode password
$ echo -n '1f2d1e2e67df' | base64
MWYyZDFlMmU2N2Rm
然后用上面的数据创建一个Secret:
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
用kubectl apply 创建一个Secret:
$ kubectl apply -f ./secret.yaml
请注意,还有其他方法可以创建秘密,请选择最适合您的方法:
现在你可以use this created Secret for environment variables。
在 Pod 的环境变量中使用密钥:
- 创建一个秘密或使用现有的。多个 Pod 可以引用同一个 Secret。
- 修改您希望使用密钥值的每个容器中的 Pod 定义,以便为您希望使用的每个密钥添加环境变量。使用密钥的环境变量应在
env[].valueFrom.secretKeyRef 中填充密钥的名称和密钥。
- 修改您的图像和/或命令行,以便程序在指定的环境变量中查找值。
这是来自 Kubernetes 文档的 Pod 示例,它展示了如何将 Secret 用于环境变量:
apiVersion: v1
kind: Pod
metadata:
name: secret-env-pod
spec:
containers:
- name: mycontainer
image: redis
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
restartPolicy: Never
最后,如文档中所述:
在使用环境变量中的密钥的容器内,密钥显示为包含密钥数据的 base64 解码值的普通环境变量。
现在在您的控制器中,您可以读取这些环境变量SECRET_USERNAME(这将为您提供来自mysecret Secret 的username 值)和SECRET_PASSWORD(这将为您提供来自mysecret 的password 值Secret):
例如:
username := os.Getenv("SECRET_USERNAME")
password := os.Getenv("SECRET_PASSWORD")
使用卷
您还可以将 ConfigMap 和 Secret 作为卷安装到您的 pod。
Populate a Volume with data stored in a ConfigMap:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: special-config
restartPolicy: Never
Using Secrets as files from a Pod:
在 Pod 的卷中使用 Secret:
- 创建一个秘密或使用现有的。多个 Pod 可以引用同一个 Secret。
- 修改您的 Pod 定义以在 .spec.volumes[] 下添加一个卷。将卷命名为任何名称,并有一个 .spec.volumes[].secret.secretName 字段等于 Secret 对象的名称。
- 为每个需要密钥的容器添加
.spec.containers[].volumeMounts[]。将 .spec.containers[].volumeMounts[].readOnly = true 和 .spec.containers[].volumeMounts[].mountPath 指定为您希望在其中显示机密的未使用目录名称。
修改您的图像或命令行,以便程序在该目录中查找文件。秘密 data 映射中的每个密钥都成为 mountPath 下的文件名。
将Secret 挂载到卷中的Pod 示例:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: redis
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret