In this section, we will take a look at secrets in kubernetes
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DB_Host: mysql
DB_User: root
DB_Password: paswrd
Second, Inject the secret into a pod.
$ kubectl create secret generic app-secret --from-literal=DB_Host=mysql --from-literal=DB_User=root --from-literal=DB_Password=paswrd
$ kubectl create secret generic app-secret --from-file=app_secret.properties
Generate a hash value of the password and pass it to secret-data.yaml definition value as a value to DB_Password variable.
$ echo -n "mysql" | base64
$ echo -n "root" | base64
$ echo -n "paswrd"| base64
Create a secret definition file and run kubectl create
to deploy it
apiVersion: v1
kind: Secret
metadata:
name: app-secret
data:
DB_Host: bX1zcWw=
DB_User: cm9vdA==
DB_Password: cGFzd3Jk
$ kubectl create -f secret-data.yaml
$ kubectl get secrets
$ kubectl describe secret
$ kubectl get secret app-secret -o yaml
$ echo -n "bX1zcWw=" | base64 --decode
$ echo -n "cm9vdA==" | base64 --decode
$ echo -n "cGFzd3Jk" | base64 --decode
To inject a secret to a pod add a new property envFrom
followed by secretRef
name and then create the pod-definition
apiVersion: v1
kind: Secret
metadata:
name: app-secret
data:
DB_Host: bX1zcWw=
DB_User: cm9vdA==
DB_Password: cGFzd3Jk
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
envFrom:
- secretRef:
name: app-secret
$ kubectl create -f pod-definition.yaml
Single ENV variable
You can inject as whole secret as files in a Volume
Each attribute in the secret is created as a file with the value of the secret as its content.
Remember that secrets encode data in base64 format. Anyone with the base64 encoded secret can easily decode it. As such the secrets can be considered not very safe.
The concept of safety of the Secrets is a bit confusing in Kubernetes. The kubernetes documentation page and a lot of blogs out there refer to secrets as a “safer option” to store sensitive data. They are safer than storing in plain text as they reduce the risk of accidentally exposing passwords and other sensitive data. In my opinion it’s not the secret itself that is safe, it is the practices around it.
Secrets are not encrypted, so it is not safer in that sense. However, some best practices around using secrets make it safer. As in best practices like:
Also the way kubernetes handles secrets. Such as:
Read about the protections and risks of using secrets here
Having said that, there are other better ways of handling sensitive data like passwords in Kubernetes, such as using tools like Helm Secrets, HashiCorp Vault. I hope to make a lecture on these in the future.