How to Mount Secrets and ConfigMaps as Volumes in OpenShift – EX280 Series 2 (Lesson 8)

Last updated: November 2026

In this lesson, you will learn how to mount secret and configmap as volumes in OpenShift.

You will also have practical ex280 exam practice questions and answers to prepare you for the ex280 exam.

Understanding The Subject Matter

In our previous OpenShift storage lesson, we explored the concept of volumes and learned how to create PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs). We then attached a PVC to a Pod to provide storage for an application.

In this lesson, we will look at another way to make data available inside a container: mounting a Secret and ConfigMap as volumes in OpenShift.

Secrets and ConfigMaps are Kubernetes objects that can be used as volume sources. They allow applications to access configuration files, credentials, certificates, and other required data directly from their container filesystem.

By the end of this lesson, you will understand how to:

  • Create a Secret and a ConfigMap using YAML manifests.
  • Reference both objects in an OpenShift Deployment.
  • Mount them into a container.
  • Verify the mounted files from inside the container.
  • Understand the difference between configuration volumes and persistent application storage.

1. Understanding Secrets and ConfigMaps as Volumes

A container may require certain files before an application can start or operate correctly.

For example, an application might need:

  • A configuration file.
  • An SSH key.
  • A TLS certificate.
  • A database connection configuration.
  • Application-specific settings.

Instead of copying these files into the container image, Kubernetes allows us to store the information in a Secret or ConfigMap and mount it into the container when the Pod is created.

1.1 What is a Secret?

A Secret is a Kubernetes object designed to store sensitive information, such as passwords, authentication tokens, and private keys.

When a Secret is mounted as a volume, Kubernetes makes its data available as files inside the container.

For example, a Secret containing a key named auth.key can be mounted so that the container can access it at:

/etc/authkey/auth.key

1.2 What is a ConfigMap?

A ConfigMap stores non-sensitive configuration data.

It can contain application configuration files, environment-specific settings, or other information that an application needs.

For example, a ConfigMap containing a key named nginx.extra can be mounted so that the file appears inside the container at:

/etc/extra/nginx.extra

ConfigMaps help separate application configuration from the container image. This allows the same image to be used in different environments with different configuration values.

1.3 Are Secret and ConfigMap volumes persistent?

Although Secrets and ConfigMaps are mounted using the Kubernetes volume mechanism, they are not equivalent to a PersistentVolume or PersistentVolumeClaim.

They provide configuration data to a Pod. They are not intended to store application data such as database files or user-uploaded content.

Volume sourceMain purpose
SecretProvide sensitive data to a container
ConfigMapProvide non-sensitive configuration
PersistentVolumeClaimProvide storage for application data

Secret and ConfigMap volumes are read-only. Their contents are managed through the Kubernetes API rather than by writing files back from the container.

How To Mount Secret and ConfigMap as Volumes in OpenShift

ACTION TIME

2. Create the Secret Object

Let us begin by creating a Secret.

For this demonstration, we will create a Secret named nginx-secret. It will contain a file named auth.key.

Create a file named nginx-secret.yml:

apiVersion: v1
kind: Secret
metadata:
  name: nginx-secret
type: Opaque
data:
  auth.key: |
    UGE1NXdvcmQ=

In this manifest:

  • apiVersion: v1 identifies the Kubernetes API version.
  • kind: Secret defines the object type.
  • metadata.name specifies the Secret name.
  • type: Opaque indicates a generic Secret.
  • data contains the Secret’s encoded values.
  • auth.key is the key that will become the filename when mounted.

The value shown above is Base64-encoded demonstration data. Base64 is an encoding format, not encryption.

You can also use stringData to provide plain-text values in a manifest; Kubernetes converts them into the stored Secret data representation.

Create the Secret:

[victor@sno ~]$ oc create -f nginx-secret.yml

secret/nginx-secret created

Verify that the Secret exists:

[victor@sno ~]$ oc get secret

NAME                       TYPE                                  DATA   AGE
builder-dockercfg-bkmg6    kubernetes.io/dockercfg               1      26d
builder-token-nxfbd         kubernetes.io/service-account-token   4      26d
default-dockercfg-jh8r5     kubernetes.io/dockercfg               1      26d
default-token-rcslw         kubernetes.io/service-account-token   4      26d
deployer-dockercfg-lkssj    kubernetes.io/dockercfg               1      26d
deployer-token-dczbw        kubernetes.io/service-account-token   4      26d
nginx-secret               Opaque                                1      7s

