Learn how to seamlessly update Kubernetes secrets generated from files, ensuring your applications always access the latest sensitive information securely.
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.
Kubernetes Secrets store sensitive data like passwords and API keys. Here's how to manage them:
1. Create a Secret:
kubectl create secret generic my-secret --from-file=my.key
kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret
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"
kubectl apply -f secret.yaml
2. View a Secret:
kubectl get secret my-secret -o yaml
3. Update a Secret:
secret.yaml
file and re-apply with kubectl apply -f secret.yaml
.kubectl patch
:
kubectl patch secret my-secret --patch '{"data":{"password":"bmV3cGFzc3dvcmQ="}}'
bmV3cGFzc3dvcmQ=
with the base64 encoded new password)4. Delete a Secret:
kubectl delete secret my-secret
5. Use a Secret in a Pod:
volumeMounts:
- name: my-secret-volume
mountPath: /etc/my-secret
volumes:
- name: my-secret-volume
secret:
secretName: my-secret
env:
- name: USERNAME
valueFrom:
secretKeyRef:
name: my-secret
key: username
Important:
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:
Modify the secret.yaml
file (e.g., change the password).
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:
base64
.This provides practical examples for managing Kubernetes Secrets. Remember to adapt these examples to your specific needs and security requirements.
Security Best Practices:
Advanced Usage:
docker-registry
for storing container registry credentials.Troubleshooting:
kubectl get secret <secret-name> -o yaml | grep <key>
command to retrieve and decode a specific secret value.Alternatives to Kubernetes Secrets:
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.
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 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.