How to install and run QEMU on K8s nodes?
If you find yourself having to emulate different CPU architectures in a Kubernetes environment you’ll probably end up running some version of binfmt as an init container or maybe manually run it once.
That would probably be ok in most cases, but wouldn’t work if, for example, you’re running multiple pod replicas on the same node concurrently (say, when you setup a CI that spawns pods for every job in your pipeline) or when nodes are autoscaled.
There’re ways to make sure only one pod sets up binfmt, but when running pods concurrently, it’s very difficult to orchestrate.
The cleaner solution is a DaemonSet, which runs binfmt once on any existing or newly scheduled node in your cluster.
Create a daemonset configuration:
# Run binfmt setup on any node
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: binfmt
namespace: kube-system
labels:
app: binfmt-setup
spec:
selector:
matchLabels:
name: binfmt
template:
metadata:
labels:
name: binfmt
spec:
tolerations:
# Allow daemonset to run on control plane nodes if needed
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
initContainers:
- name: binfmt
image: tonistiigi/binfmt:latest
args: ["--install", "all"]
securityContext:
privileged: true
containers:
- name: pause
image: registry.k8s.io/pause:3.9
resources:
limits:
cpu: 50m
memory: 50Mi
requests:
cpu: 10m
memory: 20MiApply it:
kubectl apply -f ./daemonset.yamlOnce the pods transition to running, QEMU and binfmt_misc handlers are registered in the host Linux kernel for all nodes.
If you decide on a different approach later, remove the daemonset and its pods:
kubectl delete daemonset binfmt --namespace=kube-system