如何在 kubernetes secret 中使用带连字符的连字符键?

问题描述

我想在 pod 中注入以下密钥/值:test-with=1 和 testwith=1。首先我创建秘密:

kubectl create secret generic test --from-literal=test-with=1 --from-literal=testwith=0

然后我为具有以下规范的 pod 创建一个 yaml 文件

containers:
  ...
  envFrom:
    - secretRef:
        name: test

pod 正在运行,但是容器内 env 命令的结果只显示

...
TERM=xterm
testwith=0
...

test-with=1 没有出现。我如何声明秘密以查看键/值?

解决方法

通过 printenv 查看时,名称中带有分隔符的变量显示在顶部。

检查:

$ kubectl create secret generic test --from-literal=test-with=1 --from-literal=testwith=0

$ kubectl get secret/test -o yaml
apiVersion: v1
data:
  test-with: MQ==
  testwith: MA==
kind: Secret
metadata:
  ...
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: check-env
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: nginx
    image: nginx
    envFrom:
    - secretRef:
        name: test
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
  hostNetwork: true
  dnsPolicy: Default
EOF
$ kubectl exec -it shell-demo -- printenv | grep test

test-with=1
testwith=0

GKE v1.18.16-gke.502

,

我没有看到任何问题。这是我复制它的方式:

kubectl create secret generic test --from-literal=test-with=1 --from-literal=testwith=0

cat<<EOF | kubectl apply -f- 
    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        run: cent
      name: cent
    spec:
      containers:
      - image: centos:7
        name: cent
        command:
        - sleep
        - "9999"
        envFrom:
          - secretRef:
              name: test
EOF

➜  ~ kubectl exec -it cent -- bash
[root@cent /]# env  | grep test
test-with=1
testwith=0

很可能是图片问题