Skip to main content

Secret in Kubernetes

Secret in Kubernetes


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.

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 generic
ie: 
> 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
   
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

Popular posts from this blog

Changing the FQDN of the vCenter appliance (VCSA)

This article states how to change the system name or the FQDN of the vCenter appliance 6.x You may not find any way to change the FQDN from the vCenter GUI either from VAMI page of from webclient as the option to change the hostname always be greyed out. Now the option left is from the command line of VCSA appliance. Below steps will make it possible to change the FQDN of the VCSA from the command line. Access the VCSA from console or from Putty session. Login with root permission Use above command in the command prompt of VCSA : /opt/vmware/share/vami/vami_config_net Opt for option 3 (Hostname) Change the hostname to new name Reboot the VCSA appliance.   After reboot you will be successfully manage to change the FQDN of the VCSA . Note: Above step is unsupported by VMware and may impact your SSL certificate and face problem while logging to vSphere Web Client. If you are using self-signed certificate, you can regenerate the certificate with the

Issue : Configure Management Network option is Grayed out into ESXi

Last week I got into an issue of one of my client into Vsphere environment where one of its ESXi went done out of the network. Issue was IP address was showing 0.0.0.0 on main Esxi screen and when I tried to change the network configuration, its " Configure Management network option was greyed out.  I tried to gid into it and try to analyis its vmKernal and vmwarning logs. What I found is its VMkernal switch got removed due to unexpected reason. So to resolve the issue I tried to reconfigure its vswitch0 (vmk0) by going into Tech Mode of that Exi. Below are the steps which I followed to resolve the issue. 1) Login to ESXi 2) Press F2, Check if you " Configure Management network " is greyed out or not" if yes,    follow below 3) Press ALT+F1 to move the ESXi screen to tech mode   ( This is command line like dos) 4) login with root account 5) Run the following command into it esxcli network ip interface add --interface-name= vmk0

Collecting Logs from NSX-T Edge nodes using CLI

  This article explains how to extract the logs from NSX-T Edge nodes from CLI. Let's view the steps involved: 1) Login to NSX-T  Edge node using CLI from admin credentials. 2) Use of  " get support-bundle " for Log extraction. get support-bundle command will extract the complete logs from NSX-T manager/Edge nodes. nsx-manager-1> get support-bundle file support-bundle.tgz 3) Last step is to us e of " copy file support-bundle.tgz url " command. copy file will forward your collected logs from the NSX-T manager to the destination(URL) host from where you can download the logs. copy file support.bundle.tgz url scp://root@192.168.11.15/tmp Here, the URL specified is the ESXi host ( 192.168.11.15) under /tmp partition where logs will be copied and from there one can extract it for further log review. Happy Learning.  :)