Skip to main content
Foundations: web, servers, containersLesson 7 of 13

Linux Basics for Kubernetes

Every Kubernetes node is a Linux machine, and every container is a Linux process. Follow one app on one host: how it starts, how it is stopped, what its exit code says, why it gets "permission denied", and where its logs go.

Process treebash
Signal—
Exit code—
Access—
Linux host (e.g. a node)Youssh · user devsystemdPID 1kubelet.servicesystemd unitjournaldjournalctl -ubashPID 2001 · uid devappPID 2002 · PPID 2001workerPID 2003config.yaml-rw------- root/var/logsyslog · pods/rootUID 0 · via sudossh login
RequestResponse / reportAppears in a later step

What you will learn

  • PIDs, parents and children, and what PID 1 does
  • SIGTERM vs SIGKILL vs SIGHUP, and trap
  • Reading exit codes: 0, 1, 126, 127, 128+n and 137
  • Users, root, sudo and rwx permissions
  • systemd units, journalctl and where logs live

Exit codes and signals at a glance​

CodeMeaningTypical causeWhat to check
0SuccessThe program finished normally—
1General errorThe program reported a failure itselfApp logs
2Misuse of a shell builtin or bad arguments (common convention)Wrong flagsThe command line
126Found but cannot runMissing x bit, or a directory or wrong binary formatls -l, chmod +x
127Command not foundTypo, or not in PATH (or missing in the image)which cmd, echo $PATH
128 + nKilled by signal n—Which signal, and who sent it
130128 + 2, SIGINTCtrl+C—
137128 + 9, SIGKILLOOM kill, grace period over, kill -9kubectl describe pod Reason, dmesg
143128 + 15, SIGTERMNormal stop request, app did not catch it or exited with the defaultUsually fine during shutdown
SignalNumberCan be caught?Usual meaning
SIGHUP1YesTerminal closed, or "reload config" by convention
SIGINT2YesCtrl+C
SIGKILL9NoStop now, no cleanup
SIGTERM15YesPlease stop; the default from kill and from Kubernetes

Try it yourself​

# Processes, parents and users (on a Linux host or VM)
echo $$ # PID of this shell
ps -o pid,ppid,user,cmd -p $$
sleep 300 & # child of this shell
ps -o pid,ppid,cmd --ppid $$
id # your UID, GID and groups

# Signals and exit codes
sleep 300 & kill $!; wait $!; echo "exit $?" # 143 = 128 + 15
sleep 300 & kill -9 $!; wait $!; echo "exit $?" # 137 = 128 + 9
nosuchcmd; echo "exit $?" # 127
bash -c 'trap "echo got TERM; exit 0" TERM; sleep 30 & wait' & sleep 1; kill $!; wait $! # trap runs

# Permissions
echo secret > demo.txt && chmod 600 demo.txt && ls -l demo.txt # -rw-------
printf '#!/bin/sh\necho hi\n' > hi.sh; ./hi.sh; echo "exit $?" # 126, no x bit
chmod +x hi.sh && ./hi.sh

# systemd and logs (on a Linux host or a kind node: docker exec -it lab-control-plane bash)
systemctl status kubelet --no-pager
journalctl -u kubelet --since "10 min ago" --no-pager | tail
ls /var/log/pods /var/log/containers | head

Check yourself

  1. kubectl describe pod shows Last State: Terminated, Exit Code 137. What does that tell you first?
  2. An app runs as PID 1 in its container and has no SIGTERM handler. What happens when Kubernetes deletes the Pod?
  3. A process running as user dev gets "permission denied" on a file shown as -rw------- root root. Which fix follows least privilege?

Further reading: signal(7) · chmod(1) · journalctl · Pod termination · Graceful shutdown — going down without losing requests · How Linux Isolates a Container