Zero Trust Identity with SPIFFE/SPIRE on Kubernetes: Secretless mTLS Guide

Security tutorial - IT technology blog
Security tutorial - IT technology blog

Approach Comparison: Traditional Secrets vs. SPIFFE/SPIRE Workload Identity

Most engineering teams start microservice authentication the easiest way: baking API tokens or TLS certificates into Kubernetes Secrets, mounting them as volume projections, and hoping for the best. That setup works until a compromised container or leaked etcd snapshot blows your perimeter wide open. Static credentials sitting on a filesystem are permanent targets.

When designing mutual TLS (mTLS) across microservices, you generally pick between three main architectures:

  • Static Kubernetes Secrets with cert-manager: Cert-manager issues X.509 certificates and drops them into standard Kubernetes Secrets. Pods mount these files directly. This pattern is easy to inspect and debug. However, private keys hit the disk, certificates persist in etcd, and secret rotation requires restarting pods or managing file-reload signals across dozens of services.
  • Service Mesh Sidecars (e.g., Istio, Linkerd): Meshes inject an Envoy or Rust proxy alongside every container to handle mTLS transparently. While this eliminates crypto code from your application, it comes at a steep price. Each sidecar typically consumes 50MB to 120MB of RAM and adds 1.5ms to 4ms of network latency per hop. That adds up fast on large clusters.
  • SPIFFE/SPIRE Attestation (Secretless Identity): SPIFFE standardizes workload naming with structured URIs (such as spiffe://itfromzero.local/ns/prod/sa/payment-service). SPIRE issues identities by evaluating kernel-level attributes via local Unix domain sockets. The application retrieves its short-lived X.509 SVID (SPIFFE Verifiable Identity Document) directly into memory. Zero keys ever touch disk or etcd.

Evaluating SPIRE on Kubernetes: Strengths and Trade-offs

SPIRE gives you fine-grained identity, but running it requires deliberate operational planning. Here is what to expect before migrating your cluster.

The Advantages

  • Zero Static Secrets on Disk: SVIDs reside in memory and routinely carry TTLs between 10 and 60 minutes. If an attacker gains read access to the node rootfs or inspects a pod volume, there are no long-lived keys to steal.
  • Kernel-Level Platform Attestation: Workloads cannot spoof their identity simply by passing a header. The SPIRE Agent inspects Linux cgroups, container IDs, namespaces, and kubelet pod specs directly before signing any identity request.
  • Native Federation: SPIFFE trust domains federate cleanly. You can authenticate pods across independent Kubernetes clusters, AWS IAM roles, and bare-metal nodes without sharing private CA keys.

The Trade-offs

  • Application Code Integration: Without a sidecar proxy, your services must talk directly to the SPIFFE Workload API using client SDKs (available in Go, Java, Python, and C++). You own the TLS handshake logic inside the application runtimes.
  • Node Agent Criticality: The SPIRE Agent runs as a DaemonSet. If an agent crashes or the socket directory unmounts, workloads on that node cannot renew expiring certificates. Renewal failures mean dropped TLS connections within the hour.

Recommended Production Architecture

Do not run SQLite in production. Deploy the SPIRE Server in High Availability mode backed by an external PostgreSQL or Amazon RDS instance. In front of the API server, run the SPIRE Kubernetes Workload Registrar. The registrar watches pod lifecycle events and automatically generates SPIFFE registration entries based on pod annotations and service accounts.

Core Components

  • SPIRE Server: Manages the CA root key, signs X.509 SVIDs, and validates agent identity via Kubernetes Projected Service Account Tokens (PSAT).
  • SPIRE Agent (DaemonSet): Validates local pods through workload attestation and exposes the SPIFFE Workload API over a host-mounted Unix domain socket at /run/spire/sockets/agent.sock.
  • SPIRE K8s Workload Registrar: Eliminates manual CLI registration by dynamically translating pod specs into SPIFFE entries.

Implementation Guide: Deploying SPIRE and Enabling Secretless mTLS

1. Set Up the Namespace and Pod Security Standards

Create a dedicated namespace for SPIRE. Because the agent mounts host paths and inspects system processes, set the namespace pod security profile to privileged:

kubectl create namespace spire

# Allow the SPIRE Agent to access host namespaces and kernel cgroups
kubectl label namespace spire pod-security.kubernetes.io/enforce=privileged

2. Deploy SPIRE Server with PSAT Node Attestation

Apply the server configuration ConfigMap. This setup defines the trust domain, sets a 1-hour SVID TTL, and activates the PSAT attestor:

apiVersion: v1
kind: ConfigMap
metadata:
  name: spire-server
  namespace: spire
data:
  server.conf: |
    server {
      bind_address = "0.0.0.0"
      bind_port = "8081"
      trust_domain = "itfromzero.local"
      data_dir = "/run/spire/data"
      log_level = "INFO"
      default_x509_svid_ttl = "1h"
      ca_subject = {
        country = ["US"],
        organization = ["ITFromZero"],
        common_name = "ITFromZero Workload Root CA",
      }
    }
    plugins {
      DataStore "sql" {
        plugin_data {
          database_type = "sqlite3"
          connection_string = "/run/spire/data/datastore.sqlite3"
        }
      }
      NodeAttestor "k8s_psat" {
        plugin_data {
          cluster = "production-cluster"
        }
      }
      KeyManager "disk" {
        plugin_data {
          keys_path = "/run/spire/data/keys.json"
        }
      }
    }

3. Deploy the SPIRE Agent DaemonSet

The agent needs host network access and a shared directory where it creates the Workload API socket:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: spire-agent
  namespace: spire
spec:
  selector:
    matchLabels:
      app: spire-agent
  template:
    metadata:
      labels:
        app: spire-agent
    spec:
      hostPID: true
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      serviceAccountName: spire-agent
      containers:
        - name: spire-agent
          image: ghcr.io/spiffe/spire-agent:1.8.7
          args: ["-config", "/run/spire/config/agent.conf"]
          volumeMounts:
            - name: spire-config
              mountPath: /run/spire/config
              readOnly: true
            - name: spire-agent-socket
              mountPath: /run/spire/sockets
              readOnly: false
      volumes:
        - name: spire-config
          configMap:
            name: spire-agent
        - name: spire-agent-socket
          hostPath:
            path: /run/spire/sockets
            type: DirectoryOrCreate

4. Register a Workload Entry

Map the backend-api service account to a specific SPIFFE ID. Run this entry creation command inside the SPIRE server container:

kubectl exec -n spire spire-server-0 -c spire-server -- \
  /opt/spire/bin/spire-server entry create \
  -spiffeID spiffe://itfromzero.local/ns/default/sa/backend-api \
  -parentID spiffe://itfromzero.local/spire/agent/k8s_psat/production-cluster \
  -selector k8s:ns:default \
  -selector k8s:sa:backend-api \
  -ttl 3600

5. Consume X.509 SVIDs Inside Your Microservice

Mount the agent socket directory into your application pod manifest using a read-only hostPath volume:

apiVersion: v1
kind: Pod
metadata:
  name: backend-service
  namespace: default
spec:
  serviceAccountName: backend-api
  containers:
    - name: api
      image: python:3.11-slim
      command: ["sleep", "3600"]
      volumeMounts:
        - name: spire-agent-socket
          mountPath: /run/spire/sockets
          readOnly: true
  volumes:
    - name: spire-agent-socket
      hostPath:
        path: /run/spire/sockets
        type: Directory

Your application connects to the local Unix domain socket and streams its ephemeral TLS certificates directly into memory. Test the connection with this quick Python snippet:

import socket
import sys

socket_path = "/run/spire/sockets/agent.sock"

try:
    client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    client.connect(socket_path)
    print("[+] Connected to SPIFFE Workload API socket.")
    print("[+] Ready to stream ephemeral X.509 SVIDs into process memory.")
    client.close()
except socket.error as err:
    print(f"[-] Socket connection failed: {err}")
    sys.exit(1)

Once connected, use the official SPIFFE Go, Java, or Python client libraries to automatically refresh X.509 SVIDs every 30 minutes without restarting your service or writing secret keys to disk.

Share: