Secret in Kubernetes
1) Creating secrets
2) Injecting the secrets into POD
Secrets in K8s has two ways to create.
Imperative method & Declarative method.
Imperative method: In this method we can create secrets without using secrets definition file, just by using kubectl command.
ie:
> kubectl create secret generic (secret-name) --from-literal=(key)=(value)
> kubectl create secret generic app-secret --from-literal=DB_Host=mysql
We can specify key value multiple times in secret definition file. like,
> kubectl create secret generic app-secret -from- =DB_USER=Root
> kubectl create secret generic app-secret -from-literal=Password=password
> kubectl create secret generic \ app-secret --from-file=new_secret.properties
Declarative method: Secrets can be created using definition file.
> kubectl create -f xyz.yaml
Layout of xyz.yaml Secret definition file
apiVersion:v1
kind:secret
metadata:
name:new_secret
data:
DB_Host:postgrac
DB_USER: root
DB_Password: password
In pod.definition.yaml file , add environment variable "envFrom" which state as list under spec:
envFrom: Its a environment variable
secretRef: Reference of secret definition file which we are pointing.
key: This describe the content of secret
envFrom:
-secretRef:
name:new-secret
key:DB_Password
Run below command to create the pod using pod-definition.yaml
> kubectl create -f pod-defination.yaml
From any linux server use below echo command to hash or encode the password.
> echo -n 'root' | base64
cm94v9a==
> echo -n 'password' | base64
cGFzd3d33==
data:
DB_Host:postgrac
DB_USER: cm94v9a==
DB_Password: cGFzd3d33==
To view the secret
> Kubectl get secrets
To view the detail of secrets with attributes
> kubectl describe secrets new-secret
To view the complete full function of secrets
> kubectl get secret new-secret -o yaml
To edit the secret
>kubectl edit secret new-secret
Secrets in Kubernetes are sensitive information like Shh keys, tokens, credentials etc. As in general its must require to store such kind of secret object in encrypted way rather than plantext to reduce the risk of exposing such kind of information to unauthorised species.
A secret is not encrypted, only base64-encoded by default. Its require to create an EncryptionConfiguraton with a key and proper identity.
All secret data and configuration are stored onto etcd which is accessible via API server. Secret data on nodes are stored on tmpfs volumes. Individual secret size is limited to 1MB in size. The larger size limit is discouraged as it may exhausted apiserver and kubelet memory.
To use secret its require that pod needs to reference with secret. A secret can be used in 2 ways with pod: as file in a volume mounted on one or more containers, or use by kubelets while pulling images from the pod.
A secret is not encrypted, only base64-encoded by default. Its require to create an EncryptionConfiguraton with a key and proper identity.
All secret data and configuration are stored onto etcd which is accessible via API server. Secret data on nodes are stored on tmpfs volumes. Individual secret size is limited to 1MB in size. The larger size limit is discouraged as it may exhausted apiserver and kubelet memory.
To use secret its require that pod needs to reference with secret. A secret can be used in 2 ways with pod: as file in a volume mounted on one or more containers, or use by kubelets while pulling images from the pod.
There are two steps involved in setting up secret into pod definition yaml file.
1) Creating secrets
2) Injecting the secrets into POD
Secrets in K8s has two ways to create.
Imperative method & Declarative method.
Imperative method: In this method we can create secrets without using secrets definition file, just by using kubectl command.
Step 1: Creating secret
> kubectl create secret genericie:
> kubectl create secret generic (secret-name) --from-literal=(key)=(value)
> kubectl create secret generic app-secret --from-literal=DB_Host=mysql
- generic: Create a secret from a local file, director or literal value.
- docker-registry: Creates a dockercfg Secret for use with a Docker registry. Used to authenticate against Docker registries.
- tls: Create a TLS secret from the given public/private key pair.
from-file
or--from-env-file:
Its a path to a directory containing one or more configuration files.- --from-literal: key-value pairs, each specified using.
We can specify key value multiple times in secret definition file. like,
> kubectl create secret generic app-secret -from- =DB_USER=Root
> kubectl create secret generic app-secret -from-literal=Password=password
Specifying multiple key value pair could be complicated and make the definition file confusing. So, we have other way around where we can specify file name instead of writing multiple key value using --from-file=(path-to-file)
ie: > kubectl create secret generic \ app-secret --from-file=new_secret.properties
Declarative method: Secrets can be created using definition file.
> kubectl create -f xyz.yaml
Layout of xyz.yaml Secret definition file
apiVersion:v1
kind:secret
metadata:
name:new_secret
data:
DB_Host:postgrac
DB_USER: root
DB_Password: password
Step 2: Inject secret into pod
As in Step 1 we have created Secret using Imperative and Declarative method. Now, its time to inject those secret into pod definition file.In pod.definition.yaml file , add environment variable "envFrom" which state as list under spec:
envFrom: Its a environment variable
secretRef: Reference of secret definition file which we are pointing.
key: This describe the content of secret
envFrom:
-secretRef:
name:new-secret
key:DB_Password
Run below command to create the pod using pod-definition.yaml
> kubectl create -f pod-defination.yaml
- Encode a Secret
Now, in this there is a issue. The password which we have specify is readable and its not safe to specify and prone to risk of attack. To take care of it it's require to hash or encoded the password using echo command.
From any linux server use below echo command to hash or encode the password.
> echo -n 'root' | base64
cm94v9a==
> echo -n 'password' | base64
cGFzd3d33==
So, it would be as mentioned below which is now safe to code below encoded username and password in definition file.
data:
DB_Host:postgrac
DB_USER: cm94v9a==
DB_Password: cGFzd3d33==
- Decode a secret
We can also decode the password or key which we have encrypted using below method from any linux server using "--decode"
echo -n 'cm94v9a==' | base64 --decode- Mounting Secret as Volume
Secrets can be mount as file using a volume definition file into pod. The mount path consist of file whose name will be a key of secret created with the kubectl create secret step earlier.
Example of mounting secret as volume
spec:
containers:
- image: nginx
command:
- sleep
- "4000"
volumeMounts:
- mountPath: /nginxpassword
name: vPostgres
volumes:
- name: vPostgress
secret:
secretName: dbase
containers:
- image: nginx
command:
- sleep
- "4000"
volumeMounts:
- mountPath: /nginxpassword
name: vPostgres
volumes:
- name: vPostgress
secret:
secretName: dbase
A secret is only sent to a node if a pod on that node requires it. Kubelet stores the secret into a /tmpfs so that the secret is not written to disk storage. Once the Pod that depends on the secret is deleted, kubelet will also delete its local copy of the secret data as well.
- Commands to keep in mind:
To view the secret> Kubectl get secrets
To view the detail of secrets with attributes
> kubectl describe secrets new-secret
To view the complete full function of secrets
> kubectl get secret new-secret -o yaml
To edit the secret
>kubectl edit secret new-secret
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
Comments
Post a Comment