Kubernetes - Secrets đĩī¸
Description đ
Secrets are used to store sensitive information such as passwords, API keys, and ssh keys. Secrets are stored in the cluster as base64 encoded strings. Secrets can be create using the kubectl command or using a Secret definition.
- a
secretis only sent to anodeif apodon thatnoderequires it. kubeletstores thesecretinto a tmpfs so that thesecretis not written to disk storage.- once the
podthat depends on thesecretis deleted,kubeletwill delete its local copy of the secret data as well.
Secrets are not encrypted, so it is not safer in that sense. However, some best practices around using secrets make it safer. As in best practices like:
- not checking-in secret object definition files to source code repositories.
- enabling encryption at rest for secrets so they are stored encrypted in
etcd.
Read about the protections and risks of using secrets here.
Basic Commands đ
-
create
secretsusing thekubectlcommandkubectl create secret generic <secret name> --from-literal=<key>=<value> -
create
secretsusing adefinitionkubectl create secret generic <secret name> --from-file=<path to file> -
get
secretskubectl get secret <secret name> -
get all
secretskubectl get secrets -
describe
secretskubectl describe secret <secret name>- note that this method does not show the secret value
-
view
secretsand their valueskubectl get secret <secret-name> -o yaml -
delete
secretskubectl delete secret <secret-name>
Examples đ§Š
-
command used for:Â
secret-examples/sample-secret.yamlkubectl create secret generic sample-secret --from-file=secret-examples/DB_Host --from-file=secret-examples/DB_User --from-file=secret-examples/DB_Password -
command used for:Â
secret-examples/sample-secret2.yamlkubectl create secret generic sample-secret2 --from-literal=DB_Host=postgres --from-literal=DB_User=guerrero --from-literal=DB_Password=pokemon -
sample
secretsDB_HostpostgresDB_UserguerreroDB_Passwordpokemon -
sample
secretdefinitionapiVersion: v1 kind: Secret metadata: creationTimestamp: null name: sample-secret data: DB_Host: cG9zdGdyZXM= DB_Password: cG9rZW1vbg== DB_User: Z3VlcnJlcm8= -
sample
podwithsecretdefinitionapiVersion: v1 kind: Pod metadata: name: secret-pod spec: containers: - name: secret-container image: nginx ports: - containerPort: 80 envFrom: - secretRef: name: sample-secret -
sample tls
secretdefinitionapiVersion: v1 kind: Secret metadata: name: tls-secret type: kubernetes.io/tls data: tls.crt: <base64 encoded cert> tls.key: <base64 encoded key>-
imperative command to create a tls
secretkubectl create secret tls <secret-name> --cert=<path to cert> --key=<path to key>
-