The DATA column shows the number of data entries in the Secret. It does not display their values.

3. Create the ConfigMap Object

Next, we will create a ConfigMap named nginx-configmap.

This ConfigMap will contain a configuration file named nginx.extra.

Create a file named nginx-configmap.yml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  nginx.extra: |
    service_1_port 3030
    log_server_port 4040
    listener_port 33366

The important fields are:

  • kind: ConfigMap defines the object type.
  • metadata.name specifies the ConfigMap name.
  • data contains the configuration entries.
  • nginx.extra is the key that will become the filename when mounted.

Create the ConfigMap:

[victor@sno ~]$ oc create -f nginx-configmap.yml

configmap/nginx-configmap created

Verify it:

[victor@sno ~]$ oc get cm

NAME                       DATA   AGE
kube-root-ca.crt           1      26d
nginx-configmap            1      7s
openshift-service-ca.crt   1      26d

We have now created both objects.

4. Create the Nginx Deployment and Mount the Volumes

Now that the Secret and ConfigMap exist, we can reference them from a Deployment.

The process involves two related sections in the Pod template:

  1. volumes defines the volume sources and gives each volume a name.
  2. volumeMounts connects those named volumes to paths inside a container.

The volume name in volumeMounts must match the corresponding name under volumes.

Create a file named nginx-deploy-secret.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: nginx-secret-file
              mountPath: /etc/authkey
              readOnly: true
            - name: nginx-cm-file
              mountPath: /etc/extra
              readOnly: true
      volumes:
        - name: nginx-secret-file
          secret:
            secretName: nginx-secret
        - name: nginx-cm-file
          configMap:
            name: nginx-configmap

Let us examine the important parts of this manifest.

4.1 The volumes section

The volumes section is defined under the Pod’s spec.

volumes:
  - name: nginx-secret-file
    secret:
      secretName: nginx-secret
  - name: nginx-cm-file
    configMap:
      name: nginx-configmap

The first volume references the Secret object named nginx-secret.

The second volume references the ConfigMap object named nginx-configmap.

Notice that the volume names-nginx-secret-file and nginx-cm-file-are names we choose for use inside the Pod specification. They are not the names of the Secret and ConfigMap objects themselves.

4.2 The volumeMounts section

The volumeMounts section is defined inside the container specification:

volumeMounts:
  - name: nginx-secret-file
    mountPath: /etc/authkey
    readOnly: true
  - name: nginx-cm-file
    mountPath: /etc/extra
    readOnly: true

The first mount connects the Secret volume to the container at:

/etc/authkey

The second mount connects the ConfigMap volume to the container at:

/etc/extra

Because the volume names match those defined under volumes, Kubernetes knows which source to mount at each path.

The mounted files will be:

/etc/authkey/auth.key
/etc/extra/nginx.extra

This is because, by default, each key in a Secret or ConfigMap becomes a filename inside the mount directory.

5. Deploy the Application

Create the Deployment:

[victor@sno ~]$ oc create -f nginx-deploy-secret.yml

deployment.apps/nginx created

Check the Pods:

[victor@sno ~]$ oc get pods

NAME                     READY   STATUS              RESTARTS   AGE
nginx-79fbb4788b-6x6dd   0/1     ContainerCreating   0          7s

The Pod is still being created. Let us check again:

[victor@sno ~]$ oc get pods

NAME                     READY   STATUS    RESTARTS   AGE
nginx-79fbb4788b-6x6dd   1/1     Running   0          12s

The Pod is now running and ready.

6. Verify the Secret and ConfigMap Mounts

Once the Pod is running, we can verify that the files have been mounted into the container.

We will use oc exec to run commands inside the container.

The current syntax includes -- before the command:

oc exec -it <pod-name> -- sh

In our lab, the Pod name is nginx-79fbb4788b-6x6dd.

[victor@sno ~]$ oc exec -it nginx-79fbb4788b-6x6dd -- sh
#

We are now inside the container.

6.1 Verify the Secret mount

Move to the /etc directory and list its contents:

# cd /etc
# ls

Among the listed directories, you should see:

authkey
extra

Let us inspect the Secret mount:

# cd authkey
# ls
auth.key

The file auth.key is present.

Display its content:

# cat auth.key
Pa55word#

This demonstrates that the Secret data is available as a file inside the container.

Check the current directory:

