Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

We are working on this space so that IT professionals can grow together.

Create account Log in
Jeewan Gautam
Jeewan Gautam

Posted on

Process to create secrets in K8s clusters using literal values.

What is Secret?

Secrets are secure objects which store sensitive data, such as passwords, Tokens and SSH keys in your clusters which helps to reduces the risk of exposing the data to unauthorized users.

Creating Secrets using imperative command.
Syntax:

kubectl create secret <SECRET_TYPE> <SECRET_NAME> <DATA>
Enter fullscreen mode Exit fullscreen mode

- SECRET_TYPE: The Secret type, which can be generic, docker-registry or tls.
- SECRET_NAME: The name of the Secret you are creating
- DATA: The data to add to the Secret, which can be --from-file - to specify a path to a directory containing one or more configuration files or --from-litera - to specify key-value pairs.

1. Crating secrets from literal values
To create a Secret from literal values, use --from-literal

kubectl create secret generic demo-secret1 --from-literal=username=Jeewan --from-literal=Password=Password! -o yaml > demo-secret.yaml
Enter fullscreen mode Exit fullscreen mode

Here, demo-secret.yaml file will be created with secret definition in it. let's have a look on demo-secret.yaml file using below command.

cat demo-secret.yaml
Enter fullscreen mode Exit fullscreen mode

Result:
Image description
we can see, definition for kind: Secret object is populated by default because we store YAMl output in demo-secret.yaml file using -o yaml > demo-secret.yaml command.We have created secret from literal so by default base64 decoded value is populated for key Password and username.

Let's apply demo-secret.yaml file using kubectl command with -f flag to use the file.The following command creates a generic type Secret named demo-secret1 with two key-value pairs which is defined in demo-secret.yaml file.

kubectl apply -f demo-secret.yaml
Enter fullscreen mode Exit fullscreen mode

Verify the secret

kubectl get secret/demo-secret1 -o yaml
Enter fullscreen mode Exit fullscreen mode

or

kubectl get secrets
Enter fullscreen mode Exit fullscreen mode

Results:
Image description
Image description

We have Successfully created Secret using literal. In the next blog, we will discuss how to use this secret in deployment as an environment variables.

Discussion (0)