Managing Secrets using kubectl

Creating Secret objects using kubectl command line.

This page shows you how to create, edit, manage, and delete Kubernetes Secrets using the kubectl command-line tool.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

Create a Secret

A Secret object stores sensitive data such as credentials used by Pods to access services. For example, you might need a Secret to store the username and password needed to access a database.

You can create the Secret by passing the raw data in the command, or by storing the credentials in files that you pass in the command. The following commands create a Secret that stores the username admin and the password S!B\*d$zDsb=.

Use raw data

Run the following command:

  1. kubectl create secret generic db-user-pass \
  2. --from-literal=username=admin \
  3. --from-literal=password='S!B\*d$zDsb='

You must use single quotes '' to escape special characters such as $, \, *, =, and ! in your strings. If you don’t, your shell will interpret these characters.

Use source files

  1. Store the credentials in files with the values encoded in base64:

    1. echo -n 'admin' | base64 > ./username.txt
    2. echo -n 'S!B\*d$zDsb=' | base64 > ./password.txt

    The -n flag ensures that the generated files do not have an extra newline character at the end of the text. This is important because when kubectl reads a file and encodes the content into a base64 string, the extra newline character gets encoded too. You do not need to escape special characters in strings that you include in a file.

  2. Pass the file paths in the kubectl command:

    1. kubectl create secret generic db-user-pass \
    2. --from-file=./username.txt \
    3. --from-file=./password.txt

    The default key name is the file name. You can optionally set the key name using --from-file=[key=]source. For example:

    1. kubectl create secret generic db-user-pass \
    2. --from-file=username=./username.txt \
    3. --from-file=password=./password.txt

With either method, the output is similar to:

  1. secret/db-user-pass created

Verify the Secret

Check that the Secret was created:

  1. kubectl get secrets

The output is similar to:

  1. NAME TYPE DATA AGE
  2. db-user-pass Opaque 2 51s

View the details of the Secret:

  1. kubectl describe secret db-user-pass

The output is similar to:

  1. Name: db-user-pass
  2. Namespace: default
  3. Labels: <none>
  4. Annotations: <none>
  5. Type: Opaque
  6. Data
  7. ====
  8. password: 12 bytes
  9. username: 5 bytes

The commands kubectl get and kubectl describe avoid showing the contents of a Secret by default. This is to protect the Secret from being exposed accidentally, or from being stored in a terminal log.

Decode the Secret

  1. View the contents of the Secret you created:

    1. kubectl get secret db-user-pass -o jsonpath='{.data}'

    The output is similar to:

    1. {"password":"UyFCXCpkJHpEc2I9","username":"YWRtaW4="}
  2. Decode the password data:

    1. echo 'UyFCXCpkJHpEc2I9' | base64 --decode

    The output is similar to:

    1. S!B\*d$zDsb=

    Caution: This is an example for documentation purposes. In practice, this method could cause the command with the encoded data to be stored in your shell history. Anyone with access to your computer could find the command and decode the secret. A better approach is to combine the view and decode commands.

    1. kubectl get secret db-user-pass -o jsonpath='{.data.password}' | base64 --decode

Edit a Secret

You can edit an existing Secret object unless it is immutable. To edit a Secret, run the following command:

  1. kubectl edit secrets <secret-name>

This opens your default editor and allows you to update the base64 encoded Secret values in the data field, such as in the following example:

  1. # Please edit the object below. Lines beginning with a '#' will be ignored,
  2. # and an empty file will abort the edit. If an error occurs while saving this file, it will be
  3. # reopened with the relevant failures.
  4. #
  5. apiVersion: v1
  6. data:
  7. password: UyFCXCpkJHpEc2I9
  8. username: YWRtaW4=
  9. kind: Secret
  10. metadata:
  11. creationTimestamp: "2022-06-28T17:44:13Z"
  12. name: db-user-pass
  13. namespace: default
  14. resourceVersion: "12708504"
  15. uid: 91becd59-78fa-4c85-823f-6d44436242ac
  16. type: Opaque

Clean up

To delete a Secret, run the following command:

  1. kubectl delete secret db-user-pass

What’s next