Overview
TheSecret class represents a Kubernetes Secret, which is used to store and manage sensitive information such as passwords, OAuth tokens, SSH keys, and TLS certificates. Secrets are similar to ConfigMaps but are specifically intended for confidential data.
Class Definition
v1
Constructor
Secret accepts all parameters from NamespacedResource, plus:str | None
Secret name
str | None
Namespace where the secret will be created
DynamicClient | None
Dynamic client for cluster connection
str | None
AWS access key ID (convenience parameter for AWS credentials)
str | None
AWS secret key (convenience parameter for AWS credentials)
str | None
Htpasswd data (convenience parameter for basic auth)
dict | None
Dictionary of base64-encoded data
dict | None
Dictionary of plain-text data (will be automatically base64-encoded)
str | None
Secret type (e.g.,
Opaque, kubernetes.io/service-account-token, kubernetes.io/dockercfg, kubernetes.io/tls)bool
default:"True"
Whether to delete the secret on cleanup
str | None
Path to YAML file for the secret
int
default:"TIMEOUT_4MINUTES"
Timeout for delete operations
Properties
certificate_not_after
auth.openshift.io/certificate-not-after.
certificate_not_before
auth.openshift.io/certificate-not-before.
keys_to_hash
["data", "stringData"]
Inherited Methods
Secret inherits all methods from NamespacedResource and Resource.Examples
Creating a Simple Opaque Secret
Creating a Secret with Base64-Encoded Data
Creating a Docker Registry Secret
Creating a TLS Secret
Creating an SSH Key Secret
Creating an AWS Credentials Secret
Creating an htpasswd Secret
Using Secret in a Pod (Volume Mount)
Using Secret in a Pod (Environment Variables)
Listing Secrets
Using Context Manager
Creating from YAML
Secret Types
Opaque (Default)
Arbitrary user-defined data. Most common type for generic secrets.kubernetes.io/service-account-token
Service account token secret.kubernetes.io/dockercfg
Serialized~/.dockercfg file for Docker registry authentication.
kubernetes.io/dockerconfigjson
Serialized~/.docker/config.json file for Docker registry authentication.
kubernetes.io/basic-auth
Credentials for basic authentication withusername and password keys.
kubernetes.io/ssh-auth
Credentials for SSH authentication withssh-privatekey key.
kubernetes.io/tls
TLS certificate and key withtls.crt and tls.key keys.
bootstrap.kubernetes.io/token
Bootstrap token data.Security Best Practices
Access Control
- Use RBAC to restrict access to secrets
- Follow the principle of least privilege
- Separate secrets by namespace
Encryption
- Enable encryption at rest in etcd
- Use external secret management systems (e.g., HashiCorp Vault, AWS Secrets Manager)
Secret Rotation
- Regularly rotate secrets
- Consider using external secret operators for automatic rotation
Avoid Hardcoding
- Never commit secrets to source control
- Use tools like git-secrets or pre-commit hooks
Immutability
- Consider making secrets immutable for production environments
- Recreate secrets instead of updating them
See Also
- NamespacedResource - Base class
- ConfigMap - For non-sensitive configuration data
- Pod - For using Secrets in workloads
- Kubernetes Secrets - Official documentation