DEV Community

Cover image for 5.Manage Secrets in Kubernetes
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

5.Manage Secrets in Kubernetes

Lab Information

The Nautilus DevOps team is working to deploy some tools in Kubernetes cluster. Some of the tools are licence based so that licence information needs to be stored securely within Kubernetes cluster. Therefore, the team wants to utilize Kubernetes secrets to store those secrets. Below you can find more details about the requirements:

We already have a secret key file official.txt under /opt location on jump host. Create a generic secret named official, it should contain the password/license-number present in official.txt file.

Also create a pod named secret-devops.

Configure pod's spec as container name should be secret-container-devops, image should be fedora with latest tag (remember to mention the tag with image). Use sleep command for container so that it remains in running state. Consume the created secret and mount it under /opt/demo within the container.

To verify you can exec into the container secret-container-devops, to check the secret key under the mounted path /opt/demo. Before hitting the Check button please make sure pod/pods are in running state, also validation can take some time to complete so keep patience.
Enter fullscreen mode Exit fullscreen mode

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.


Lab Solutions

Step 1: Check the Secret Key File

First, let's verify the content of the official.txt file:

cat /opt/official.txt
Enter fullscreen mode Exit fullscreen mode

This will show you the password/license-number that needs to be stored as a secret.

Step 2: Create the Kubernetes Secret

Create a generic secret named official from the file:

kubectl create secret generic official --from-file=official=/opt/official.txt
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the Secret was Created

Check if the secret was created successfully:

kubectl get secrets
Enter fullscreen mode Exit fullscreen mode

To see more details:

kubectl describe secret official
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the Pod Configuration

Create a file named secret-devops.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: secret-devops
spec:
  containers:
  - name: secret-container-devops
    image: fedora:latest
    command: ["/bin/bash", "-c", "sleep infinity"]
    volumeMounts:
    - name: secret-volume
      mountPath: /opt/demo
      readOnly: true
  volumes:
  - name: secret-volume
    secret:
      secretName: official
Enter fullscreen mode Exit fullscreen mode

Step 5: Deploy the Pod

Apply the pod configuration:

kubectl apply -f secret-devops.yaml
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify the Pod is Running

Check the pod status:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify the Secret is Mounted Correctly

# Once the pod is running, execute into the container to verify the secret:
kubectl exec -it secret-devops -- /bin/bash


# Inside the container, check the mounted secret:
ls -la /opt/demo/

# You should see the secret file. Then check its content:
cat /opt/demo/official

# Exit the container:
exit
Enter fullscreen mode Exit fullscreen mode


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)