liveBook · Manning

Kubernetes Secret

As explained in Kubernetes for Developers

A Kubernetes Secret is an API object that stores sensitive data such as keys, certificates, and credentials.

A Kubernetes Secret is an API object for storing sensitive data—such as database passwords, API keys, private keys, certificates, and other credentials—separately from workload configuration. Pods can receive that data without embedding it directly in application code or ordinary configuration. section 11.4 section 11.5

Choose how a workload consumes the Secret based on the shape of the data:

  • Expose a simple value such as a database password through an environment variable using valueFrom.secretKeyRef. section 11.4.1
  • Mount file-oriented data such as a private SSL key as a read-only directory; each Secret key becomes a file. section 11.4.3
  • Reference certificate and private-key data from an Ingress to terminate TLS connections. section 7.2.1

Define an opaque Secret with stringData for readable values, or use data with Base64-encoded content. kubectl can create Secrets imperatively, or generate YAML with --dry-run=client -o yaml for later application through configuration as code. section 11.4.3

TLS certificate for an Ingress

Create a TLS Secret from certificate files with kubectl create secret tls my-tls-cert --cert example.crt --key example.key, then set the Ingress tls.secretName to my-tls-cert. The Ingress can terminate HTTPS and forward requests to a backend over HTTP or HTTPS.

Listing 11.5 Chapter11/11.4.1_StringSecrets/deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: timeserver
spec:
  replicas: 1
  selector:
    matchLabels:
      pod: timeserver-pod
  template:
    metadata:
      labels:
        pod: timeserver-pod
    spec:
      containers:
      - name: timeserver-container
        image: docker.io/wdenniss/timeserver:5
        env:
        - name: AVATAR_ENDPOINT            #1
          value: http://robohash-internal  #1
        - name: SECRET_KEY                 #2
          valueFrom:                       #2
            secretKeyRef:                  #2
              name: secrets-production     #2
              key: SECRET_KEY              #2
#1 A regular plain-text environment variable
#2 An environment variable populated from a Secret

Inspect the Deployment pattern that injects a Secret value into the SECRET_KEY environment variable. section 11.4.1

How Kubernetes for Developers covers it

How deep it goes
Explains object definitions, string and file delivery, TLS references, and kubectl workflows, but does not detail storage protection.
Examples
Uses database passwords, API keys, private SSL keys, and TLS certificates delivered through environment variables, mounted files, and Ingress.
What it assumes
Assumes basic Kubernetes object and workload configuration knowledge while teaching Secret creation and consumption patterns directly.
What you can do after
Operate/configure Kubernetes workloads to consume sensitive values as environment variables, mounted files, or Ingress TLS credentials.
How it connects
Connects Secrets with Pods, Deployments, Ingress, TLS, environment variables, volumes, and GitOps configuration as code.

Also explained in

Compare how 6 books explain Kubernetes Secret →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.