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:
Exploring the Best Electronic Data Interchange (EDI) Software of 2023 In today's fast-paced business landscape,…
Understanding n8n: A Low-Code Workflow Automation Tool 1. What is n8n? n8n is a source-available,…
The Hidden Reality of Smart TVs: Are You Aware of What They’re Tracking? Credit: Adam…
The Future of Apple: A Foldable iPhone with Under-Display Camera Technology What’s in Store? The…
The Hidden Value of Timeless Gadgets: A Rebellion Against Planned Obsolescence Planned obsolescence has become…
The Ultimate Guide to Choosing the Best Antivirus Software Understanding the Importance of Antivirus Software…