12-Factor App: Dev/Prod Parity, Logging Streams, and One-Off Admin Operations

Factor X: Development and Production Parity

  • Problem solved: When development, staging, and production differ in hardware, OS, backing services, or deployment workflow, bugs appear only after reaching production.
  • The three parity gaps (slide 20):
    1. Time gap: Weeks or months pass between writing code and deploying it, hiding which change broke production.
    2. Personnel gap: One group writes code (developers) while a separate group deploys and operates it (operators), creating handoff friction.
    3. Tools gap (“It works on my computer”): Developers run lightweight local tools/stacks that behave differently from the production stack.

Closing the gaps: CI/CD, GitOps, and Infrastructure as Code

  • CI/CD Pipelines (slides 21–22): Declarative pipeline files (see DevOps and CI-CD Pipelines) automate integration and delivery across stages: Source (git push) -> Build (compile, docker build) -> Test (smoke, unit, integration) -> Deploy (staging -> QA -> production).
    • Tooling examples from the slides: Jenkins (formerly Hudson); CI-as-a-Service (GitLab CI, TravisCI, SemaphoreCI, CircleCI, GitHub Actions).
    • GitOps: Kubernetes-native frameworks (ArgoCD, JenkinsX; see GitOps Workflow Components and Tradeoffs) map Git operations—such as a PR Merge—directly into cluster deployment operations, narrowing both time and personnel gaps.
  • Infrastructure as Code (IaC) and Local Kubernetes (slide 23): Two standard approaches keep environments identical:
    1. Server Templating (e.g., Docker, Packer) — build immutable machine/container templates.
    2. Server Provisioning (e.g., Terraform; see Infrastructure as Code Terraform and OpenTofu) + Configuration Management (e.g., Ansible).
    • Because cloud-native apps also interact with Kubernetes APIs, developers run lightweight local clusters—Minikube, MicroK8s, or KIND (Kubernetes in Docker)—to close the tools gap.

Factor XI: Logging as a Stream

  • Problem solved: Writing logs to local file paths (such as /var/log/app.log) forces the application to assume host filesystem layout and loses logs whenever a disposable Pod crashes or is deleted.
  • Mechanism: Applications print logs purely as an unbuffered event stream to stdout, leaving collection, routing, and storage to the execution environment.
  • Why kubectl logs is not enough (slide 24):
    • kubectl logs retrieves stream output from the Docker daemon -> Kubelet -> API Server -> user.
    • Critical limitation: Those logs belong to a specific container instance; if the container crashes or the Pod is deleted, the logs are lost permanently.
  • Centralized log aggregation in Kubernetes (slides 25–26):
    1. Collection Agents (Fluentd, FluentBit): Deployed as a DaemonSet so one collector runs automatically on every node (master included).
    2. Enrichment & Indexing: Agents read node container streams, enrich events with Kubernetes metadata (pod, namespace, deployment tags), and ship them to a Log Aggregation System (Elasticsearch, Loki, or cloud-managed StackDriver).
  • Engineering challenges (slide 25): Log traffic can reach multiple MB/s (risking performance overhead), and teams must handle multi-tenancy (e.g., shared Ingress Controller logs), fine-grained tagging (deployment grain), and custom store integration.

Factor XII: Admin as One-Off Operations

  • Problem solved: Applications occasionally require one-time maintenance tasks—such as migrating a database to a new schema, backfilling data, or seeding initial records—that do not belong in the continuously running request loop.
  • Analogy: A restaurant kitchen runs continuously serving diners. Re-labeling every spice jar before launching a new menu should happen as a one-time prep job, not by making every chef stop cooking orders to re-label jars repeatedly, nor by letting someone scribble unrecorded changes on a napkin. (Limit: prep tasks in Kubernetes must still ship with the release’s exact code and config.)
  • The two traps Factor XII avoids:
    1. Baking admin logic into normal app startup: If 5 replicas of a Deployment boot simultaneously, all 5 try to run the schema migration at once (race conditions, locks, crashes), and later Pod restarts re-run admin logic needlessly.
    2. Manual “cowboy ops”: Running ad-hoc commands manually against production breaks repeatability, skips staging validation, and leaves no versioned audit trail.
  • Golden rule — Separate execution, shared versioning: Even though admin tasks run outside the core application execution, they must be versioned and shipped together with the application codebase (Factor I), container dependencies (Factor II), and environment configuration (Factor III).

Kubernetes mechanisms: Job vs. InitContainer (slide 27)

MechanismExecution behaviorTypical role
One-time Job (or one-time Pod)Spawns a standalone Pod, runs the admin task once to completion, then exitsRelease-wide operations such as a database schema migration or one-time data backfill
InitContainerDefined inside a Pod spec; runs strictly before normal application containers startPer-Pod startup prep, such as checking database readiness or preparing local config before the main app boots

Retrieval

  1. Match each of Factor X’s three parity gaps (Time, Personnel, Tools) to a concrete practice or tool from the lecture.
    Time gap: automated CI/CD pipelines and GitOps (e.g., ArgoCD deploying on PR merge). Personnel gap: shared declarative pipelines/GitOps bridging devs and ops. Tools gap: IaC (Docker/Packer, Terraform + Ansible) and local Kubernetes clusters (Minikube, MicroK8s, KIND).
  2. Why can’t production operations rely solely on kubectl logs, and why are Fluentd/FluentBit deployed as a DaemonSet rather than a regular Deployment?
    kubectl logs reads directly from a live container instance via Kubelet and API Server—if the container crashes or the Pod is deleted, those logs are lost. Deploying collectors as a DaemonSet guarantees an agent runs on every node (including the master) to capture stdout streams locally, enrich them with Pod/Namespace metadata, and forward them to Elasticsearch/Loki.
  3. Why is a Kubernetes Job preferred over an InitContainer or main server startup code for running a database schema migration?
    Putting migration in main startup code or an InitContainer runs it on every Pod replica (causing concurrent races across 5 replicas and re-running on restarts), whereas a Job executes the versioned migration once for the release while still shipping with the same image and ConfigMap/Secret configuration.

Source: Lecture 12 - Cloud Native Applications and the 12-Factor App, PDF pages 19–26 (printed slides 20–27).