Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

k8s: configMap does not work in deployment

We ran into an issue recently as to using environment variables inside container.

OS: windows 10 pro
k8s cluster: minikube
k8s version: 1.18.3

1. The way that doesn't work, though it's preferred way for us

Here is the deployment.yaml using 'envFrom':

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:

POSTGRES_HOST_AUTH_METHOD=trust

step 1:

kubectl create configmap db-configmap ./db.properties

step 2:

kebuctl apply -f ./deployment.yaml

step 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.

2. The way that works (we can't work with this approach)

Here is the deployment.yaml using 'env':

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:

kubectl apply -f ./deployment.yaml

step 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

like image 828
zeisen Avatar asked Jul 24 '20 18:07

zeisen


People also ask

How configmap is stored in K8s?

Let us understand how this ConfigMap is stored in K8s. Through ConfigMap, we will mount the application.properties file into K8s cluster and it can be used in the application. Notice that the data section contains the contents of application.properties, and the key is the file name.

How to override the default file in K8s?

In simple terms, we are going to override the default file by supplying the file through ConfigMap. The first step is to create ConfigMap from the application.properties. Let us understand how this ConfigMap is stored in K8s. Through ConfigMap, we will mount the application.properties file into K8s cluster and it can be used in the application.

How long does it take for K8s to respond to configmap changes?

After a ConfigMap change, it doesn't apply the changes right away, but can take time. Like 5 minutes, or something like that. If that doesn't happen, you can report a bug about that specific version of k8s.

Does changing configmap restart the pod in a deployment?

I have a deployment which includes a configMap, persistentVolumeClaim, and a service. I have changed the configMap and re-applied the deployment to my cluster. I understand that this change does not automatically restart the pod in the deployment:


1 Answers

For creating config-maps for usecase-1. please use the below command

kubectl create configmap db-configmap --from-env-file db.properties
like image 168
Ramakrishnan M Avatar answered Sep 17 '22 02:09

Ramakrishnan M