How to enable systemd within a k8s Pod

1.4k Views Asked by At

I am running a kata containers on my k8s cluster and would like to use systemd within the pod.

Using the following dockerfile to build my image:

FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y systemd systemd-sysv && apt-get clean && rm -rf /var/lib/apt/lists/*
# Disable some services that we do not need and which can cause issues
CMD ["/sbin/init"]

And this is my yaml file for the pod:

apiVersion: v1
kind: Pod
metadata:
  name: pod6
spec:
  runtimeClassName: kata-qemu
  containers:
  - name: c1
    image: my-ubuntu-systemd:latest
    imagePullPolicy: Always
    command:
      - bash
    securityContext:
      privileged: true
      capabilities:
            add: ["SYS_ADMIN"]
    volumeMounts:
    - name: cgroup
      mountPath: /sys/fs/cgroup
      readOnly: true
    - name: tmp
      mountPath: /tmp
      subPath: tmp
    - name: tmp
      mountPath: /run
      subPath: run
    - name: tmp
      mountPath: /run/lock
      subPath: run-lock
  volumes:
  - name: cgroup
    hostPath:
      path: /sys/fs/cgroup
      type: Directory
  - name: tmp
    emptyDir:
     medium: Memory
     sizeLimit: 128Mi

Once I am in the pod, getting the following:

root@pod6:/# systemctl status
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

What can I do to fix this?

3

There are 3 best solutions below

0
On BEST ANSWER

Found the answer:

Instead of launching bash, I am launching systemd:

command:
      - /usr/lib/systemd/systemd
0
On

in case this question is still relevant.

you can use

command: 
  - /your/script.sh

and in that script

#!/bin/bash

<do your things>

# now pass arguments, control and pid 1 to systemd
# which is likely linked to /sbin/init, otherwise adapt the path here
exec /sbin/init "$@"
0
On

Using systemd in command doesn't work for me in the case that I need to run a script as command/args. It works fine if you manually exec into the container, but it hangs if you have something like bash my-script.sh as command/args.

Entrypoint of systemd container for Gitlab CI is more relevant in that case.