Skip to main content
In this guide, we walk you through a lab exercise that focuses on managing Kubernetes Secrets. We’ll start by inspecting the default secret in the cluster, review its content, and then deploy an application that uses a custom secret for connecting to a MySQL database.

Inspecting Default Secrets

Begin by checking the number of secrets within the default namespace with the following command:
You should see an output similar to this:
Next, retrieve details about the default token secret:
The output will list several key items under the Data section:
  • ca.crt (570 bytes)
  • namespace (7 bytes)
  • token (a long encoded token)
This confirms there are three data items. Note that the secret type is kubernetes.io/service-account-token.

Reviewing the Secret Data

When you run the following command:
the output will still be:
Then, describing the secret with:
reveals the three keys:
  • ca.crt
  • namespace
  • token
Even though these keys represent secret data, the “type” field (kubernetes.io/service-account-token) is not classified as secret data.

Reviewing the Application Architecture

The next step involves inspecting the deployed application architecture. The necessary Pods and Services are already created. However, note that there is no deployment resource in the default namespace. Check deployments using:
Output:
Next, verify the pods:
Output:
Two pods are present: one for the web application and another for MySQL. To inspect services, run:
Output:
This verifies the presence of two services: one managing the web application and another for the SQL database (named “sql01”). The default token secret, which was reviewed earlier, is not used by the application. Instead, the web application needs a dedicated secret to connect to the MySQL database.

Application Error and Creating the DB Secret

Upon examining the application, you might notice an error indicating a failure to connect to MySQL. This happens because the necessary environment variables (database host, user, and password) are not set. The error message typically includes:
  • Database host is not set.
  • DB user is not set.
  • DB password is not set.
To fix the issue, create a new Kubernetes Secret named db-secret containing the required credentials. Execute the following command:
Confirm the secret was created by running:
Output:
You can further inspect the secret with:
and you’ll see three keys: DB_Host, DB_User, and DB_Password.

Configuring the Web Application Pod to Use the Secret

Now, update your web application Pod so that it sources its environment variables from the newly created db-secret. This configuration allows the container to directly access the MySQL connection information via environment variables. Below is a sample pod specification illustrating how to include the secret using the envFrom field:
Apply the updated configuration with:
Allow the Pod to restart and then verify that it has been updated successfully:
Within the container section, you should see a reference confirming that environment variables are loaded from db-secret.
The image shows a Kubernetes task to configure a pod named "webapp-pod" to load environment variables from a secret, with a terminal on the right.
Following these changes, the application should successfully establish a connection with the MySQL database using the correct host, user, and password settings.
The image shows a green success message indicating a successful MySQL database connection, displaying environment variables including host, user, and password.

Conclusion

After updating the pod configuration, the web application now correctly reads the required environment variables from db-secret and connects to the MySQL database. This lab exercise emphasizes how Kubernetes Secrets can be used to securely externalize sensitive data—like database credentials—and integrate them seamlessly into application Pods. This completes the lab exercise.

Watch Video