• Home
  • Docker
  • Kubernetes
  • Java
  • Ubuntu
  • Maven
  • Big Data
  • CI
  • Install
  • Samples
  • Archived
Kubernetes | Kubernetes Secrets (secrets)
  1. Notes
  2. Kubernetes Secrets (secrets)
  3. Creating Secrets using kubectl
    --from-literal
    --from-file: from file without a key, from file with a key, from a directory
    --from-env-file
  4. Creating Secrets using manifest YAML file
  5. Edit Secrets
  6. Using Secrets: Environment variables
    env/valueFrom/secretKeyRef
    envFrom/secretRef
  7. Using Secrets: Files in Volumes
  8. Referencing Secrets's environment variables in the container's command
  9. Delete Secrets

  1. Notes
    Please visit these pages for more details about Secrets:
    https://kubernetes.io/docs/concepts/configuration/secret/
    https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/
    https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-kubectl/
  2. Kubernetes Secrets (secrets)
    Secrets allows creating data objects that can be consumed by Pods at runtime.

    Here's an example of a Secret (YAML):
    The manifest file of the Secret is pretty simple. It contains the fields apiVersion, kind, and metadata. It also contain the data field that contains the keys-values of the Secret.

    The data in Secrets is stored as key-value entries:
    • Key: is a string value formed by alphanumeric, dot (.), dash (-), and underscore (_) characters.

    • Value: is a base64-encoded string value.

    The key and the value, in the Secrets, are separated with a colon (key:value).

    The name of the Secrets must be a valid DNS subdomain name.

    The data stored in a Secrets can be consumed in a container in one the following ways:
    • Environment variables.

    • Command-line arguments of the container command.

    • Files in volumes.

    • Custom code (read the Secrets directly from the Kubernetes API).

    To encode a text into a base64 string:

    The -n flag ensures that the generated output doesn't have an extra newline character at the end of the text. This is to avoid that the extra newline character gets encoded along with the text.

    To decode a base64 data:

    Note: If you update a Secret that was already posted to the Kubernetes API server, then Pods that were already created will be able to leverage the new data only if it was injected as files in a volume. Secrets data injected as environment variables won't be updated in a running Pod unless you recreate it.
  3. Creating Secrets using kubectl
    The data of the Secrets can be specified using one of the following methods:

    • generic --from-literal:
      This command allows you to specify a key and literal value to insert in the Secret.

      You need to escape the special characters $, \, *, =, and ! (otherwise they will be interpreted by the shell).

      One way to escape the special characters is to enclose the value within single quotes (').

      Create the Secrets:

      Check the Secret:

      Describe the Secret:

      View the YAML file of the Secret:

      To decode the Secret of a specific key, you need to decode its base64 data:


    • generic --from-file:
      This command allows you to specify a file path (--from-file="./secrets/file1.txt"), in which case the file base name will be used as the Secret key.

      Optionally you can use a key with the file path (--from-file="mykey1=./secrets/file1.txt"), in which case the given key will be used as the Secret key.

      If you specify a directory (--from-file="./secrets/") then kubectl will iterate each file in the directory and create a new entry for each file whose base name is a valid Secret key.

      The path to the file (or directory) can be an absolute path or relative to the location from where the "kubectl create secret" is executed.

      The content of the file can be anything. You don't have to escape special characters in the text that you put in the file.

      Let's crate a directory to hold the Secrets data (this can be any location you want):

      Create a sample file "file1.txt" (single line text):

      Create a sample file "file2.txt" (multiple lines text):

      • Create the Secret from a file without a key:

        View the Secrets (notice that the content of the file is indented properly):

      • Create the Secret from a file with a key:

        View the Secrets (notice that the content of the file is indented properly):

      • Create the Secret from a directory:

        View the Secrets (notice that the content of the file is indented properly):

    • --from-env-file:
      This command allows you to specify the path to an environement (properties) file (--from-file="./secrets/file1.properties") to read lines of key=value pairs to insert in the Secret.

      Create a sample property file:

      Create the Secret:

      View the Secret (notice that each entry in the property file is represented by a key:value pairs in the Secret):
  4. Creating Secrets using manifest YAML file
    The manifest YAML file defines Secrets with key:value entries.

    The values must be base64 encoded strings.


    To apply the file:
  5. Edit Secrets
    If you have created a Secret using the manifest YAML file, then to change that Secret you can adjust the YAML file and run the command kubectl apply -f SECRET-YAML-FILE-PATH

    You can also use the command kubectl edit secret SECRET-NAME to edit a Secret. The command will open the Secret in your default text editor. Make your changes then save and exit the editor. Your changes will be submitted to API Server.

    For example to view and edit the Secret "secretkeyvalue":
  6. Using Secrets: Environment variables
    In a Pod YAML file, You can reference the entries of the Secrets directly in the "spec.containers.env" field. The environment variables will be set like any other standard Linux environment variables. You can check them using the env command directly from inside the container.

    • env/valueFrom/secretKeyRef

      Let's use the Secret "secretkeyvalue" (see above) with the Pod "hello-busybox-secret-keyref":

      Note: You can give the environment variable a different name than the Secrets key name (i.e. name: mykey1).

      Apply the Pod:

      Check the environment variables:

      Note that the secrets are clear text!

    • envFrom/secretRef

      Instead of referencing individual entries of the Secret, you can use the "spec.containers.envFrom" field to reference all entries of the Secret:

      Apply the Pod:

      Check the environment variables:
  7. Using Secrets: Files in Volumes
    Secrets can also be referenced using volumes.

    Each entry of the Secrets will be referenced as a separate file in the volume.

    The changes to the Secrets will be reflect directly in the volumes.

    The Secrets is defined as a volume (spec.volumes) and mounted to the container (spec.containers.volumeMounts).

    • In the following example, the "spec.volumes" field creates a volume (named secretkeyvalue) from the "secretkeyvalue" Secrets.
      The "spec.containers.volumeMounts" field mounts the volume (secretkeyvalue) into the container (under /tmp/secretkeyvalue).
      The volume will be populated with files where the file name is the key of the Secrets (key1, key2) and the content of the file is the value of the key (value1, value2).


      Apply the Pod:

      Check the files in the volume (notice two files are created: key1, key2):

      Check the content of the files:

    • You can also decide what entries of the Secret can be mounted.

      You can also decide the names of the files in the volume "path".


      Apply the Pod:

      Check the files in the volume (notice the "key1path" file was created):

      Check the content of the files:
  8. Referencing Secrets' environment variables in the container's command
    You can use the data of the Secret to set the arguments of the container command.

    You need to use the following syntax to reference the environment variable: $(ENV_VAR_NAME).

    As previously mentioned, changes to the Secret won't be reflected on the environment variables already set.


    Apply the Pod:

    Check the logs of the Pod:
  9. Delete Secrets
    To delete a Secret: using manifest file (kubectl delete):

    To delete a Secret using its name:
© 2025  mtitek