62/100 Manage Secrets in Kubernetes
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.
- We already have a secret key file
ecommerce.txtunder/optlocation onjump host. Create ageneric secretnamedecommerce, it should contain the password/license-number present inecommerce.txtfile. - Also create a
podnamedsecret-datacenter. - Configure pod's
specas container name should besecret-container-datacenter, image should bedebianwithlatesttag (remember to mention the tag with image). Usesleepcommand for container so that it remains in running state. Consume the created secret and mount it under/opt/clusterwithin the container. - To verify you can exec into the container
secret-container-datacenter, to check the secret key under the mounted path/opt/cluster. Before hitting theCheckbutton please make sure pod/pods are in running state, also validation can take some time to complete so keep patience.
Create the secret from the file we already have in /opt/ecommerce.txt which is present on the jump host.
thor@jumphost ~$ kubectl create secret generic ecommerce --from-file=/opt/ecommerce.txt
secret/ecommerce createdThis will create a secret where:
Key = ecommerce.txt
Value = the contents of /opt/ecommerce.txt (password/license-number)
This can then be verified:
thor@jumphost ~$ kubectl get secrets
NAME TYPE DATA AGE
ecommerce Opaque 1 50s
thor@jumphost ~$ kubectl describe secret ecommerce
Name: ecommerce
Namespace: default
Labels: <none>
Annotations: <none>
Type: Opaque
Data
====
ecommerce.txt: 7 bytesNext step is then to create the pod yaml file, named secret-datacenter.yaml
apiVersion: v1
kind: Pod
metadata:
name: secret-datacenter
spec:
containers:
- name: secret-container-datacenter
image: debian:latest
command: ["sleep", "infinity"]
volumeMounts:
- name: secret-volume
mountPath: /opt/cluster
volumes:
- name: secret-volume
secret:
secretName: ecommerce
Next, apply the pod configuration and then check the pod status:
thor@jumphost ~$ kubectl apply -f secret-datacenter.yaml
pod/secret-datacenter created
thor@jumphost ~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
secret-datacenter 1/1 Running 0 31sFinally, verify the secret inside the container:
thor@jumphost ~$ kubectl exec -it secret-datacenter -- bash
root@secret-datacenter:/# ls /opt/cluster
ecommerce.txt
root@secret-datacenter:/# cat /opt/cluster/ecommerce.txt
5ecur3