WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kubernetes · Advanced · question 57 of 100

Explain how Kubernetes manages Secrets and ConfigMaps, and how they can be used to inject configuration data into applications.?

📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

Kubernetes provides two resources, Secrets and ConfigMaps, to store and manage configuration data that can be injected into containers as environment variables, command-line arguments, or mounted files.

Secrets in Kubernetes

Secrets are Kubernetes objects that store sensitive information, such as passwords, authentication tokens, or SSL certificates, in an encrypted form. Secrets can be created manually or generated from existing files using the kubectl create secret command. There are four types of Secrets:

generic: stores arbitrary key-value pairs

docker-registry: stores authentication credentials for a Docker registry

tls: stores a pair of SSL certificates and their private key

ssh: stores an SSH private key and its public key

Secrets are base64-encoded when stored, but they can be decoded when mounted into containers. Secrets can be mounted as environment variables or as files inside a container. To mount a Secret as an environment variable, define a container environment variable and set its value to the secret key, like this:

env:
- name: DB\_PASSWORD
  valueFrom:
    secretKeyRef:
      name: db-credentials
      key: password

To mount a Secret as a file, define a volume that references the Secret and mount the volume into the container, like this:

volumes:
- name: db-secrets
  secret:
    secretName: db-credentials
    items:
    - key: username
      path: db-username
    - key: password
      path: db-password
containers:
- name: app
  image: myapp:latest
  volumeMounts:
  - name: db-secrets
    mountPath: /etc/db-secrets

ConfigMaps in Kubernetes

ConfigMaps are Kubernetes objects that store non-sensitive configuration data, such as configuration files, command-line arguments, or environment variables, in a key-value pair format. ConfigMaps can be created manually or generated from existing files or directories using the kubectl create configmap command. ConfigMaps can be used in a similar way to Secrets, but they are not encrypted and can be used to store non-sensitive data.

ConfigMaps can be mounted as environment variables or as files inside a container. To mount a ConfigMap as an environment variable, define a container environment variable and set its value to the ConfigMap key, like this:

env:
- name: APP\_CONFIG
  valueFrom:
    configMapKeyRef:
      name: app-config
      key: config-file

To mount a ConfigMap as a file, define a volume that references the ConfigMap and mount the volume into the container, like this:

volumes:
- name: app-config
  configMap:
    name: app-config
containers:
- name: app
  image: myapp:latest
  volumeMounts:
  - name: app-config
    mountPath: /etc/app-config

Injecting Secrets and ConfigMaps into a Pod

Both Secrets and ConfigMaps can be injected into a Pod as environment variables or as files. To inject a Secret or a ConfigMap into a Pod as an environment variable, use the envFrom field and set its value to a list of secretRef or configMapRef objects, like this:

envFrom:
- secretRef:
    name: db-credentials
- configMapRef:
    name: app-config

To inject a Secret or a ConfigMap into a Pod as a file, define a volume that references the Secret or the ConfigMap and mount the volume into the container, like this:

volumes:
- name: db-secrets
  secret:
    secretName:
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kubernetes interview — then scores it.
📞 Practice Kubernetes — free 15 min
📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

All 100 Kubernetes questions · All topics