# pwd
/etc/authkey

We have confirmed that the Secret is mounted at /etc/authkey.

6.2 Verify the ConfigMap mount

Return to /etc and enter the ConfigMap mount directory:

# cd ..
# cd extra
# ls
nginx.extra

The file nginx.extra is present.

Display its content:

# cat nginx.extra
service_1_port 3030
log_server_port 4040
listener_port 33366

We have confirmed that the ConfigMap is mounted at /etc/extra and that its configuration data is available inside the container.

The complete verification looks like this:

[victor@sno ~]$ oc exec -it nginx-79fbb4788b-6x6dd -- sh
# cd /etc
# ls
authkey
extra
# cd authkey
# ls
auth.key
# cat auth.key
Pa55word#
# pwd
/etc/authkey
# cd ..
# cd extra
# ls
nginx.extra
# cat nginx.extra
service_1_port 3030
log_server_port 4040
listener_port 33366

This confirms that both volume sources have been mounted successfully.

7. Mount Only Selected Keys as Files

Sometimes a Secret or ConfigMap contains multiple entries, but an application needs only one of them.

You can use the items field to select keys and specify their filenames or paths inside the volume.

For example:

volumes:
  - name: nginx-cm-file
    configMap:
      name: nginx-configmap
      items:
        - key: nginx.extra
          path: custom/nginx.extra

With the mount path set to /etc/extra, the file will be available at:

/etc/extra/custom/nginx.extra

The same approach can be used with Secret volumes.

When you specify items, only the listed keys are projected into the volume. The referenced keys must exist in the source object.

8. Important Things to Know

8.1 Secret and ConfigMap objects must exist first

A Pod that references a required Secret or ConfigMap cannot successfully mount the volume if the referenced object is missing.

Create the objects before deploying the application.

8.2 Secret and ConfigMap volumes are read-only

These volume sources are intended to provide data to containers, not to receive changes from the application.

If an application needs to modify files or store application data, use an appropriate writable storage volume, such as a PVC.

8.3 Updates are eventually reflected in mounted files

When a Secret or ConfigMap is updated, Kubernetes can update the projected files in a mounted volume. The update is not necessarily immediate.

An application may also need to reload its configuration before it uses the new content.

Important exception: If a Secret or ConfigMap file is mounted using subPath, automatic updates to that mounted file are not propagated. Plan for an application restart or another update mechanism when using this approach.

8.4 Keep sensitive data out of ConfigMaps

Do not store passwords, private keys, tokens, or other confidential information in a ConfigMap.

Use a Secret for sensitive values and apply appropriate access controls.

8.5 Base64 is not encryption

Secret data stored under data is Base64-encoded. Base64 is an encoding format, not encryption.

For production workloads, follow your organization’s Secret management and security practices.

9. Clean Up the Lab

When you have finished the demonstration, remove the Deployment, Secret, and ConfigMap:

[victor@sno ~]$ oc delete deployment nginx
deployment.apps "nginx" deleted

[victor@sno ~]$ oc delete secret nginx-secret
secret "nginx-secret" deleted

[victor@sno ~]$ oc delete configmap nginx-configmap
configmap "nginx-configmap" deleted

Alternatively, you can remove the resources using their YAML files:

[victor@sno ~]$ oc delete -f nginx-deploy-secret.yml
[victor@sno ~]$ oc delete -f nginx-secret.yml
[victor@sno ~]$ oc delete -f nginx-configmap.yml

Summary

In this lesson, we learned how to mount a Secret and a ConfigMap into a container running on OpenShift.

The process is straightforward:

  1. Create a Secret containing sensitive data.
  2. Create a ConfigMap containing non-sensitive configuration.
  3. Define both objects as volume sources in the Pod specification.
  4. Use volumeMounts to mount the volumes at the required container paths.
  5. Verify the files from inside the container.

Remember that Secret and ConfigMap volumes provide configuration files to containers; they are not replacements for PersistentVolumes and PersistentVolumeClaims used to store application data.

Recent EX280 Exam Questions & Answers On How to Mount Secret and ConfigMap as Volumes in OpenShift – EX280 Series 2

For Complete Exam Practice Questions & Answer, use this link.

For EX280 Exam Tips, use this link


Create a secret with the name, “secret-santa” that has the key value pair of
MYSQL_ROOT_PASSWORD=c29tZXRoaW5n in “limit-range” project


