🐶
Kubernetes

Kubernetes Secret Update from File: A Guide

By Jan on 01/21/2025

Learn how to seamlessly update Kubernetes secrets generated from files, ensuring your applications always access the latest sensitive information securely.

Kubernetes Secret Update from File: A Guide

Table of Contents

Introduction

Kubernetes Secrets provide a secure way to manage sensitive information like passwords, API keys, and tokens, keeping them separate from your application code. This guide outlines how to create, view, update, delete, and use Secrets in your Kubernetes deployments.

Step-by-Step Guide

Kubernetes Secrets store sensitive data like passwords and API keys. Here's how to manage them:

1. Create a Secret:

  • From a file:
    kubectl create secret generic my-secret --from-file=my.key 
  • Using literals:
    kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret
  • With YAML:
    apiVersion: v1
    kind: Secret
    metadata:
      name: my-secret
    type: Opaque
    data:
      username: YWRtaW4=  # base64 encoded value of "admin"
      password: c2VjcmV0  # base64 encoded value of "secret"
    Apply with: kubectl apply -f secret.yaml

2. View a Secret:

kubectl get secret my-secret -o yaml

3. Update a Secret:

  • Edit and apply YAML: Modify the secret.yaml file and re-apply with kubectl apply -f secret.yaml.
  • Use kubectl patch:
    kubectl patch secret my-secret --patch '{"data":{"password":"bmV3cGFzc3dvcmQ="}}' 
    (Replace bmV3cGFzc3dvcmQ= with the base64 encoded new password)

4. Delete a Secret:

kubectl delete secret my-secret

5. Use a Secret in a Pod:

  • Mount as volume:
    volumeMounts:
    - name: my-secret-volume
      mountPath: /etc/my-secret
    volumes:
    - name: my-secret-volume
      secret:
        secretName: my-secret
  • As environment variables:
    env:
    - name: USERNAME
      valueFrom:
        secretKeyRef:
          name: my-secret
          key: username

Important:

  • Secret data is base64 encoded.
  • Consider tools like Sealed Secrets for encrypting secrets at rest.
  • Never store secrets in plain text within your application code.

Code Example

This document provides code examples for managing Kubernetes Secrets. It covers creating secrets from files, literals, and YAML, viewing and updating secrets, deleting secrets, and using secrets in pods as volumes and environment variables. It emphasizes encoding secret data with base64, using tools like Sealed Secrets for encryption at rest, and avoiding storing secrets in plain text within application code.

This document provides code examples for managing Kubernetes Secrets, as described in the provided article.

1. Create a Secret:

  • From a file (e.g., my.key):

    kubectl create secret generic my-secret --from-file=my.key
  • Using literals:

    kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret
  • With YAML (file: secret.yaml):

    apiVersion: v1
    kind: Secret
    metadata:
      name: my-secret
    type: Opaque
    data:
      username: YWRtaW4=  # base64 encoded value of "admin"
      password: c2VjcmV0  # base64 encoded value of "secret"

    Apply with:

    kubectl apply -f secret.yaml

2. View a Secret:

kubectl get secret my-secret -o yaml

3. Update a Secret:

  • Edit and apply YAML:

    1. Modify the secret.yaml file (e.g., change the password).

    2. Re-apply with:

      kubectl apply -f secret.yaml
  • Use kubectl patch:

    kubectl patch secret my-secret --patch '{"data":{"password":"bmV3cGFzc3dvcmQ="}}'

    Replace bmV3cGFzc3dvcmQ= with the base64 encoded value of the new password.

4. Delete a Secret:

kubectl delete secret my-secret

5. Use a Secret in a Pod:

  • Mount as a volume (in pod.yaml):

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-pod
    spec:
      containers:
      - name: my-container
        image: my-image
        volumeMounts:
        - name: my-secret-volume
          mountPath: /etc/my-secret
      volumes:
      - name: my-secret-volume
        secret:
          secretName: my-secret
  • As environment variables (in pod.yaml):

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-pod
    spec:
      containers:
      - name: my-container
        image: my-image
        env:
        - name: USERNAME
          valueFrom:
            secretKeyRef:
              name: my-secret
              key: username

