Ephemeral Containers — temporary containers in Pod for debugging (without restart).
Problem: If a container is in CrashLoopBackOff, you cannot kubectl exec (container is not running).
Solution — debug container:
1# Add debug container to Pod2kubectl debug -it my-pod --image=busybox --target=my-container34# Clone Pod with debug container5kubectl debug node/my-node -it --image=busybox67# Debug with PID namespace change8kubectl debug -it my-pod --image=busybox --target=my-container --same-namespace
Ephemeral Container spec:
1apiVersion: v12kind: Pod3metadata:4 name: my-pod5spec:6 ephemeralContainers:7 - name: debugger8 image: busybox:1.369 command: ["sh"]10 stdin: true11 tty: true12 targetContainerName: my-container13 resources:14 requests:15 memory: "64Mi"16 cpu: "100m"
Debugging CrashLoopBackOff:
1# View logs of previous container2kubectl logs my-pod --previous34# Add ephemeral container5kubectl debug -it my-pod --image=busybox --target=my-container67# Inside debug container8ls /proc/1/root/9cat /proc/1/root/etc/config.yaml10nsenter -t 1 -n ss -tlnp
Advantages: does not restart Pod, does not affect main container, access to PID namespace.