简体   繁体   English

k8s:configMap 在部署中不起作用

[英]k8s: configMap does not work in deployment

We ran into an issue recently as to using environment variables inside container.我们最近遇到了一个关于在容器内使用环境变量的问题。

OS : windows 10 pro操作系统:windows 10 亲
k8s cluster : minikube k8s 集群:minikube
k8s version : 1.18.3 k8s 版本:1.18.3

1. The way that doesn't work, though it's preferred way for us 1. 行不通的方式,虽然它是我们的首选方式

Here is the deployment.yaml using 'envFrom':这是使用“envFrom”的部署。yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: db
  labels:
    app.kubernetes.io/name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: db
  template:
    metadata:
      labels:
        app.kubernetes.io/name: db
    spec:
      serviceAccountName: default
      securityContext:
        {}
      containers:
        - name: db
          image: "postgres:9.4"
          ports:
            - name: http
              containerPort: 5432
              protocol: TCP
          envFrom:
            - configMapRef:
                name: db-configmap

here is the db.properties:这是 db.properties:

POSTGRES_HOST_AUTH_METHOD=trust

step 1:步骤1:

kubectl create configmap db-configmap ./db.properties

step 2:第2步:

kebuctl apply -f ./deployment.yaml

step 3:第 3 步:

kubectl get pod

Run the above command, get the following result:运行上面的命令,得到如下结果:

db-8d7f7bcb9-7l788        0/1    CrashLoopBackOff   1      9s

That indicates the environment variables POSTGRES_HOST_AUTH_METHOD is not injected.这表明环境变量 POSTGRES_HOST_AUTH_METHOD注入。

2. The way that works (we can't work with this approach) 2. 工作方式(我们不能使用这种方法)

Here is the deployment.yaml using 'env':这是使用“env”的部署。yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: db
  labels:
    app.kubernetes.io/name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: db
  template:
    metadata:
      labels:
        app.kubernetes.io/name: db
    spec:
      serviceAccountName: default
      securityContext:
        {}
      containers:
        - name: db
          image: "postgres:9.4"
          ports:
            - name: http
              containerPort: 5432
              protocol: TCP
          env:
            - name: POSTGRES_HOST_AUTH_METHOD
              value: trust

step 1:步骤1:

kubectl apply -f ./deployment.yaml

step 2:第2步:

kubectl get pod

Run the above command, get the following result:运行上面的命令,得到如下结果:

db-fc58f998d-nxgnn                   1/1        Running        0            32s

the above indicates the environment is injected so that the db starts.以上表示环境已注入,以便数据库启动。

What did I do wrong in the first case?在第一种情况下我做错了什么?

Thank you in advance for the help.预先感谢您的帮助。

Update:更新:

Provide the configmap:提供配置图:

 kubectl describe configmap db-configmap
Name:         db-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
db.properties:
----
POSTGRES_HOST_AUTH_METHOD=trust

For creating config-maps for usecase-1.用于为 usecase-1 创建配置映射。 please use the below command请使用以下命令

kubectl create configmap db-configmap --from-env-file db.properties

Are you missing the key?你错过了钥匙吗? (see "key:" (no quotes) below) And I think you need to provide the name of the env-variable...which people usually use the key-name, but you don't have to. (请参阅下面的“键:”(无引号))而且我认为您需要提供环境变量的名称......人们通常使用键名,但您不必这样做。 I've repeated the same value ("POSTGRES_HOST_AUTH_METHOD") below as the environment variable NAME and the keyname of the config-map.我在下面重复了相同的值(“POSTGRES_HOST_AUTH_METHOD”)作为环境变量 NAME 和配置映射的键名。

  #start env .. where we add environment variables
env:
    # Define the environment variable
- name: POSTGRES_HOST_AUTH_METHOD
  #value: "UseHardCodedValueToDebugSometimes"

  valueFrom:
    configMapKeyRef:
          # The ConfigMap containing the value you want to assign to environment variable (above "name:")
      name: db-configmap 
          # Specify the key associated with the value
      key: POSTGRES_HOST_AUTH_METHOD      

My example (trying to use your values)....comes from this generic example:我的示例(尝试使用您的值)....来自这个通用示例:

https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data

pods/pod-single-configmap-env-variable.yaml pods/pod-single-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:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config
              # Specify the key associated with the value
              key: special.how
  restartPolicy: Never

PS附言

You can use "describe" to take a looksie at your config-map, after you (think:) ) you have set it up correctly.您可以使用“描述”来查看您的配置图,在您(认为:))正确设置之后。

kubectl describe configmap db-configmap --namespace=IfNotDefaultNameSpaceHere

See when you do it like you described.看看你什么时候像你描述的那样做。

deployment# exb db-7785cdd5d8-6cstw
root@db-7785cdd5d8-6cstw:/# env | grep -i TRUST
db.properties=POSTGRES_HOST_AUTH_METHOD=trust

the env set is not exactly POSTGRES_HOST_AUTH_METHOD its actually taking filename in env.环境集并不完全是 POSTGRES_HOST_AUTH_METHOD 它实际上在环境中采用文件名。 create configmap via kubectl create cm db-configmap --from-env-file db.properties and it will actually put env POSTGRES_HOST_AUTH_METHOD in pod.通过kubectl create cm db-configmap --from-env-file db.properties configmap,它实际上会将 env POSTGRES_HOST_AUTH_METHOD 放入 pod。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM