Lesson 7 / 10
Module 7Lesson 1⏱ 50 min study

Lesson 7: Kustomize Overlays, Security & Dynamic Inventory

Eliminate configuration drift with Kustomize, implement Pod Security Standards and Linux kernel hardening, and orchestrate clusters using dynamic inventories.

✦ Key Architectural Takeaways
  • •Kustomize provides template-free customization through declarative overlays (dev, staging, prod) without string interpolation bugs.
  • •Pod Security Standards (PSS) define three hardening levels: Privileged, Baseline, and Restricted, enforced via namespace admission labels.
  • •Kernel-level hardening utilizes AppArmor profiles, Seccomp system call filtering, and strict Linux capability dropping.
  • •Ansible Dynamic Inventories query live Kubernetes API servers to generate real-time host and pod inventories dynamically.
  • •Combining Kustomize with Ansible enables multi-environment continuous deployment without duplicating manifest files.

1. Template-Free Customization with Kustomize

When managing applications across Development, Staging, and Production environments, naive templating approaches often introduce syntax errors and configuration drift.

Kustomize introduces a pure declarative approach:

  • Base: Contains the common, core Kubernetes manifests shared across all environments.
  • Overlays: Contains environment-specific patches (e.g., dev uses 1 replica and low memory; prod uses 5 replicas and persistent cloud disks).
ARCHITECTURE DIAGRAMSYSTEM SCHEMATIC
Directory Structure:
kustomize/
  base/
    deployment.yaml
    service.yaml
    kustomization.yaml
  overlays/
    development/
      replica_patch.yaml
      kustomization.yaml
    production/
      replica_patch.yaml
      ingress_patch.yaml
      kustomization.yaml

Applying Kustomize via Ansible

YAMLSYSTEM SCHEMATIC
- name: Deploy Production Stack via Kustomize Overlay
  kubernetes.core.k8s:
    state: present
    kustomize: ./kustomize/overlays/production
§

2. Pod Security Standards (PSS) & Admission (PSA)

Legacy PodSecurityPolicies (PSP) were deprecated due to operational complexity. Modern Kubernetes replaces them with built-in Pod Security Standards (PSS) categorized into three profiles:

  1. 1Privileged: Unrestricted; allows root access, host networking, and full kernel capabilities. Intended solely for system-level daemonsets (CNI plugins, storage CSI drivers).
  2. 2Baseline: Minimally restrictive; prevents known privilege escalations while allowing standard default container configurations.
  3. 3Restricted: Hardened production profile. Mandates running as non-root, read-only root filesystems, dropping all capabilities, and disabling privilege escalation.

Enforcing PSS via Namespace Labels:

YAMLSYSTEM SCHEMATIC
apiVersion: v1
kind: Namespace
metadata:
  name: payment-processing
  labels:
    # Reject non-compliant pods immediately
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/enforce-version: latest
    # Warn administrators in CLI output
    pod-security.kubernetes.io/warn: restricted
§

3. Linux Kernel Hardening: AppArmor & Seccomp

Defense-in-depth requires restricting what containerized processes can execute at the Linux kernel boundary:

  • AppArmor: A Linux Security Module (LSM) that confines programs to a specific profile, restricting access to file paths, raw network sockets, and capabilities.
  • Seccomp (Secure Computing Mode): Restricts which system calls (syscalls) a process can execute. Modern Kubernetes supports seccompProfile: type: RuntimeDefault, which blocks over 300 dangerous system calls (such as ptrace, sys_chroot, and reboot).
YAMLSYSTEM SCHEMATIC
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 10001
    seccompProfile:
      type: RuntimeDefault
  containers:
    - name: api-service
      securityContext:
        allowPrivilegeEscalation: false
        readOnlyRootFilesystem: true
        capabilities:
          drop:
            - ALL
§

4. Ansible Dynamic Inventory for Kubernetes

In elastic cloud environments, worker nodes and pods scale up and down dynamically. Static IP inventories become obsolete within hours.

Ansible's kubernetes.core.k8s inventory plugin queries the Kubernetes API server at runtime to build dynamic inventories based on namespaces, labels, and annotations:

Dynamic Inventory Configuration (k8s.yaml)

YAMLSYSTEM SCHEMATIC
plugin: kubernetes.core.k8s
connections:
  - namespaces:
      - production-workloads
    labels:
      app: web-application

Running ansible-inventory -i k8s.yaml --graph outputs all matching pods as addressable Ansible target hosts, enabling automated diagnostic scripts and live log audits across distributed clusters!