Skip to main content
In this lab, you’ll learn how to work with Kubernetes Secrets. We’ll start by inspecting the default Namespace’s secrets, then create a custom secret for database credentials and configure a web application pod to use it.

Inspecting Existing Secrets

Question 1: How many Secrets exist in the default Namespace?

Run the following command:
You’ll see output similar to:
This indicates that only one secret exists in the default Namespace.

Question 2: How many pieces of secret data are defined in the default token secret?

Inspect the details by running:
The output shows:
This secret contains three key data fields:
  • ca.crt
  • namespace
  • token
Thus, there are three pieces of secret data defined.

Question 3: What is the type of the default token secret?

To confirm the secret type, first list the secrets:
Then describe the secret:
The output reiterates:
The secret type is clearly “kubernetes.io/service-account-token”. Remember, the “namespace” listed in the secret data is not the secret type.

Deploying an Application with Secrets

The application deployment follows a specific architecture where required pods and services are already running. Verify their current state by running:
Then, check the secret details:
Next, review the pods and services:
  • List Deployments:
  • List Pods:
  • List Services:
Note that the default token secret is not used by the web application. It is instead intended to enable the web application to connect to the MySQL database.

Application Error and the Need for a New Secret

The web application is failing to connect to the MySQL database. The error message is:
This error indicates that the secret containing the database credentials has not been created. To resolve this, we need to create a new secret named “db-secret” with the necessary data fields.
Data required for the new secret:
  • DB_Host
  • DB_User
  • DB_Password

Creating the DB Secret

Before creating the new secret, check the help documentation:
You’ll see that subcommands include:
  • docker-registry
  • generic
  • tls
Since you’re creating a generic secret, the syntax is:
For this lab, create the secret with:
Verify the creation by listing the secrets:
Expected output:
And review the secret details:

Configuring the Web Application Pod to Use the New Secret

At present, the web application pod (webapp-pod) does not load the environment variables from the new “db-secret.” To pass these variables into the pod, update its configuration by referencing the secret.

Example Pod Configuration

Below is an example configuration that demonstrates how to load environment variables from a secret:
For the existing web application pod, find the container section in its pod definition and add an entry under envFrom to reference db-secret. For example:
After modifying the configuration, update the pod:
Once the pod has been recreated, verify that the environment variables are correctly loaded by checking:
This confirms that the web application is now receiving the required database credentials.
The image shows a Kubernetes pod configuration for "webapp-pod," detailing its status, container image, environment variables, and volume mounts.

Reviewing Best Practices for Using Secrets

Using secrets to store sensitive data such as database credentials is a common practice. However, by default, these secrets are stored in etcd without encryption, potentially leaving them exposed to anyone with access to the Kubernetes API server or the etcd database.
For enhanced security, consider enabling encryption at rest and proper role-based access controls (RBAC) to protect your secrets.
For more details on safely managing your secrets, refer to the Kubernetes Documentation on Secrets.
The image shows a Kubernetes documentation page about "Secrets," detailing their use for storing sensitive data and providing security precautions.

Conclusion

In this lab, you learned how to:
  1. Inspect the default service account secret in Kubernetes.
  2. Create a new generic secret named “db-secret” to store database credentials.
  3. Configure a web application pod to load environment variables from the newly created secret.
  4. Understand key security considerations when working with Kubernetes Secrets.
This concludes the lab on managing Kubernetes Secrets. Happy deploying!

Watch Video