ConfigMaps of Kubernetes

73 views Asked by At

I'm new to kubernetes and Azure Kubernetes Services, and I'm experiencing problems when creating a Deployment object by applying a YAML file directly.

I have an image in my azure container registry with a NodeJs application that needs env variables.

This is the Dockerfile I used to create the image:

FROM node:18.15.0

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000

CMD [ "npm", "start" ]

I have created a ConfigMap such as the following:

kind: ConfigMap
metadata:
  name: catsapi-env
  namespace: catsapi
data:
  .env: |
    CONTAINER_ID= <COSMOS_CONTAINER>
    COSMOS_ENDPOINT= <COSMOS_ENDPOINT>
    COSMOS_KEY= <COSMOS_KEY>
    DATABASE_ID= <DATABASE_ID>
    PORT= <PORT>

In the config map I also tried listing the keys one by one instead of using the .env: |

And this is the Deployment

kind: Deployment
metadata:
  name: catsapi-deployment
  namespace: catsapi
spec:
  replicas: 1
  selector:
    matchLabels:
      app: catsapi
  template:
    metadata:
      labels:
        app: catsapi
    spec:
      containers:
        - name: catsapi-container
          image: <container-registry>
          ports:
            - containerPort: 3000
          env:
            - name: CONTAINER_ID
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: CONTAINER_ID
            - name: COSMOS_ENDPOINT
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: COSMOS_ENDPOINT
            - name: COSMOS_KEY
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: DATABASE_ID
            - name: PORT
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: PORT

When AKS executes this I get the CreateContainerConfigError. Some ideas? I've also tried to mount the configMap as a volume or the envFrom property and failed as well.

1

There are 1 answers

1
Daredevil On BEST ANSWER

The CreateContainerConfigError implies that there's a problem with how you've configured the container in the pod specification.

Possible Issues:

  1. Image Name: Ensure that your <container-registry> placeholder in the Deployment is replaced with the fully qualified image name (e.g., myregistry.azurecr.io/myimage:latest). Also, make sure AKS has the necessary permissions to pull from the Azure Container Registry.

  2. ConfigMap Reference: You have both multi-line format (.env: |) and individual key references in the Deployment. Please stick to one format for simplicity.

  3. ConfigMap Creation: Ensure that the ConfigMap was created in the catsapi namespace before you deploy the application. The order matters.

  4. Namespace: Ensure the catsapi namespace exists. If it doesn't, either create the namespace or deploy the resources in the default namespace.

I tried to use following in my AKS instance and it worked.

  1. ConfigMap:

    kind: ConfigMap
    metadata:
      name: catsapi-env
    data:
      CONTAINER_ID: "<COSMOS_CONTAINER>"
      COSMOS_ENDPOINT: "<COSMOS_ENDPOINT>"
      COSMOS_KEY: "<COSMOS_KEY>"
      DATABASE_ID: "<DATABASE_ID>"
      PORT: "<PORT>"
    
  2. Deployment:

    kind: Deployment
    metadata:
      name: catsapi-deployment
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: catsapi
      template:
        metadata:
          labels:
            app: catsapi
        spec:
          containers:
          - name: catsapi-container
            image: <container-registry>
            ports:
            - containerPort: 3000
            env:
            - name: CONTAINER_ID
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: CONTAINER_ID
            - name: COSMOS_ENDPOINT
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: COSMOS_ENDPOINT
            - name: COSMOS_KEY
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: COSMOS_KEY
            - name: DATABASE_ID
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: DATABASE_ID
            - name: PORT
              valueFrom:
                configMapKeyRef:
                  name: catsapi-env
                  key: PORT
    

If this is not working, Try Diving deeper into the error by Describing the pod to get more details about the error:

kubectl describe pod -n catsapi <POD_NAME>