Secret Configuration
Secret Manifest Structure
Kubernetes secrets are typically defined using YAML manifests with specific configuration parameters:
apiVersion: v1
kind: Secret
metadata:
name: example-secret
type: Opaque
data:
username: YWRtaW4=
password: cGFzc3dvcmQ=
Secret Types
graph TD
A[Secret Types] --> B[Opaque]
A --> C[kubernetes.io/service-account-token]
A --> D[kubernetes.io/dockercfg]
A --> E[kubernetes.io/dockerconfigjson]
A --> F[kubernetes.io/basic-auth]
A --> G[kubernetes.io/ssh-auth]
A --> H[kubernetes.io/tls]
Encoding Secrets
Base64 Encoding
Kubernetes requires secret values to be base64 encoded:
## Encoding
echo -n 'admin' | base64
## Output: YWRtaW4=
## Decoding
echo 'YWRtaW4=' | base64 --decode
## Output: admin
Creating Secrets Manually
1. Generic Secrets
## Create secret from literal values
kubectl create secret generic db-credentials \
--from-literal=username=dbuser \
--from-literal=password=securepass
## Create secret from files
kubectl create secret generic app-config \
--from-file=./config.json
2. Docker Registry Secrets
kubectl create secret docker-registry regcred \
--docker-server=https://index.docker.io/v1/ \
--docker-username=youruser \
--docker-password=yourpassword \
[email protected]
Secret Configuration Options
Configuration |
Description |
Example |
type |
Defines secret type |
Opaque , kubernetes.io/tls |
data |
Base64 encoded key-value pairs |
username: YWRtaW4= |
stringData |
Allows plain text input |
username: admin |
Advanced Secret Management
Immutable Secrets
apiVersion: v1
kind: Secret
metadata:
name: immutable-secret
type: Opaque
immutable: true
data:
key: BASE64_ENCODED_VALUE
LabEx Recommendation
When practicing secret configurations, LabEx provides interactive environments to safely experiment with Kubernetes secret management.
Security Best Practices
- Use
stringData
for easier secret creation
- Implement secret rotation
- Limit secret access using RBAC
- Consider external secret management solutions
Common Pitfalls
- Accidentally committing secrets to version control
- Using weak or predictable secret values
- Insufficient access controls
- Not encrypting secrets at rest
By mastering secret configuration, you can enhance the security and flexibility of your Kubernetes deployments.