There is an already existing pod from the deployment name of, “frontdb”, and in the
project, “limit-range” namespace. make the pod use the “secret-santa” secret you
just created.


Answer

Method 1: Imperative Approach

Step 1: Verify that the project exists

Check whether the limit-range project already exists:

[api@sno ~]$ oc get project limit-range
NAME          DISPLAY NAME   STATUS
limit-range                  Active

If the project does not exist, create it:

[api@sno ~]$ oc new-project limit-range

If you attempt to create a project that already exists, OpenShift returns an AlreadyExists error. This is not a problem; simply continue using the existing project.

Step 2: Create the Secret using the CLI

Create the Secret named secret-santa with the required key/value pair:

[api@sno ~]$ oc create secret generic secret-santa \
  --from-literal=MYSQL_ROOT_PASSWORD=c29tZXRoaW5n \
  -n limit-range

secret/secret-santa created

The command creates an Opaque Secret by default.

Step 3: Verify that the Secret was created

Run:

[api@sno ~]$ oc get secret secret-santa -n limit-range

NAME           TYPE     DATA   AGE
secret-santa   Opaque   1      10s

The DATA column shows 1 because the Secret contains one key.

To verify the key name without displaying its value:

[api@sno ~]$ oc get secret secret-santa \
  -n limit-range \
  -o jsonpath='{.data}'

Expected output:

{"MYSQL_ROOT_PASSWORD":"c29tZXRoaW5n"}

You can also list the Secret using:

[api@sno ~]$ oc get secrets -n limit-range | grep secret-santa

Method 2: Declarative Approach

In the declarative approach, you define the Secret configuration in a YAML manifest and create it using oc apply.

Step 1: Create the YAML manifest

Create a file named secret-santa.yaml:

[api@sno ~]$ vi secret-santa.yaml

Add the following configuration:

apiVersion: v1
kind: Secret
metadata:
  name: secret-santa
  namespace: limit-range
type: Opaque
data:
  MYSQL_ROOT_PASSWORD: c29tZXRoaW5n

Save and close the file.

Understanding the YAML configuration

  • apiVersion: v1 specifies the Kubernetes API version for the Secret.
  • kind: Secret identifies the resource being created.
  • metadata.name specifies the Secret name.
  • metadata.namespace specifies the project where the Secret will be created.
  • type: Opaque indicates a general-purpose Secret.
  • data contains the Secret’s key/value pair.

Important: Values under data must be Base64-encoded. The value c29tZXRoaW5n is the Base64 representation of the plain-text string something.

Alternatively, you can use stringData to provide the plain-text value directly:

apiVersion: v1
kind: Secret
metadata:
  name: secret-santa
  namespace: limit-range
type: Opaque
stringData:
  MYSQL_ROOT_PASSWORD: something

OpenShift automatically converts stringData values into the encoded representation when storing the Secret.

Step 2: Apply the YAML manifest

[api@sno ~]$ oc apply -f secret-santa.yaml

secret/secret-santa created

Step 3: Verify the Secret

[api@sno ~]$ oc get secret secret-santa -n limit-range

NAME           TYPE     DATA   AGE
secret-santa   Opaque   1      10s

for the second part,

set the pod to use the secret by using the environment variable option

[api@sno ~]$ oc set env deployment/frontdb --from secret/secret-santa -n limit
range 

deployment.apps/frontdb updated 

Solution Summary

oc get project limit-range
oc create secret generic secret-santa --from-literal=MYSQL_ROOT_PASSWORD=c29tZXRoaW5n -n limit-range
oc get secret secret-santa -n limit-range
oc set env deployment/frontdb --from secret/secret-santa -n limit-range

EX280 Exam Tips (2027)

  1. Know the Secret creation command:oc create secret generic <secret-name> --from-literal=KEY=VALUE
  2. Remember the project: Secrets are namespaced resources. Ensure you create the Secret in the correct project.
  3. Understand Opaque Secrets: They are used to store arbitrary key/value data, such as passwords, tokens, and configuration values.
  4. Know the difference between data and stringData: data expects Base64-encoded values, while stringData accepts plain text in a YAML manifest.
  5. Verify the Secret: Use oc get secret or oc describe secret to confirm that it exists.

Both methods can satisfy the task. The imperative approach creates the Secret directly using the CLI, while the declarative approach defines the desired Secret configuration in a YAML manifest.

Be the first to comment

Leave a Reply

Your email address will not be published.


*