Kubernetes 【安全】2. System Hardening - apparmor
文章目錄
- Kube-apparmor-manager
- AppArmorProfile 自定義資源定義
- Apparmor-manager 插件
- Sysdig Secure 構建強大的 Apparmor Profile
現在,想想在生產中實施 AppArmor的挑戰。
首先,您必須為每個容器構建強大的配置文件,以在不阻塞日常任務的情況下防止攻擊。
然后,您將必須跨集群中的所有節點管理多個配置文件。
我們將介紹kube-apparmor-manager如何幫助管理部分,以及Sysdig Secure 中的圖像分析功能如何幫助構建這些配置文件。
Kube-apparmor-manager
有一些工具,比如apparmor-loader,可以幫助管理 Kubernetes 集群中的 AppArmor 配置文件。Apparmor-loader作為特權守護進程運行,輪詢包含 AppArmor 配置文件的配置映射,最后將配置文件解析為強制模式或投訴模式。然而,這引入了遠非理想的特權工作負載。這就是為什么我們可以想出另一種方法。
Kube-apparmor-manager方法不同:
- 它使用自定義資源(apparmorprofiles.crd.security.sysdig.com)將配置文件表示為Kubernetes 對象。
- 一個kubectl插件轉換AppArmorProfile對象,存儲在ETCD,到實際AppArmor配置文件,并同步他們的節點之間。
讓我們詳細看看它們是如何工作的。
AppArmorProfile 自定義資源定義
AppArmorProfile CRD定義了一個架構來將 AppArmor 配置文件表示為 Kubernetes 對象。
這就是我們的示例 AppArmor 配置文件在這種格式下的樣子:
apiVersion: crd.security.sysdig.com/v1alpha1 kind: AppArmorProfile metadata:name: k8s-apparmor-example-deny-write spec:# Add fields hereenforced: truerules: |# read only file pathsfile,deny /** w,該enforced字段指示配置文件處于強制模式還是投訴模式。該字段rules包含帶有白名單或黑名單規則列表的 AppArmor 配置文件正文。
請注意,這是一個集群級別的對象。
Apparmor-manager 插件
在 Kubernetes 集群中安裝 CRD 后,您可以開始使用 kubectl 與 AppArmorProfile 對象進行交互。但是,您仍然需要將 AppArmorProfile 對象中的內容轉換為實際的 AppArmor 配置文件,并將它們分發到所有節點。
這就是apparmor-managerkubectl 插件所做的。
您可以使用krew安裝它:
$ wget https://github.com/kubernetes-sigs/krew/releases/download/v0.4.2/krew-linux_amd64.tar.gz $ tar xzvf krew-linux_amd64.tar.gz $ mv krew-linux_amd64 /usr/local/bin/krew $ krew install apparmor-manager $ krew install krew WARNING: To be able to run kubectl plugins, you need to add the following to your ~/.bash_profile or ~/.bashrc:export PATH="${PATH}:${HOME}/.krew/bin"and restart your shell.Updated the local copy of plugin index. Installing plugin: krew Installed plugin: krew \| Use this plugin:| kubectl krew| Documentation:| https://krew.sigs.k8s.io/| Caveats:| \| | krew is now installed! To start using kubectl plugins, you need to add| | krew's installation directory to your PATH:| | | | * macOS/Linux:| | - Add the following to your ~/.bashrc or ~/.zshrc:| | export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"| | - Restart your shell.| | | | * Windows: Add %USERPROFILE%\.krew\bin to your PATH environment variable| | | | To list krew commands and to get help, run:| | $ kubectl krew| | For a full list of available plugins, run:| | $ kubectl krew search| | | | You can find documentation at| | https://krew.sigs.k8s.io/docs/user-guide/quickstart/.| / /$ echo 'export PATH="${PATH}:${HOME}/.krew/bin"' >> /root/.bashrc $ kubectl krew install apparmor-manager當apparmor-manager通過 SSH 與工作節點通信時,您需要設置一些環境變量:
- SSH_USERNAME: SSH 用戶名以訪問工作節點。默認為admin.
- SSH_PERM_FILE:用于訪問工作節點的 SSH 私鑰。默認為$HOME/.ssh/id_rsa.
- SSH_PASSPHRASE: SSH 密碼(僅當私鑰受密碼保護時才適用)。
如果節點上沒有安裝 AppArmor,apparmor-manager 可以幫助您使用以下命令在工作節點上啟用 AppArmor:
$ kubectl apparmor-manager init該init命令還將為您安裝 CRD。
配置完所有內容后,您可以使用以下命令檢查節點上 AppArmor 的狀態:
$ kubectl apparmor-manager status +-------------------------------+---------------+----------------+--------+------------------+ | NODE NAME | INTERNAL IP | EXTERNAL IP | ROLE | APPARMOR ENABLED | +-------------------------------+---------------+----------------+--------+------------------+ | ip-172-20-45-132.ec2.internal | 172.20.45.132 | 54.91.xxx.xx | master | false | | ip-172-20-54-2.ec2.internal | 172.20.54.2 | 54.82.xx.xx | node | true | | ip-172-20-58-7.ec2.internal | 172.20.58.7 | 18.212.xxx.xxx | node | true | +-------------------------------+---------------+----------------+--------+------------------+您還可以使用 kubectl 創建您的第一個 AppArmorProfile 對象:
$ kubectl apply -f deny-write.yaml apparmorprofile.crd.security.sysdig.com/k8s-apparmor-example-deny-write created $ kubectl get aap NAME AGE k8s-apparmor-example-deny-write 5s創建后,您需要將 AppArmorProfiles 同步到工作節點:
$ kubectl apparmor-manager enforced +-------------------------------+--------+---------------------------------------------------------------+ | NODE NAME | ROLE | ENFORCED PROFILES | +-------------------------------+--------+---------------------------------------------------------------+ | ip-172-20-48-62.ec2.internal | node | /usr/sbin/ntpd,docker-default,k8s-apparmor-example-deny-write | | ip-172-20-77-231.ec2.internal | node | /usr/sbin/ntpd,docker-default,k8s-apparmor-example-deny-write | | ip-172-20-80-19.ec2.internal | master | | | ip-172-20-97-60.ec2.internal | node | /usr/sbin/ntpd,docker-default,k8s-apparmor-example-deny-write | +-------------------------------+--------+---------------------------------------------------------------+這k8s-apparmor-example-deny-write是我們剛剛創建并同步的一個,而另外兩個默認安裝在 AppArmor 中。
最后一步是配置 Pod 以使用此配置文件,使用annotations我們之前看到的。
接下來,我們來談談如何使用 Sysdig Secure 構建健壯的 AppArmor 配置文件。
Sysdig Secure 構建強大的 Apparmor Profile
借助圖像分析,Sysdig Secure 將分析容器 24 小時,了解預期的進程、文件系統活動、網絡行為和系統調用。有了這些知識,您可以生成學習的映像配置文件,并使用它來創建運行時策略,以保護容器免受生產中的異常行為的影響。
總結
以上是生活随笔為你收集整理的Kubernetes 【安全】2. System Hardening - apparmor的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Adruino单片机的自动开关垃圾桶
- 下一篇: ADRV9009 与 zcu102搭建A