đŸ¶
Kubernetes

kubectl Modify Secret: A Comprehensive Guide

By Jan on 02/09/2025

Learn how to easily modify Kubernetes secrets using kubectl commands for secure configuration management.

kubectl Modify Secret: A Comprehensive Guide

Table of Contents

Introduction

Kubernetes Secrets are the standard way to store and manage sensitive information like passwords, API keys, and tokens within your Kubernetes cluster. While Kubernetes stores this data in a base64 encoded format, it's crucial to remember that this is not true encryption. This article provides a practical guide to managing Kubernetes Secrets, covering creation, viewing, updating, and deletion. We'll also touch upon important security considerations and alternative solutions for enhanced protection of your sensitive data.

Step-by-Step Guide

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

Creating Secrets:

  • From a file:
    kubectl create secret generic my-secret --from-file=./my.cnf 
  • From literal values:
    kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret

Viewing Secrets:

  • List all secrets:
    kubectl get secrets
  • View details of a secret:
    kubectl describe secret my-secret
  • Get data in a specific format (e.g., JSON):
    kubectl get secret my-secret -o jsonpath='{.data.username}' | base64 --decode

Updating Secrets:

  • Directly editing is not recommended. Instead, use these methods:
    • Using kubectl patch:
      kubectl patch secret my-secret -p '{"data":{"password":"$(echo -n 'newpassword' | base64)"}}'
    • Using kubectl apply with a modified YAML file:
      1. Get the secret YAML: kubectl get secret my-secret -o yaml > my-secret.yaml
      2. Edit the data section in the YAML, encoding values with base64.
      3. Apply the changes: kubectl apply -f my-secret.yaml
    • Using the kubectl-modify-secret plugin:
      1. Install the plugin: kubectl krew install modify-secret
      2. Update the secret: kubectl modify-secret my-secret -k password -v newpassword

Deleting Secrets:

kubectl delete secret my-secret

Important Notes:

  • Secret data is stored base64 encoded. Decode it when retrieving values.
  • Consider using tools like sops for encrypting secrets at rest.
  • Avoid storing highly sensitive data in plain secrets. Explore solutions like HashiCorp Vault for enhanced security.

Code Example

This code provides examples of how to manage Kubernetes Secrets using kubectl. It demonstrates creating secrets from files and literal values, viewing secret details, updating secret data using various methods, and deleting secrets. The examples cover using commands like kubectl create secret, kubectl get secret, kubectl describe secret, kubectl patch, kubectl apply, and kubectl delete. Additionally, it shows how to extract specific data from secrets using jsonpath and base64 decoding. The code emphasizes the importance of base64 encoding secret values and suggests using tools like sops for encryption and HashiCorp Vault for enhanced security.

This document provides code examples for managing Kubernetes Secrets, covering creation, viewing, updating, and deletion.

Prerequisites:

  • A running Kubernetes cluster
  • kubectl configured to interact with your cluster

1. Creating Secrets

a) From a file:

# Create a secret named "db-credentials" from a file named "db.cnf"
kubectl create secret generic db-credentials --from-file=./db.cnf

b) From literal values:

# Create a secret named "api-credentials" with username and password
kubectl create secret generic api-credentials \
  --from-literal=username=apiuser \
  --from-literal=password=apisecret

2. Viewing Secrets

a) List all secrets:

kubectl get secrets

b) View details of a secret:

kubectl describe secret api-credentials

c) Get data in JSON format:

# Get the "username" value from the "api-credentials" secret
kubectl get secret api-credentials -o jsonpath='{.data.username}' | base64 --decode

3. Updating Secrets

a) Using kubectl patch:

# Update the "password" value in the "api-credentials" secret
kubectl patch secret api-credentials -p '{"data":{"password":"$(echo -n 'newapisecret' | base64)"}}'

b) Using kubectl apply with a modified YAML file:

# 1. Get the secret YAML:
kubectl get secret api-credentials -o yaml > api-credentials.yaml

# 2. Edit the "data" section in the YAML, encoding values with base64.
#    For example, change the password to "updatedpassword":
#    data:
#      password: $(echo -n 'updatedpassword' | base64)

# 3. Apply the changes:
kubectl apply -f api-credentials.yaml

c) Using the kubectl-modify-secret plugin:

