Advanced Docker & Kubernetes: A Sysadmin’s Guide to Scaling, Security, and Optimization - Tech Digital Minds
Introduction
Modern infrastructure demands sysadmins to go beyond basic container orchestration. Docker and Kubernetes (K8s) are powerful, but misconfigurations can lead to security breaches, wasted resources, or downtime. This guide dives into advanced techniques for optimizing images, hardening clusters, debugging at scale, and cutting costs without sacrificing performance.
Problem: Bloated images slow deployments and increase attack surfaces.
Solutions:
dockerfile
Copy
Download
# Stage 1: Build
FROM golang:1.21 as builder
WORKDIR /app
COPY . .
RUN go build -o myapp .
# Stage 2: Runtime
FROM alpine:latest
COPY –from=builder /app/myapp /usr/local/bin/
CMD [“myapp”]
Pro Tip: Use dive to analyze image layers:
bash
Copy
Download
dive my-image:latest
bash
Copy
Download
docker network create –driver=bridge –subnet=192.168.100.0/24 my_network
yaml
Copy
Download
# docker-compose.yml
services:
redis:
volumes:
– type: tmpfs
target: /data
bash
Copy
Download
dockerd-rootless-setuptool.sh install
json
Copy
Download
{
“defaultAction”: “SCMP_ACT_ERRNO”,
“syscalls”: [{“name”: “mkdir”, “action”: “SCMP_ACT_ALLOW”}]
}
bash
Copy
Download
export DOCKER_CONTENT_TRUST=1
docker push my-repo/image:signed
yaml
Copy
Download
# Taint a node
kubectl taint nodes node1 gpu=true:NoSchedule
# Pod toleration
tolerations:
– key: “gpu”
operator: “Equal”
value: “true”
effect: “NoSchedule”
yaml
Copy
Download
topologySpreadConstraints:
– maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
yaml
Copy
Download
apiVersion: v1
kind: LimitRange
metadata:
name: cpu-limiter
spec:
limits:
– defaultRequest:
cpu: “500m”
type: Container
bash
Copy
Download
# Install VPA
kubectl apply -f https://github.com/kubernetes/autoscaler/releases/download/vertical-pod-autoscaler-0.14.0/vertical-pod-autoscaler.yaml
bash
Copy
Download
kubectl debug -it my-pod –image=busybox –target=my-pod
yaml
Copy
Download
# fluentd-configmap.yaml
data:
fluent.conf: |
<source>
@type tail
path /var/log/containers/*.log
read_from_head true
<parse>
@type json
</parse>
</source>
<match **>
@type elasticsearch
host elasticsearch.default.svc.cluster.local
</match>
K8s deprecated PSPs in 1.25. Use Pod Security Admission (PSA) or OPA Gatekeeper:
yaml
Copy
Download
# PSA example (enforce baseline policy)
apiVersion: v1
kind: Namespace
metadata:
name: secure-ns
labels:
pod-security.kubernetes.io/enforce: baseline
yaml
Copy
Download
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
– Ingress
– Egress
yaml
Copy
Download
apiVersion: secrets-store.csi.x-k8s.io/v1
kind: SecretProviderClass
metadata:
name: vault-db-creds
spec:
provider: vault
parameters:
vaultAddress: “http://vault:8200”
roleName: “k8s-role”
objects: |
– objectPath: “secret/database”
secretKey: “password”
yaml
Copy
Download
# AWS EKS node group with spot
apiVersion: eksctl.io/v1alpha5
nodeGroups:
– name: ng-spot
instanceTypes: [“t3.large”, “t3a.large”]
spot: true
yaml
Copy
Download
– alert: HighMemoryUsage
expr: container_memory_usage_bytes{pod=”my-pod”} > 1GB
for: 10m
bash
Copy
Download
kubectl cost –window 7d –show-all-resources
Mastering advanced Docker and Kubernetes requires a balance of security, efficiency, and observability. Key takeaways:
Next Steps:
Introduction In January 2024, Elon Musk announced a milestone that sounded like science fiction: the…
Introduction In 2021, a patent application for a fractal-shaped food container and a neural stimulation…
Introduction: Bitcoin’s Lifeline in Conflict In 2025, Bitcoin’s price surge to $150,000 isn’t just a…
1. Introduction Decentralized Finance (DeFi) promised a revolution—borderless, permissionless financial services governed by code, not…
Introduction: The CBDC Tipping Point By 2025, central bank digital currencies (CBDCs) will transition from…
1. Introduction Bitcoin’s halving events are among the most anticipated occurrences in the cryptocurrency market.…