How to Create a Secret in Kubernetes

0 min read 140 words

Secrets allow applications to not hardcode usernames, passwords or additional information such as hostnames, IP addresses or other protected/sensitive information.

Create a Secret in Kubernetes

It’s simple to create a secret in Kubernetes . It can either be done --from-file or --from-literal.

Option 1: From a File

echo -n 'adminuser' > ./username.txt
echo -n 'jf392hf782hf932h' > ./password.txt

kubectl create secret generic admin-user-pass \
  --from-file=./username.txt \
  --from-file=./password.txt

The result will be something like:

secret/admin-user-pass created

By default, the key name is the filename, but we could also be explicit about this:

kubectl create secret generic admin-user-pass \
  --from-file=username=./username.txt \
  --from-file=password=./password.txt

Option 2: From a Literal

We don’t have to create a file to create a secret, we can also create one on the fly:

kubectl create secret generic admin-user-pass \
  --from-literal=username=adminuser \
  --from-literal=password='jf392hf782hf932h'

The result will be something like:

secret/admin-user-pass created
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags