Exercise
- Create a file private.txt with the content
token=cb3456a54EB5
-
Create a Secret named credentials, of type generic, from the file private.txt. Make sure the key under the data property of the secret is creds
-
Create a Pod named test, with a single container based on the alpine image and running the “sleep 3600” command. Make sure this Pod has access to the content of the Secret in the /secrets/credentials folder in the container’s filesystem
-
Run a shell in the Pod’s container and verify the content of the /secrets/credentials/creds
-
Delete the Pod and the Secret
Documentation
https://kubernetes.io/docs/concepts/configuration/secret/
Solution
- Create the file private.txt
cat >> private.txt << EOF
token=cb3456a54EB5
EOF
- Create a Secret named credentials from this file
k create secret generic credentials --from-file=creds=./private.txt
The data property contains the creds key:
k get secret credentials -o yaml
apiVersion: v1
data:
creds: dG9rZW49Y2IzNDU2YTU0RUI1Cg==
kind: Secret
metadata:
name: credentials
type: Opaque
- Create a Pod named test, with a single container based on the alpine image and running the “sleep 10000” command. Make sure this Pod has access to the content of the Secret in the /secrets/credentials folder in the container’s filesystem
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- image: alpine
name: alpine
command:
- "sleep"
- "3600"
volumeMounts:
- name: creds
mountPath: /secrets/credentials
volumes:
- name: creds
secret:
secretName: credentials
- Run a shell in the Pod’s container and verify the content of the /secrets/credentails/creds
k exec test -- cat /secrets/credentials/creds
token=cb3456a54EB5
- Delete the Pod and the Secret
k delete po/test secret/credentials