Sub topic 8
Built-in Secret Types
This chapter assumes you can write Pod and Secret manifests, use kubectl, and know that Secret values are base64-encoded in the data field. Every Secret has a type field. The type tells Kubernetes, and the tools that read the Secret, what keys to expect.
What the type field does
If you omit type, Kubernetes sets it to Opaque. For most other built-in types, the API server validates that required keys exist and rejects the object if they are missing. The type is fixed after creation, so you cannot change it later.
| Type | Purpose | Required keys |
|---|---|---|
| `Opaque` | Arbitrary user data | None |
| `kubernetes.io/dockerconfigjson` | Image registry credentials | `.dockerconfigjson` |
| `kubernetes.io/basic-auth` | Username and password | `password` (and usually `username`) |
| `kubernetes.io/ssh-auth` | SSH private key | `ssh-privatekey` |
| `kubernetes.io/tls` | TLS certificate and key | `tls.crt`, `tls.key` |
| `kubernetes.io/service-account-token` | ServiceAccount token | Annotation naming the ServiceAccount |
| `bootstrap.kubernetes.io/token` | Node bootstrap token | Token ID and secret keys |
Opaque
Opaque is the default. Use it for database passwords, API keys, and anything else without a standard shape. The db-credentials Secret from the previous chapters is an Opaque Secret.
Registry credentials
A Secret of type kubernetes.io/dockerconfigjson holds the login for a private image registry. A Pod references it through imagePullSecrets. The easiest way to create one is with kubectl:
kubectl create secret docker-registry regcred \
--docker-server=registry.example.com \
--docker-username=app \
--docker-password='S3cr3t!'The command stores a JSON document under the .dockerconfigjson key. The Pod then uses it like this:
apiVersion: v1
kind: Pod
metadata:
name: private-app
spec:
imagePullSecrets:
- name: regcred
containers:
- name: app
image: registry.example.com/team/app:1.0The kubelet uses the credentials only when it pulls the image. The container itself never sees them.
TLS Secrets
A kubernetes.io/tls Secret holds a certificate and its private key. Ingress controllers read this type to terminate HTTPS.
kubectl create secret tls web-tls \
--cert=tls.crt \
--key=tls.keyThe API server checks that both tls.crt and tls.key are present. It does not check that the key matches the certificate.
Basic auth and SSH
The basic-auth and ssh-auth types add key-name validation only. They store the data the same way Opaque does. Choosing them documents your intent and lets tools find the right keys.
apiVersion: v1
kind: Secret
metadata:
name: git-ssh
type: kubernetes.io/ssh-auth
stringData:
ssh-privatekey: |
-----BEGIN OPENSSH PRIVATE KEY-----
(key material)
-----END OPENSSH PRIVATE KEY-----ServiceAccount tokens
Since Kubernetes 1.24, Pods get short-lived tokens through the TokenRequest API, and Kubernetes no longer creates a token Secret for each ServiceAccount automatically. You can still create a kubernetes.io/service-account-token Secret by hand if you need a long-lived token. Treat it as a last resort, because the token does not expire.
Common errors
- Missing key. Creating a
kubernetes.io/tlsSecret withouttls.keyfails with a validation error. - Wrong type for a consumer. An Ingress that references an
OpaqueSecret for TLS may not work, because the controller expects thetls.crtandtls.keykeys of the TLS type. - Changing the type. The
typefield is immutable. Delete and recreate the Secret instead.
Exercise
You created a Secret with type: kubernetes.io/tls and only a tls.crt key. What happens, and which type would you pick for an arbitrary API key?
Show answer
The API server rejects the Secret because the required tls.key is missing. For an arbitrary API key, use Opaque, the default type for data without a standard shape.
Exercise
Which Secret type does a Pod reference through imagePullSecrets, and who reads the credentials?
Show answer
The type is kubernetes.io/dockerconfigjson. The kubelet reads it when pulling the image, and the container never sees it.