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>
- 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
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
Result:
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
Verify the secret
kubectl get secret/demo-secret1 -o yaml
or
kubectl get secrets
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.
Top comments (0)