1. Automating Container Engine Installation
Kubernetes requires an OCI-compliant Container Runtime Interface (CRI). For standalone development or host-level auxiliary tooling, engines like Docker and Podman are automated using Ansible.
A critical requirement when configuring container engines for Kubernetes is ensuring the runtime utilizes the `systemd` cgroup driver rather than the legacy cgroupfs driver. Having two distinct cgroup managers causes instability when system memory pressure occurs.
Ansible Playbook: Hardened Docker Daemon Configuration
---
- name: Deploy and Configure Docker Engine with Systemd Cgroup Driver
hosts: container_hosts
become: true
tasks:
- name: Ensure required dependencies are present
ansible.builtin.apt:
name:
- ca-certificates
- curl
- gnupg
state: present
update_cache: true
- name: Configure Docker daemon JSON with security limits
ansible.builtin.copy:
dest: /etc/docker/daemon.json
owner: root
group: root
mode: "0644"
content: |
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "50m",
"max-file": "3"
},
"storage-driver": "overlay2",
"live-restore": true,
"no-new-privileges": true
}
notify: Restart Docker Daemon
handlers:
- name: Restart Docker Daemon
ansible.builtin.systemd:
name: docker
state: restarted2. Multi-Stage Dockerfile Architecture
A major vulnerability in containerized systems is including compilers, package managers, and debug tools in production container images.
The Anti-Pattern: Single-Stage Build
# INSECURE & BLOATED (Size: ~1.2 GB)
FROM golang:1.22
WORKDIR /app
COPY . .
RUN go build -o server .
EXPOSE 8080
CMD ["./server"]
# Contains full Go compiler, git, curl, bash, and build toolchains!The Production Pattern: Multi-Stage Build
# STAGE 1: Compilation Sandbox
FROM golang:1.22-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o /bin/server .
# STAGE 2: Distroless Minimal Runtime (Size: ~18 MB)
FROM gcr.io/distroless/static-debian12:nonroot
WORKDIR /app
COPY --from=builder /bin/server /app/server
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app/server"]
# Contains ZERO shell, zero package manager, and runs as UID 65532!3. Container Hardening Checklist
When deploying containerized workloads, enforce defense-in-depth security:
+----------------------------------------------------------------+
| CONTAINER ISOLATION BOUNDARY |
| |
| +----------------------------------------------------------+ |
| | User Isolation: Runs as unprivileged UID 10001 | |
| | (Root execution disabled via securityContext) | |
| +----------------------------------------------------------+ |
| | Filesystem: Read-Only Root Filesystem (readOnlyRootFilesystem|
| | (Writable paths restricted to isolated in-memory tmpfs) | |
| +----------------------------------------------------------+ |
| | Linux Capabilities: Drop ALL ('CAP_DROP: ALL') | |
| | (Prevent raw sockets, kernel probing, and privilege escalation
| +----------------------------------------------------------+ |
| | Syscall Filtering: Enforce seccomp=RuntimeDefault | |
+--+----------------------------------------------------------+--+- 1Non-Root Execution: Configure
USER 10001:10001in Dockerfiles orrunAsNonRoot: truein Kubernetes security contexts. - 2Read-Only Root Filesystem: Prohibits attackers from writing scripts or modifying binaries if an application vulnerability is exploited.
- 3Drop All Capabilities: Linux divides root privileges into distinct capabilities (e.g.,
CAP_CHOWN,CAP_NET_RAW,CAP_SYS_ADMIN). Drop all capabilities by default, adding back only what is strictly necessary. - 4No New Privileges: Prevent child processes from acquiring additional privileges via setuid binaries.
4. Orchestrating Containers with Ansible
Ansible's community.docker collection enables declarative deployment of local containers:
- name: Deploy Secure Isolated NGINX Reverse Proxy
community.docker.docker_container:
name: edge-proxy
image: nginx:1.25-alpine
state: started
restart_policy: always
read_only: true
user: "101:101"
capabilities:
- NET_BIND_SERVICE
cap_drop:
- ALL
ports:
- "80:8080"
tmpfs:
- /var/cache/nginx:rw,noexec,nosuid,size=50m
- /var/run:rw,noexec,nosuid,size=10m