Kubernetes安全之KubeEye

网友投稿 739 2022-05-29

一 前言

kubeEye 是一款 Kubernetes 安全及配置问题检测工具,针对部署在 K8s 集群中的业务应用进行配置检测使用 OPA,针对集群部署的 Node 使用Node-Problem-Detector进行检测,同时除了系统内置有根据大多数业界常见场景的预定义规则,还支持用户自定义规则来进行集群检测。

二 架构

KubeEye 通过调用 Kubernetes API,通过匹配资源中的关键字和容器语法的规则匹配来获取集群诊断数据,详见架构图。

其中针对 Node 节点检测,需要在被检测 Node 主机上安装

三 特点

3.1 特性

KubeEye 根据行业最佳实践审查你的工作负载 yaml 规范,帮助你使你的集群稳定。

KubeEye 可以发现你的集群控制平面的问题,包括 kube-apiserver/kube-controller-manager/etcd 等。

KubeEye 可以帮助你检测各种节点问题,包括内存/CPU/磁盘压力,意外的内核错误日志等。

3.2 检查项

四 部署

kubeEye 本身使用 Golang 编写,可使用编译好的二进制可执行文件进行相关组件安装。

4.1 安装

wget https://github.com/kubesphere/kubeeye/releases/download/v0.3.0/kubeeye-0.3.0-linux-amd64.tar.gz tar -zxvf kubeeye-0.3.0-linux-amd64.tar.gz mv kubeeye /usr/bin/

git clone https://github.com/kubesphere/kubeeye.git cd kubeeye make installke

4.2 安装NPD

针对集群 Node 主机的检测,kubeEye 采用Node-problem-Detector ,需要在 Node 主机节点进行安装,kubeeye 封装安装命令,可以进行一键安装。

⚠️注意:这将在你的集群上安装 npd,只有当你想要详细的节点报告时才需要。

[root@VM-48-7-centos ~]# kubeeye install -e npd kube-system ConfigMap node-problem-detector-config created kube-system DaemonSet node-problem-detector created

其主要在 kube-system 名称空间创建 node-problem-detector-config 的 ConfigMap 和 node-problem-detector DaemonSet。

4.3 集群中运行kubeEye

kubeEye 除了可以一次性使用工具运行,同时 kubeEye 也是一个 Operator,可以运行在集群内部,进行长久的持续对集群进行检测。

kubectl apply -f https://raw.githubusercontent.com/kubesphere/kubeeye/main/deploy/kubeeye.yaml kubectl apply -f https://raw.githubusercontent.com/kubesphere/kubeeye/main/deploy/kubeeye_insights.yaml

$ kubectl get clusterinsight -o yaml apiVersion: v1 items: - apiVersion: kubeeye.kubesphere.io/v1alpha1 kind: ClusterInsight metadata: name: clusterinsight-sample namespace: default spec: auditPeriod: 24h status: auditResults: auditResults: - resourcesType: Node resultInfos: - namespace: "" resourceInfos: - items: - level: waring message: KubeletHasNoSufficientMemory reason: kubelet has no sufficient memory available - level: waring message: KubeletHasNoSufficientPID reason: kubelet has no sufficient PID available - level: waring message: KubeletHasDiskPressure reason: kubelet has disk pressure name: kubeeyeNode

五 测试

5.1 命令选项

[root@VM-48-7-centos ~]# kubeeye -h KubeEye finds various problems on Kubernetes cluster. Usage: ke [command] Available Commands: audit audit resources from the cluster completion generate the autocompletion script for the specified shell help Help about any command install A brief description of your command uninstall A brief description of your command Flags: -f, --config string Specify the path of kubeconfig. -h, --help help for ke --kubeconfig string Paths to a kubeconfig. Only required if out-of-cluster. --master --kubeconfig (Deprecated: switch to --kubeconfig) The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.

可以看到 kubeeye 目前主要支持两个命令,一个为 install package 例如 NPD,另外一个执行 audit,对集群应用进行配置扫描。

5.2 audit

[root@VM-48-7-centos ~]# kubeeye audit KIND NAMESPACE NAME MESSAGE Deployment dddd jenkins-1644220286 [NoCPULimits ImagePullPolicyNotAlways NoMemoryLimits NoPriorityClassName NotReadOnlyRootFilesystem NotRunAsNonRoot] Deployment jenkins jenkins-1644220286 [NoCPULimits ImagePullPolicyNotAlways NoMemoryLimits NoPriorityClassName NotReadOnlyRootFilesystem NotRunAsNonRoot] Deployment smartkm-api-k8s velero [ImageTagIsLatest NoLivenessProbe NoPriorityClassName NotReadOnlyRootFilesystem NoReadinessProbe NotRunAsNonRoot] DaemonSet smartkm-api-k8s restic [ImageTagIsLatest NoLivenessProbe NoPriorityClassName NotReadOnlyRootFilesystem NoReadinessProbe NotRunAsNonRoot] Node minikube [KernelHasNoDeadlock FilesystemIsNotReadOnly KubeletHasSufficientMemory KubeletHasNoDiskPressure KubeletHasSufficientPID] Event kube-system node-problem-detector-dmsws.16d844532f662318 [Failed to pull image "k8s.gcr.io/node-problem-detector/node-problem-detector:v0.8.7": rpc error: code = Unknown desc = Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)] Event kube-system node-problem-detector-dmsws.16d844532f66703e [Error: ErrImagePull] Event kube-system node-problem-detector-dmsws.16d84453351b8b19 [Error: ImagePullBackOff]

5.3 添加自定义检查规则

我们利用命令查看预定义 OPA 检查规则。

kubectl get cm -n kube-system node-problem-detector-config -oyaml

同时也可以根据自己业务创建自定义检查规则。

创建 OPA 规则存放目录

mkdir opa

添加自定义 OPA 规则文件

注意:为检查工作负载设置的 OPA 规则, package 名称必须是 kubeeye_workloads_rego 为检查 RBAC 设置的 OPA 规则, package 名称必须是 kubeeye_RBAC_rego 为检查节点设置的 OPA 规则, package 名称必须是 kubeeye_nodes_rego

以下为检查镜像仓库地址规则,保存以下规则到规则文件 imageRegistryRule.rego

package kubeeye_workloads_rego deny[msg] { resource := input type := resource.Object.kind resourcename := resource.Object.metadata.name resourcenamespace := resource.Object.metadata.namespace workloadsType := {"Deployment","ReplicaSet","DaemonSet","StatefulSet","Job"} workloadsType[type] not workloadsImageRegistryRule(resource) msg := { "Name": sprintf("%v", [resourcename]), "Namespace": sprintf("%v", [resourcenamespace]), "Type": sprintf("%v", [type]), "Message": "ImageRegistryNotmyregistry" } } workloadsImageRegistryRule(resource) { regex.match("^myregistry.public.kubesphere/basic/.+", resource.Object.spec.template.spec.containers[_].image) }

使用额外的规则运行 kubeeye

提示:kubeeye 将读取指定目录下所有 .rego 结尾的文件

kubeeye audit -p ./opa

六 问题排查

NPD 安装异常,默认使用 k8s.gcr.io,如果安装服务器无法连通公网可使用我的镜像仓库:1832990/node-problem-detector:v0.8.7。

Kubernetes安全之KubeEye

kubEye 安装使用默认使用主机$HOME/.kube/config 文件,如果不存在 K8s config 文件,则无法正常运行。

参考链接

https://github.com/kubesphere/kubeeye/

Kubernetes 镜像服务

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何使用 Wireshark Tshark 指定文件,时间,缓冲区捕获限制
下一篇:ActiveMQ介绍及安装
相关文章