顾名思义,Bulk Action
是一个用于进行批量操作的插件。
它使用 kubectl bulk <resourceType> [<parameters>] [action] [<fields>]
形式的命令,对指定资源的字段进行批量操作。
这个插件是使用 Shell 脚本通过大量的
grep
、sed
完成操作的,对完整资源并无认识,因此操作还是具有一定的危险性的,请慎重使用写操作。
这个命令用于读取特定对象的指定字段值。
例如获取所有 Deployment 中的特定字段的内容:
$ kubectl bulk-action deploy get image
image fields are getting
image: dustise/flaskapp
image: dustise/flaskapp
- image: dustise/sleep
- image: dustise/sleep
上面的输出内容中,也能很清晰的分辨出 sh
的操作痕迹。推测 labels
这种字段就无法获取了:
$ kubectl bulk-action deploy get labels
labels fields are getting
labels:
labels:
...
这个命令是 bulk-action
的缺省操作,用 yaml 的形式列出所有指定类型的资源。
根据目前的资源,经过一定的替换之后生成新资源。
例如复制一个 Deployment
:
$ kubectl bulk-action deploy create name sleep-v2 sleep-v3
creating new resource with changing name: sleep-v2 to name: sleep-v3 for all deploy
deployment.extensions/sleep-v3 created
Error from server (AlreadyExists): deployments.extensions "flaskapp-v1" already exists
Error from server (AlreadyExists): deployments.extensions "flaskapp-v2" already exists
Error from server (AlreadyExists): deployments.extensions "sleep-v1" already exists
前面说过,这个工具的操作是通过 grep
sed
等完成的,因此这里的情况就比较有趣:sleep-v2
被替换生成新的 Deployment
,其它 Deployment
不包含这段文本,因此无效。
选择指定资源执行删除操作,例如删除所有的 Service
。
这个操作相当危险。 这里有个 bug,可以
mv ~/.krew/bin/kubectl-bulk_action ~/.krew/bin/kubectl-bulk
来纠正。
和 Create
类似,不过这里调用的是 kubectl replace
古怪的功能,危险的操作。非常不建议采用。
使用 kubesec.io 提供的功能对集群进行扫描,得到安全相关的问题以及对应的建议。
kubectl kubesec-scan [resource-type] [-n namespace] <resource-name>
例如:
$ kubectl kubesec-scan daemonset -n kube-system cilium
scanning daemonset cilium in namespace kube-system
kubesec.io score: -44
-----------------
Critical
1. containers[] .securityContext .privileged == true
Privileged containers can allow almost completely unrestricted host access
2. .spec .hostNetwork
Sharing the host's network namespace permits processes in the pod to communicate with processes bound to the host's loopback adapter
3. .spec .volumes[] .hostPath .path == "/var/run/docker.sock"
Mounting the docker.socket leaks information about other containers and can allow container breakout
-----------------
Advise1. containers[] .securityContext .runAsNonRoot == true
Force the running image to run as a non-root user to ensure least privilege
2. containers[] .securityContext .capabilities .drop
Reducing kernel capabilities available to a container limits its attack surface
3. containers[] .securityContext .readOnlyRootFilesystem == true
An immutable root filesystem can prevent malicious binaries being added to PATH and increase attack cost
4. containers[] .securityContext .runAsUser > 10000
Run as a high-UID user to avoid conflicts with the host's user table
5. containers[] .securityContext .capabilities .drop | index("ALL")
Drop all capabilities and add only those required to reduce syscall attack surface
https://kubesec.io/
https://github.com/emreodabas/kubectl-plugins