Kubernetes Secret Data Vs StringData: A Deep Dive

Kubernetes Secret Data Vs StringData

In Kubernetes secrets management, two key concepts often come up: data and stringData. These are both fields used in Kubernetes Secret objects, but they serve different purposes and have distinct characteristics.

In this comprehensive guide, we will explore Kubernetes Secret Data vs StringData, comparing their features, use cases, and implementation details.

Let's look at each field and compare them.

Data Field

The data field in a Kubernetes Secret is used to store secret information in base64-encoded format.

Here are its key characteristics:

  • It's a map of key-value pairs.
  • Values must be base64-encoded strings.
  • It's the traditional way of specifying secret data in Kubernetes.
  • The base64 encoding provides a level of encoding, but it's not encryption.
  • When the secret is mounted in a pod, the values are automatically decoded.

Example of data in a Secret YAML:

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: YWRtaW4=  # base64 encoded "admin"
  password: MWYyZDFlMmU2N2Rm  # base64 encoded "1f2d1e2e67df"

You can see the values are given as base64-encoded strings. If you want to create base64-encoded strings, you can use the following command.

echo -n 'admin' | base64

StringData Field

The stringData field is an alternative way to specify secret data in Kubernetes. Its key features are:

  • It's also a map of key-value pairs.
  • Values are specified as plain strings (not base64-encoded).
  • It's a write-only convenience field.
  • When the secret is created, values in stringData are encoded and moved to the data field.
  • It's particularly useful for secrets that are human-readable, like configuration files.

Example of stringData in a Secret YAML:

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
stringData:
  config.yaml: |
    apiUrl: "https://my.api.com/api/v1"
    username: admin
    password: 1f2d1e2e67df

Data Vs StringData

Here's a detailed comparison of data and stringData:

Aspect data stringData
Encoding Requires base64 encoding Uses plain text
Readability Less readable due to encoding More human-readable
Editing Requires decoding/encoding for editing Can be edited directly
Storage Stored as-is in etcd Encoded and stored in the data field
API Visibility Visible when retrieving via API Not visible in API responses (converted)
Use in Pod Both decoded when mounted in a pod Both decoded when mounted in a pod
Overwriting stringData value takes precedence if both exist stringData value takes precedence if both exist

Use Cases for Data

The data field is particularly useful in the following scenarios:

  1. When working with existing base64-encoded secrets
  2. For programmatic secret creation where encoding is handled by the client
  3. When dealing with binary data that's not easily represented as plain text
  4. In situations where the slight obfuscation provided by base64 encoding is desired

Use Cases for StringData

The stringData field shines in these situations:

  1. When creating secrets directly in YAML files
  2. For storing configuration files as secrets
  3. When working with human-readable secrets that don't require pre-encoding
  4. In CI/CD pipelines where secrets are injected as plain text

Now, let's create a secret with each field and see the difference.

Example of Creating and Managing a Secret with Data

Here's an example of creating and managing a Secret using the data field.

Step 1: First, create a Secret with data field.

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
  name: mysecret-data
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm
EOF

Then, verify that the secret was created.

$ kubectl get secret

NAME            TYPE     DATA   AGE
mysecret-data   Opaque   2      8s

Step 2: Next, check the secret created.

kubectl get secret mysecret-data -o yaml

You will get the YAML output of the secret you created.

Step 3: Now, create a Pod and make the pod use the secret we created in the previous step.

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: secret-data-test
spec:
  containers:
  - name: test-container
    image: busybox
    command: ["sleep", "infinity"]
    volumeMounts:
    - name: secret-volume
      mountPath: /etc/secret
  volumes:
  - name: secret-volume
    secret:
      secretName: mysecret-data
EOF

This manifest creates a busybox Pod that runs continuously with the sleep infinity command. The secret is mounted as a volume in pods /etc/secret directory as a file.

Then, verify if the pod starts running.

$ kubectl get po

NAME               READY   STATUS    RESTARTS   AGE
secret-data-test   1/1     Running   0          5s

Step 4: Once the Pod is up and running, use the following commands to check if the secrets are injected into the pod.

kubectl exec secret-data-test -- cat /etc/secret/username
kubectl exec secret-data-test -- cat /etc/secret/password

These commands will list the username and password as shown below.

admin
1f2d1e2e67df

Now, we have looked at a Secret with the Data field. Next, let's look at the Secret with the StringData field.

Example of Creating and Managing a Secret with StringData

We are going to do the exact steps we did in the previous section. The only difference will be how we provide the secret.

Here's an example of creating and managing a Secret using the stringData field.

Step 1: Create a Secret with StringData.

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
  name: mysecret-stringdata
type: Opaque
stringData:
  config.yaml: |
    apiUrl: "https://my.api.com/api/v1"
    username: admin
    password: 1f2d1e2e67df
EOF

Here, you can see we provided the secret as plain text instead of encoding it. Then verify that the secret was created.

$ kubectl get secret

NAME                  TYPE     DATA   AGE
mysecret-stringdata   Opaque   1      74s

Step 2: Next, check the secret created.

kubectl get secret mysecret-stringdata -o yaml

You will get the YAML output of the secret you created.

Step 3: Now, create a Pod and make it use the secret you created in the previous step.

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: secret-stringdata-test
spec:
  containers:
  - name: test-container
    image: busybox
    command: ["sleep", "infinity"]
    volumeMounts:
    - name: secret-volume
      mountPath: /etc/secret
  volumes:
  - name: secret-volume
    secret:
      secretName: mysecret-stringdata
EOF

Same as the previous manifest, this also creates a busybox Pod that runs continuously with the sleep infinity command. The secret is mounted as a volume in pods /etc/secret directory as a file.

Then, verify if the pod starts running.

$ kubectl get po

NAME                     READY   STATUS    RESTARTS   AGE
secret-stringdata-test   1/1     Running   0          6s

Step 4: Once the Pod starts running, use the following command to check if the secrets are injected into the pod.

kubectl exec secret-stringdata-test -- cat /etc/secret/config.yaml

The command will list the apiUrl, username and password injected into the pod from the secret.

apiUrl: "https://my.api.com/api/v1"
username: admin
password: 1f2d1e2e67df

Key Points Summary

Below are the key differences between each field.

  • data and stringData are two ways to specify secret data in Kubernetes.
  • data requires base64 encoding, while stringData accepts plain text.
  • stringData is converted to data when the secret is created.
  • Both are decoded when mounted in a pod.
  • data is useful for pre-encoded or binary data, while stringData is convenient for human-readable secrets.
  • DaemonSets are a different concept, used for running pods on all or some nodes in a cluster.
  • Choose between data and stringData based on your specific use case and workflow.

Conclusion

The choice between Kubernetes Secret Data and StringData often comes down to specific use cases and team workflows.

In my experience, I've found that while Data offers more flexibility with binary content, StringData provides a more user-friendly approach for day-to-day secret management.

I hope this guide helped you understand the key differences between Data and StringData fields.

About the author
Bibin Wilson

Bibin Wilson

Bibin Wilson (authored over 300 tech tutorials) is a cloud and DevOps consultant with over 12+ years of IT experience. He has extensive hands-on experience with public cloud platforms and Kubernetes.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to DevOpsCube – Easy DevOps, SRE Guides & Reviews.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.

📩 Join 20K+ Engineers