Kubernetes
When you wnat to store some sensitive data in your Kubernetes cluster, you will need the "Secret" to help you. Secret usually store some sensitive data such as account of database, Access Token or SSH Key etc.
Kubernetes Secrets let you store and manage sensitive information, such as passwords, OAuth tokens, and ssh keys. Storing confidential information in a Secret is safer and more flexible than putting it verbatim in a Pod definition or in a container image.
We have three ways to import the sesitive data to Secert.
We can save our data in the txt file. Then, we can use
kubrctl create
to create the Secret.
We can save Account and Password to username.txt and password.txt.
Then, we can use
kubectl create secret generic demo-seccret-file
to create Secret.
Message
Use
kubectl describe
to seedemo-seccret-file
.
We can also use
kubectl get Secret
to see the Secret.
We can use
kubectl create
and with--from-literal
to create Secret.
Use
kubectl describe secret demo-secret-literal
to see the Secret.
When we use the Yaml to create Secret, we need to use base64 to encode password.
Write the yaml file.
Use
kubectl create
to create Secret.
To consume a Secret in a volume, we need to add the volume in yaml file.
In this yaml file. We can set the Secret permission. Then, the secret will be mounted on /etc/creds and all the files created by the secret volume mount will have permission 0400.
Or you can also use mapping, as in the previous example, and specify different permissions for different files.
In this case, the file resulting in
/etc/creds/my-group/my-username
will have permission value of 0777. If you use JSON, owing to JSON limitations, you must specify the mode in decimal notation, 511.
Then use
kubectl apply -f ./demo-pod
to create a Pod.
And we can use
kubectl exec
into the container to find our data in/etc/creds
.
kubectl exec -it demo-pod-mounting-secret -- bash
We can find our data in Pod.
Secrets can be exposed as environment variables to be used by a container in a Pod.
Example yaml file
Use
kubectl apply -f ./ev-pod.yml
to create Pod.
We can see the environment varibles in the container. Then, use
kubectl exec
enter the contaioner.
kubectl exec -it ev-pod -- bash