liveBook · Manning

Kubernetes Secret

As explained in Developer Experience on Kubernetes

A Secret is a Kubernetes resource for storing sensitive-looking configuration data, although its encoded contents are not inherently encrypted.

A Kubernetes Secret stores application data such as credentials and configuration properties. It can be referenced by a workload manifest, for example to supply database connection settings to a container rather than placing them directly in the application image. section 3.3.3

A Secret is not inherently secret because its data is usually Base64-encoded, and Base64 can be decoded directly with base64 -d. Base64 is encoding, not encryption. The protection comes from the process used to manage the Secret; by itself, a Secret is like a ConfigMap. section 3.3.3

Use Secrets to keep sensitive values out of container image layers:

  • Do not put passwords or keys in a Dockerfile or image, even temporarily. Removing them in a later layer leaves the original layer accessible. section 3.2.1
  • Reference Secret keys from a Deployment to populate container environment variables, such as PostgreSQL host and port settings. section 3.3.3

Database settings in a Deployment

The appointments container reads POSTGRES_HOST and POSTGRES_PORT through secretKeyRef entries pointing at db-appointments-app. This keeps those values outside the Deployment's literal environment-value fields while making them available to the running container. section 3.3.3

Listing 3.1 Deployment configuration for a cloud native application.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: appointments #A
  labels:
    app: appointments
spec:
  selector:
    matchLabels:
      app: appointments
  template:
    metadata:
      labels:
        app: appointments
    spec:
      containers:
        - name: appointments #B
          image: ko.local/appointments:0.0.1-SNAPSHOT #C
          env: #D
            - name: POSTGRES_HOST
              valueFrom:
                secretKeyRef:
                  name: db-appointments-app
                  key: host
            - name: POSTGRES_PORT
              valueFrom:
                secretKeyRef:
                  name: db-appointments-app
                  key: port
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: db-appointments-app
                  key: dbname
            - name: POSTGRES_USERNAME
              valueFrom:
                secretKeyRef:
                  name: db-appointments-app
                  key: username
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-appointments-app
                  key: password
          ...

Inspect the Deployment manifest to see Secret keys mapped into container environment variables with secretKeyRef. section 3.3.3

How Developer Experience on Kubernetes covers it

How deep it goes
Defines Secrets, distinguishes Base64 encoding from encryption, and demonstrates Deployment key references; defers Secret-management processes.
Examples
Uses an appointments Deployment whose PostgreSQL host and port are read from db-appointments-app Secret keys.
What it assumes
Assumes familiarity with Kubernetes manifests and container images while clarifying that Base64 is not encryption.
What you can do after
Apply it via Kubernetes manifests to provide Secret-backed environment variables to a container.
How it connects
Connects Secrets to ConfigMaps, Deployments, containers, Dockerfiles, and PostgreSQL configuration.

Also explained in

Compare how 7 books explain Kubernetes Secret →

Related concepts

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