如何在k8s Pod中启用systemd

huangapple go评论56阅读模式
英文:

How to enable systemd within a k8s Pod

问题

我正在我的K8s集群上运行Kata容器,并希望在容器中使用systemd。

使用以下Dockerfile来构建我的镜像:

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/*
# 禁用一些不需要的服务,可能会引发问题
CMD ["/sbin/init"]

这是我的Pod的YAML文件:

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

当我进入Pod后,出现以下问题:

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

我该如何解决这个问题?

英文:

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?

答案1

得分: 0

找到答案:

不是启动bash,而是启动systemd:

命令:
- /usr/lib/systemd/systemd

英文:

Found the answer:

Instead of launching bash, I am launching systemd:

command:
      - /usr/lib/systemd/systemd

答案2

得分: 0

在命令中使用 systemd 对我来说行不通,因为我需要将脚本作为命令/参数运行。如果您手动进入容器并执行,它可以正常工作,但如果命令/参数中有类似 bash my-script.sh 的内容,它会挂起。

在这种情况下,https://stackoverflow.com/questions/73714080/entrypoint-of-systemd-container-for-gitlab-ci 更相关。

英文:

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.

https://stackoverflow.com/questions/73714080/entrypoint-of-systemd-container-for-gitlab-ci is more relevant in that case.

huangapple
  • 本文由 发表于 2023年6月22日 03:44:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定