# 1. Install the plugin (if not already installed):
kubectl krew install modify-secret

# 2. Update the "password" value in the "api-credentials" secret
kubectl modify-secret api-credentials -k password -v newapisecret

4. Deleting Secrets

# Delete the "api-credentials" secret
kubectl delete secret api-credentials

Important Notes:

  • Remember to replace placeholder values like my-secret, username, password, etc., with your actual secret names and data.
  • Always base64 encode your secret values before storing them in Kubernetes Secrets.
  • Consider using tools like sops for encrypting secrets at rest and explore solutions like HashiCorp Vault for enhanced security.

Additional Notes

Security:

  • Base64 encoding is NOT encryption: While Kubernetes encodes secret data with base64, this is easily reversible and doesn't provide real security. Treat base64 encoded secrets as plain text.
  • Secrets are stored in etcd: By default, secrets are stored in the Kubernetes control plane's etcd database. Ensure your etcd is properly secured to protect your secrets.
  • Limit access to secrets: Use Role-Based Access Control (RBAC) to restrict which users and applications can access specific secrets.
  • Rotate secrets regularly: Regularly rotating your secrets reduces the impact of a potential compromise.

Best Practices:

  • Use descriptive names: Choose names that clearly indicate the purpose of the secret.
  • Version your secrets: Consider using a versioning scheme in your secret names (e.g., my-secret-v1) to manage updates effectively.
  • Use environment variables: Inject secrets into your applications as environment variables instead of hardcoding them.
  • Automate secret management: Explore tools and practices for automating secret creation, rotation, and revocation.

Alternatives for Enhanced Security:

  • HashiCorp Vault: Provides robust secret management with features like encryption at rest, dynamic secrets, and auditing.
  • AWS Secrets Manager, Azure Key Vault, Google Cloud Secret Manager: Cloud-specific solutions for managing secrets securely.
  • Sealed Secrets: Encrypts secrets using public-key cryptography, allowing only authorized pods to decrypt them.

Troubleshooting:

  • "Error: secrets is forbidden": This error indicates insufficient permissions. Review your RBAC configuration.
  • Secret data is not updated: Ensure you are encoding the new secret value with base64 before updating.

Additional Resources:

This information provides a more comprehensive understanding of Kubernetes Secrets, covering security considerations, best practices, alternative solutions, troubleshooting tips, and additional resources.

Summary

Action Description Command Example
Create from file Create a secret from a file containing sensitive data. kubectl create secret generic my-secret --from-file=./my.cnf
Create from literal values Create a secret by specifying key-value pairs directly. kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret
List all secrets Display a list of all secrets in the current namespace. kubectl get secrets
View secret details Show detailed information about a specific secret. kubectl describe secret my-secret
Get specific data Retrieve a specific value from a secret in a desired format. kubectl get secret my-secret -o jsonpath='{.data.username}' | base64 --decode
Update secret (patch) Modify a secret's data using a patch operation. kubectl patch secret my-secret -p '{"data":{"password":"$(echo -n 'newpassword' | base64)"}}'
Update secret (apply) Update a secret by modifying its YAML definition. 1. kubectl get secret my-secret -o yaml > my-secret.yaml
2. Edit data section, base64 encode values.
3. kubectl apply -f my-secret.yaml
Update secret (kubectl-modify-secret plugin) Use a plugin for easier secret modification. 1. kubectl krew install modify-secret
2. kubectl modify-secret my-secret -k password -v newpassword
Delete a secret Remove a secret from the cluster. kubectl delete secret my-secret

Key Points:

  • Secret data is base64 encoded for storage.
  • Use tools like sops for encrypting secrets at rest.
  • For highly sensitive data, consider solutions like HashiCorp Vault.

Conclusion

Kubernetes Secrets provide a built-in mechanism for handling sensitive data within your cluster, offering a standardized approach over hardcoding credentials. However, it's essential to recognize that base64 encoding is not true encryption. While Kubernetes Secrets are a good starting point, consider implementing additional security measures like encryption at rest using tools like sops and explore robust solutions like HashiCorp Vault for managing highly sensitive information. By combining Kubernetes Secrets with these best practices, you can enhance the security of your applications and protect your sensitive data more effectively.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
đŸ€źClickbait