简体   繁体   English

在K8S上安全运行Apache容器

[英]Securely running an apache container on k8s

I've built a docker image based on httpd:2.4 . 我已经基于httpd:2.4构建了一个docker镜像。 In my k8s deployment I've defined the following securityContext : 在我的k8s部署中,我定义了以下securityContext

securityContext:
  privileged: false
  runAsNonRoot: true
  runAsUser: 431
  allowPrivilegeEscalation: false

When I apply the deployment without this securityContext everything works fine, the server starts up correctly, etc. However when I add in the above securityContext my pod has the status CrashLoopBackOff and I get the following from $ kubectl logs... 当我在没有该securityContext情况下应用部署时,一切正常,服务器正常启动, CrashLoopBackOff 。但是,当我在上述securityContext添加时,我的pod的状态为CrashLoopBackOff ,我从$ kubectl logs...获取了以下信息$ kubectl logs...

(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs

From searching around online I've found that this is because apache needs to be root in order to run, so running as non-root will fail. 通过在线搜索,我发现这是因为apache必须是root才能运行,因此以非root身份运行将失败。

I've also found that httpd.conf has the following 我还发现httpd.conf具有以下内容

#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User daemon
Group daemon

Which seems to suggest that if I don't use runAsNonRoot or runAsUser in securityContext it should automatically switch to whichever user I specify in httpd.conf . 这似乎表明,如果我不使用runAsNonRootrunAsUsersecurityContext应该自动切换到任何一个用户我指定httpd.conf In my case I created a user/group swuser and edited httpd.conf accordingly. 就我而言,我创建了一个用户/组swuser并相应地编辑了httpd.conf However when I run the image locally with docker run -p 5000:80 my-registry/my-image:1.0.12-alpha and then run docker exec -it my-container whoami it prints root . 但是,当我使用docker run -p 5000:80 my-registry/my-image:1.0.12-alpha本地运行映像时,请docker run -p 5000:80 my-registry/my-image:1.0.12-alpha ,然后运行docker exec -it my-container whoami它会打印root

So my question is, what can I do to run my container safely as non-root in k8s (and be sure it is non-root) 所以我的问题是,我该怎么做才能在k8s中以非根用户身份安全运行我的容器(并确保它是非根用户)

Run the apache on a port greater than 1024. 在大于1024的端口上运行apache。

Ports below 1024 are privileged ports only available to the root user. 低于1024的端口是特权端口,仅对root用户可用。

As you will have some ingress load balancer before that, it shouldn't matter :-) 因为在此之前您将拥有一些入口负载均衡器,所以没关系:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM