首页 > 解决方案 > Kubernetes 安全上下文

问题描述

如果我删除 spec.containers.command,我似乎无法理解为什么下面提到的 pod 清单不起作用,如果我删除命令,pod 会失败。

我从官方文档中拿了这个例子

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo
spec:
  securityContext:
    runAsUser: 1000
    runAsGroup: 3000
    fsGroup: 2000
  volumes:
  - name: sec-ctx-vol
    emptyDir: {}
  containers:
  - name: sec-ctx-demo
    image: busybox
    command: [ "sh", "-c", "sleep 1h" ]
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
    securityContext:
      allowPrivilegeEscalation: false

标签: kuberneteskubernetes-security

解决方案


因为busybox图像本身在启动时不会运行任何进程。容器旨在运行单个应用程序并在应用程序退出时关闭。如果图像没有运行任何东西,它将立即退出。在 Kubernetes 中,spec.containers.command覆盖默认容器命令。您可以尝试更改 ie 的清单图像image: nginx,删除spec.containers.command它并且它将运行,因为该图像作为默认 Nginx 服务器。


推荐阅读