容器中不存在Kubernetes部署对象环境变量

时间:2020-11-10 19:43:27

标签: kubernetes environment-variables

我目前正在学习Kubernetes,并且正面临一堵墙。 我尝试从我的YAML文件定义中传递环境变量 到我的容器。但是之后似乎没有这些变量。 kubectl exec <pod name> -- printenv为我提供了环境清单 变量。但是我在YAML文件中定义的文件不存在。

我在部署中定义了环境变量,如下所示:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-boot
  labels:
    app: hello-world-boot
spec:
  selector:
    matchLabels:
      app: hello-world-boot
  template:
    metadata:
      labels:
        app: hello-world-boot
  containers:
    - name: hello-world-boot
      image: lightmaze/hello-world-spring:latest
      env:
        - name: HELLO
          value: "Hello there"
        - name: WORLD
          value: "to the entire world"
      resources:
        limits:
          memory: "128Mi"
          cpu: "500m"
      ports:
        - containerPort: 8080
    selector:
    app: hello-world-boot

希望有人可以看到我在YAML中失败的地方:)

1 个答案:

答案 0 :(得分:2)

如果我更正了Deployment配置中的错误,使其看起来像这样:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-boot
  labels:
    app: hello-world-boot
spec:
  selector:
    matchLabels:
      app: hello-world-boot
  template:
    metadata:
      labels:
        app: hello-world-boot
    spec:
      containers:
        - name: hello-world-boot
          image: lightmaze/hello-world-spring:latest
          env:
            - name: HELLO
              value: "Hello there"
            - name: WORLD
              value: "to the entire world"
          resources:
            limits:
              memory: "128Mi"
              cpu: "500m"
          ports:
            - containerPort: 8080

并将其部署到我的本地minikube实例中:

$ kubectl apply -f pod.yml

然后它似乎按预期工作:

$ kubectl exec -it hello-world-boot-7568c4d7b5-ltbbr -- printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
HOSTNAME=hello-world-boot-7568c4d7b5-ltbbr
TERM=xterm
HELLO=Hello there
WORLD=to the entire world
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
LANG=C.UTF-8
JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
JAVA_VERSION=8u212
JAVA_ALPINE_VERSION=8.212.04-r0
HOME=/home/spring

如果查看上述输出,则可以同时看到在HELLO中定义的WORLDDeployment环境变量。