This guide covers managing Kubernetes Secrets, including inspecting default secrets and creating a custom secret for a MySQL database connection.
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.
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:
Copy
Ask AI
kubectl get deploy
Output:
Copy
Ask AI
No resources found in default namespace.
Next, verify the pods:
Copy
Ask AI
kubectl get pods
Output:
Copy
Ask AI
NAME READY STATUS RESTARTS AGEwebapp-pod 1/1 Running 0 26smysql 1/1 Running 0 26s
Two pods are present: one for the web application and another for MySQL. To inspect services, run:
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.
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:
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:
Allow the Pod to restart and then verify that it has been updated successfully:
Copy
Ask AI
kubectl describe pod webapp-pod
Within the container section, you should see a reference confirming that environment variables are loaded from db-secret.
Following these changes, the application should successfully establish a connection with the MySQL database using the correct host, user, and password settings.
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.