【问题标题】:Kubernetes 137 exit code when using SecurityContext readOnlyRootFilesystem使用 SecurityContext readOnlyRootFilesystem 时 Kubernetes 137 退出代码
【发布时间】:2021-11-17 00:44:31
【问题描述】:

我正在尝试在具有只读文件系统的容器中托管 Web 应用程序。每当我尝试通过容器的SecurityContext 将根文件系统配置为只读时,我都会收到以下错误:

    Ports:          80/TCP, 443/TCP
    Host Ports:     0/TCP, 0/TCP
    State:          Terminated
      Reason:       Error
      Exit Code:    137
      Started:      Thu, 23 Sep 2021 18:13:08 +0300
      Finished:     Thu, 23 Sep 2021 18:13:08 +0300
    Ready:          False

我尝试使用 AppArmor 配置文件来实现相同的目的,如下所示:

profile parser-profile flags=(attach_disconnected) {
  #include <abstractions/base>
   ...
  deny /** wl,
   ...

不幸的是结果是一样的。

我假设正在发生的事情是容器无法保存 Web 应用程序的文件并且失败了。

在我的场景中,我将运行不受信任的代码,我必须确保不允许用户访问文件系统。

关于我做错了什么以及如何实现只读文件系统的任何想法?

我正在使用 AKS,以下是我的部署配置:

apiVersion: v1
kind: Service
metadata:
  name: parser-service
spec:
  selector:
    app: parser
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    targetPort: 443
    protocol: TCP
    name: https
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: parser-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: parser
  template:
    metadata:
      labels:
        app: parser
      annotations:
        container.apparmor.security.beta.kubernetes.io/parser: localhost/parser-profile
    spec:
      containers:
      - name: parser
        image: parser.azurecr.io/parser:latest
        ports:
        - containerPort: 80
        - containerPort: 443
        resources:
          limits:
            cpu: "1.20"
        securityContext:
          readOnlyRootFilesystem: true


编辑:我也尝试过创建一个集群级 PSP,但也没有用。

【问题讨论】:

  • 您发布的错误 - 它是从哪个命令输出的?您能否运行kubectl logs {pod-name} 并将日志粘贴到您的问题中?我也尝试复制您的问题,但我无法从azurecr.io 提取图像。能否请您在 Docker hub 上提供此图像的链接?
  • 我已经将镜像推送到 Docker Hub,你应该可以在 joronator/parser:latest 下载它。但是,关于日志,由于某种原因,当我执行 kubectl logs podname 时,我收到以下错误 Failed to create CoreCLR, HRESULT: 0x80004005.我将不得不调查一下正在发生的事情。它适用于其他部署的 pod。
  • 现在图像可以工作了,谢谢!请检查我的答案。

标签: kubernetes azure-aks readonly security-context


【解决方案1】:

我设法复制了您的问题并实现了只读文件系统,但一个目录除外。

首先,值得注意的是,您在部署中同时使用了这两种解决方案 - AppArmor 配置文件和 SecurityContext。由于 AppArmor 似乎要复杂得多,并且需要对每个节点进行配置,所以我决定只使用 SecurityContext,因为它工作正常。

我收到了你在评论中提到的这个错误:

Failed to create CoreCLR, HRESULT: 0x80004005

这个错误并没有说太多,但经过一些测试我发现它只在您运行文件系统只读的 pod 时发生 - 应用程序尝试保存文件但不能这样做。

该应用程序在/tmp 目录中创建了一些文件,因此解决方案是使用Kubernetes Volumes 挂载/tmp,以便它可以读写。在我的示例中,我使用了emptyDir,但您可以使用任何其他您想要的卷,只要它支持写入即可。部署配置(可以看到我在底部添加了volumeMountsvolumes):

apiVersion: v1
kind: Service
metadata:
  name: parser-service
spec:
  selector:
    app: parser
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    targetPort: 443
    protocol: TCP
    name: https
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: parser-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: parser
  template:
    metadata:
      labels:
        app: parser
    spec:
      containers:
      - name: parser
        image: parser.azurecr.io/parser:latest
        ports:
        - containerPort: 80
        - containerPort: 443
        resources:
          limits:
            cpu: "1.20"
        securityContext:
          readOnlyRootFilesystem: true
        volumeMounts:
        - mountPath: /tmp
          name: temp
      volumes:
      - name: temp
        emptyDir: {}

执行到 pod 后,我可以看到 pod 文件系统被挂载为只读:

# ls   
app  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# touch file
touch: cannot touch 'file': Read-only file system
# mount 
overlay on / type overlay (ro,...)

通过运行kubectl describe pod {pod-name} 我可以看到/tmp 目录被挂载为读写并且它正在使用temp 卷:

Mounts:
  /tmp from temp (rw)

请记住,如果您使用其他目录(例如保存文件),您还需要以与 /tmp 相同的方式挂载它们。

【讨论】:

  • 那是正确的!干杯伙伴!
猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2016-04-10
相关资源
最近更新 更多