- AppArmor:定义Pod使用AppArmor限制容器对资源访问限制
- Seccomp:定义Pod使用Seccomp限制容器进程的系统调用。
- AllowPrivilegeEscalation: 禁止容器中进程(通过 SetUID 或 SetGID 文件模式)获得特权提升。当容器以特权模式运行或者具有CAP_SYS_ADMIN能力时,AllowPrivilegeEscalation总为True。
- readOnlyRootFilesystem:以只读方式加载容器的根文件系统。
1.1 配置参数
注意事项:
- securityContext下配置的参数是针对该pod里的所有容器生效。
- containers下配置的参数只对某个容器生效,不同容器配置各自需要的参数。
| pod安全上下文配置参数 | 释义 |
|---|---|
| spec.securityContext | Pod级别的安全上下文,对内部所有容器均有效。 |
| spec.securityContext.runAsUser < integer > | 以指定的用户身份运行容器进程,默认由镜像中的USER指定。 |
| spec.securityContext.runAsGroup < integer > | 以指定的用户组运行容器进程,默认使用的组随容器运行时。 |
| spec.securityContext.fsGroup < integer > | 数据卷挂载后的目录文件设置为该组。 |
| spec.securityContext.runAsNonRoot < boolean > | 是否以非root身份运行。 |
| spec.securityContext.seLinuxOptions < Object > | SELinux的相关配置。 |
| spec.securityContext.sysctls < lObject > | 应用到当前Pod的名称空间级别的sysctl参数设置列表。 |
| spec.containers. securityContext | 容器级别的安全上下文,仅生效于当前容器。 |
| spec.containers. securityContext.runAsUser < integer > | 以指定的用户身份运行容器进程。 |
| spec.containers. securityContext.runAsGroup < integer > | 以指定的用户组运行容器进程。 |
| spec.containers. securityContext.runAsNonRoot < boolean > | 是否以非root身份运行。 |
| spec.containers. securityContext.allowPrivilegeEscalation < boolean > | 是否允许特权升级。 |
| spec.containers. securityContext.capabilities < Object > | 于当前容器上添加 (add) 删除 (drop)的内核能力。 |
| spec.containers. securityContext.add < [ ]string > | 添加由列表定义的各内核能力。 |
| spec.containers. securityContext.drop< [ ]string > | 移除由列表定义的各内核能力。 |
| spec.containers. securityContext.privileged < boolean > | 是否运行为特权容器。 |
| spec.containers. securityContext.readOnlyRootFilesystem < boolean > | 是否将根文件系统设置为只读模式 |
| spec.containers. securityContext.selinuxOptions < opect > | SeLinux的相关配置。 |
1.2 案例1
背景:
- 容器中的应用程序默认以root账号运行的,这个root与宿主机root用户权限是一样的,拥有大部分对Linux内核的系统调用权限,不安全,所以我们应该将容器里的程序以普通用户运行,减少应用程序对权限的使用。
需求:
- 设置容器以普通用户运行。
实现思路:
- Dockerfile里使用USER指定运行用户。
- K8s里指定spec.securityContext.runAsUser,指定容器默认用户UID。
1.2.1 dockerfile方式
1.准备程序源码。我这里是使用python写的一个脚本,渲染了一个index.html的模板首页,端口是8080。
[root@k8s-master1 flask-demo]# cat Dockerfile
FROM python ##基于python基础镜像构建。
RUN mkdir /data/www -p
COPY . /data/www ##将当前目录下的文件目录拷贝到/data/www目录。
RUN pip install flask -i https://mirrors.aliyun.com/pypi/simple/ ##安装flask,是Python 的一个web框架
WORKDIR /data/www ##定义默认目录。
CMD python qingjun.py ##使用python启动这个man.py脚本。
##使用python写了一个web的demo。
[root@k8s-master1 flask-demo]# cat qingjun.py
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run(host="0.0.0.0",port=8080)
[root@k8s-master1 flask-demo]# cat templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>你好 qingjun!</h1>
</body>
</html>
2.构建镜像。
[root@k8s-master1 flask-demo]# docker build -t flask-demo:v1 .

3.使用构建镜像运行容器qingjun,此时可以查看到容器运行的进程是以root用户进行的。
[root@k8s-master1 flask-demo]# docker run -d --name=qingjun -p 80:8080 flask-demo:v1


4.访问web页面。

5.此时我们修改dockerfile,指定使用普通用户启用。
[root@k8s-master1 flask-demo]# cat Dockerfile
FROM python
RUN useradd qingjun ##创建普通用户qingjun。
RUN mkdir -p /data/www
COPY . /data/www
RUN chown -R qingjun /data #数据目录修改权限,不然还是root用户。
RUN pip install flask -i https://mirrors.aliyun.com/pypi/simple/
WORKDIR /data/www
USER qingjun ##指定使用什么用户启用python程序。
CMD python qingjun.py
6.再次构建镜像。
[root@k8s-master1 flask-demo]# docker build -t flask-demo:v2 .
7.新镜像运行容器baimu,此时可以查看到容器运行的进程是以普通用户pyuser进行的。
[root@k8s-master1 flask-demo]# docker run -d --name=baimu -p 81:8080 flask-demo:v2


8.访问网页。

1.2.2 pod安全上下文方式
deployment.yaml指定示例:
spec: securityContext: runAsUser: 1000 ##镜像里必须有这个用户UID。 fsGroup: 1000 ##数据卷挂载后的目录属组设置为该组。 containers: - image: 192.168.130.152/qingjun/flask-demo:v3 name: web securityContext: allowPrivilegeEscalation: false ##不允许提权。
1.使用dockerfile构建一个镜像,创建一个普通用户qingjun用户,但不指定qingjun用户启用程序。
[root@k8s-master1 ~]# cat dockerfile
FROM python
RUN mkdir /data/www -p
RUN useradd qingjun
COPY . /data/www
RUN pip install flask -i https://mirrors.aliyun.com/pypi/simple/
WORKDIR /data/www
CMD python qingjun.py
##构建镜像

最低0.47元/天 解锁文章
1064

被折叠的 条评论
为什么被折叠?