Important:

  • Secret data is base64 encoded. You can encode and decode values using online tools or command-line utilities like base64.
  • Consider tools like Sealed Secrets for encrypting secrets at rest.
  • Never store secrets in plain text within your application code.

This provides practical examples for managing Kubernetes Secrets. Remember to adapt these examples to your specific needs and security requirements.

Additional Notes

Security Best Practices:

  • Rotation: Regularly rotate secrets to minimize the impact of a potential compromise.
  • Access Control: Use Role-Based Access Control (RBAC) to restrict which users and applications can access specific secrets.
  • Namespace Isolation: Store secrets in separate namespaces to further limit access and potential damage.
  • Avoid Hardcoding: Never hardcode secrets directly into your application code or configuration files.
  • External Secrets Management: For enhanced security, consider using external secrets management solutions that integrate with Kubernetes.

Advanced Usage:

  • Secret Types: Kubernetes supports different secret types like docker-registry for storing container registry credentials.
  • Image Pull Secrets: Use image pull secrets to authenticate with private container registries when pulling images for your pods.
  • Templating: Use tools like Helm to manage and deploy secrets along with your application configurations.
  • Secret Backups: Implement a backup and recovery strategy for your secrets to prevent data loss.

Troubleshooting:

  • Decoding Secrets: Use the kubectl get secret <secret-name> -o yaml | grep <key> command to retrieve and decode a specific secret value.
  • Pod Errors: Check pod logs and events for errors related to secret access or mounting.
  • Permissions: Ensure that the service account used by your pods has sufficient permissions to access the required secrets.

Alternatives to Kubernetes Secrets:

  • Sealed Secrets: Encrypts secrets at rest using public-key cryptography, allowing you to store them securely in Git repositories.
  • HashiCorp Vault: A dedicated secrets management solution that integrates with Kubernetes and provides advanced features like secret rotation and auditing.

Remember that managing secrets effectively is crucial for the security of your Kubernetes applications. Always follow best practices and choose the right tools and strategies based on your specific needs and security requirements.

Summary

Task Method Command/Code Notes
Create a Secret From a file kubectl create secret generic my-secret --from-file=my.key Creates a secret named "my-secret" with data from "my.key" file.
Using literals kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret Creates a secret with key-value pairs specified directly.
With YAML yaml apiVersion: v1 kind: Secret ... Define the secret in a YAML file and apply with kubectl apply -f secret.yaml. Data values must be base64 encoded.
View a Secret kubectl get secret my-secret -o yaml Displays the secret in YAML format.
Update a Secret Edit and apply YAML Modify the YAML file and re-apply with kubectl apply -f secret.yaml.
Use kubectl patch kubectl patch secret my-secret --patch '{"data":{"password":"bmV3cGFzc3dvcmQ="}}' Updates specific keys within the secret data. New value must be base64 encoded.
Delete a Secret kubectl delete secret my-secret
Use a Secret in a Pod Mount as volume yaml volumeMounts: - name: my-secret-volume ... Makes the secret accessible as files within the pod at the specified mount path.
As environment variables yaml env: - name: USERNAME ... Injects secret values as environment variables within the pod.

Key Points:

  • Kubernetes Secrets store sensitive data in base64 encoded format.
  • Use tools like Sealed Secrets for encrypting secrets at rest.
  • Never hardcode secrets directly into your application code.

Conclusion

Kubernetes Secrets are a fundamental part of securely managing sensitive information in your Kubernetes deployments. By understanding how to create, view, update, delete, and use Secrets effectively, you can ensure your application credentials and sensitive data are stored and accessed securely. Remember to follow security best practices such as secret rotation, access control, and using tools like Sealed Secrets for enhanced security. By incorporating these practices, you can confidently deploy and manage your applications while minimizing the risks associated with sensitive data exposure.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait