k8s安全07--使用AppArmor限制容器访问资源

1 介绍

AppArmor is a kernel enhancement to confine programs to a limited set of resources. AppArmor’s unique security model is to bind access control attributes to programs rather than to users.
AppArmor confinement is provided via profiles loaded into the kernel via apparmor_parser(8), typically through the /etc/init.d/apparmor SysV initscript, which is used like this: /etc/init.d/apparmor start
| stop | restart

2 操作案例

2.1 apparmor 基础命令

查看使用方法
# man apparmor

查看
# apparmor_status | grep your_
# cd /etc/apparmor.d
# apparmor_parser -q your_profile_file
# apparmor_status | grep your_profile_name

2.2 通过AppArmor 限制pod访问的资源

1 创建 profile

cd /etc/apparmor.d
vim k8s-apparmor-example-deny-write

#include <tunables/global>

profile k8s-apparmor-example-deny-write flags=(attach_disconnected) {
  #include <abstractions/base>

  file,

  # Deny all file writes.
  deny /** w,
}

2 启动 profile

# apparmor_parser k8s-apparmor-example-deny-write
# apparmor_status |grep k8s
   k8s-apparmor-example-deny-write

3 新增pod配置

$ wget https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/security/hello-apparmor.yaml

$ cat hello-apparmor.yaml
apiVersion: v1
kind: Pod
metadata:
  name: hello-apparmor
  annotations:
    container.apparmor.security.beta.kubernetes.io/hello: localhost/k8s-apparmor-example-deny-write
spec:
  containers:
  - name: hello
    image: busybox
    command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ]

$ kubectl apply -f hello-apparmor.yaml
pod/hello-apparmor created

在这里插入图片描述
4 测试deny write 配置

$ kubectl exec hello-apparmor -- cat /proc/1/attr/current
k8s-apparmor-example-deny-write (enforce)
$ kubectl exec hello-apparmor -- touch /apparmor.txt
touch: /apparmor.txt: Permission denied
command terminated with exit code 1

在这里插入图片描述

3 注意事项

  1. 更多使用说明请参考 man apparmor
  2. k8s 中使用格式说明
    container.apparmor.security.beta.kubernetes.io/<container_name>: <profile_ref>
    
    container_name 对应的是具体的容器名称,可以直接填写pod名称;
    profile_ref一般为 localhost/k8s-apparmor-example-deny-write(/etc/apparmor.d 下的profile 名称)
    
    由于不确定pod为在哪个节点上,因此需要将profile配置到所有节点上,并启动该配置
    

4 说明

Restrict a Container’s Access to Resources with AppArmor
docs/tutorials/clusters/apparmor/
docs.docker.com/engine/security/apparmor/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昕光xg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值