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).
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.yamlApplying Kustomize via Ansible
- name: Deploy Production Stack via Kustomize Overlay
kubernetes.core.k8s:
state: present
kustomize: ./kustomize/overlays/production2. 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:
- 1Privileged: Unrestricted; allows root access, host networking, and full kernel capabilities. Intended solely for system-level daemonsets (CNI plugins, storage CSI drivers).
- 2Baseline: Minimally restrictive; prevents known privilege escalations while allowing standard default container configurations.
- 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:
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: restricted3. 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 supportsseccompProfile: type: RuntimeDefault, which blocks over 300 dangerous system calls (such asptrace,sys_chroot, andreboot).
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: api-service
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL4. 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)
plugin: kubernetes.core.k8s
connections:
- namespaces:
- production-workloads
labels:
app: web-applicationRunning 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!