AppArmor
AppArmor
【简介】
apparmor 是linux内核安全功能,可用于限制容器对资源的使用。 例如:容器使用linux内核能力,容器使用网络能力,容器对文件系统读写权限 等。
apparmor一条规则称为一个profile。profile可以 enforcing 或 complain模式运行。
- enforcing 可以理解为强制模式,需要容器必须符合规则
- complain 可以理解为兼容模式,当容器不符合规范时仅仅打印提示信息
【前提条件】
-
操作系统:Ubuntu 22.04
-
内核支持
cat /sys/module/apparmor/parameters/enabled Y -
k8s>1.4版本,截止2023年功能仍处于beta阶段。
【示例】
-
编写apparmor插件,暂时不做深入研究语法
tee test<<EOF #include <tunables/global> profile k8s-apparmor-example-deny-write flags=(attach_disconnected) { #include <abstractions/base> file, # 拒绝所有文件写入 deny /** w, } EOF -
加载插件
# 加载配置文件到内核 apparmor_parser -q test # 查看是否加载成功 # grep k8s-apparmor-example-deny-write /sys/kernel/security/apparmor/profiles root@master01:~# apparmor_status |grep k8s-apparmor-example-deny-write k8s-apparmor-example-deny-write -
创建pod并使用该apparmor插件
通过注解加载apparmor功能,注解格式如下:
container.apparmor.security.beta.kubernetes.io/<容器名称>: <profile_ref>
<profile_ref>
可以是以下取值之一:
1.runtime/default
应用运行时的默认配置
2.localhost/<profile_name>
使用本地的profile插件
3.unconfined
表示不加载配置文件kubectl run busybox \ --image=busybox \ --annotations=container.apparmor.security.beta.kubernetes.io/busybox=localhost/k8s-apparmor-example-deny-write \ -- sleep 100 -
验证功能是否生效
测试预期:无法写入文件
root@master01:~# kubectl exec busybox -- cat /proc/1/attr/current k8s-apparmor-example-deny-write (enforce) root@master01:~# kubectl exec busybox -- touch /tmp/1 touch: /tmp/1: Permission denied command terminated with exit code 1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