使用 KubeEye 为你的 K8s 集群安全保驾护航
其他 2022-04-24 18:51:30 阅读次数: 0
作者:KaliArch(薛磊),某Cloud MSP服务商产品负责人,熟悉企业级高可用/高并发架构,包括混閤云架构、异地灾,熟练企业DevOPS改造优化,熟悉Shell/Python/Go等开发语言,熟悉Kubernetes、 Docker、 云原生、微服务架构等。
前言
KubeEye 是一款 Kubernetes 安全及配置问题检测工具,针对部署在 K8s 集群中的业务应用进行配置检测使用 OPA,针对集群部署的 Node 使用Node-Problem-Detector进行检测,同时除了係统内置有根据大多数业界常见场景的预定义规则,还支持用户自定义规则来进行集群检测。
架构
KubeEye 通过调用 Kubernetes API,通过匹配资源中的关键字和容器语法的规则匹配来获取集群诊断数据,详见架构图。
其中针对 Node 节点的检测,需要在被检测 Node 主机上安装。
特点
特性
KubeEye 根据行业最佳实践审查你的工作负载 YAML 规範,帮助你使你的集群稳定。KubeEye 可以发现你的集群控製平麵的问题,包括 kube-apiserver/kube-controller-manager/etcd 等。KubeEye 可以帮助你检测各种节点问题,包括内存/CPU/磁盘压力,意外的内核错误日誌等。检查项
部署
KubeEye 本身使用 Golang 编写,可使用编译好的二进製可执行文件进行相关组件安装。
免费v2ray节点
安装
二进製安装
wget https://github.com/kubesphere/kubeeye/releases/download/v0.3.0/kubeeye-0.3.0-linux-amd64.tar.gztar -zxvf kubeeye-0.3.0-linux-amd64.tar.gzmv kubeeye /usr/bin/
源码编译安装
git clone https://github.com/kubesphere/kubeeye.gitcd kubeeye make installke
安装 NPD
针对集群 Node 主机的检测,kubeEye 採用Node-problem-Detector ,需要在 Node 主机节点进行安装,KubeEye 封装安装命令,可以进行一键安装。
⚠️注意:这将在你的集群上安装 NPD,只有当你想要详细的节点报告时才需要。
[root@VM-48-7-centos ~]# kubeeye install -e npdkube-system ConfigMap node-problem-detector-config createdkube-system DaemonSet node-problem-detector created
其主要在 kube-system 名称空间创建 node-problem-detector-config 的 ConfigMap 和 node-problem-detector DaemonSet。
集群中运行 KubeEye
kubeEye 除了可以一次性使用工具运行,同时 kubeEye 也是一个 Operator,可以运行在集群内部,进行长久的持续对集群进行检测。
Kubernetes 中部署 KubeEye
kubectl apply -f https://raw.githubusercontent.com/kubesphere/kubeeye/main/deploy/kubeeye.yamlkubectl apply -f https://raw.githubusercontent.com/kubesphere/kubeeye/main/deploy/kubeeye_insights.yaml
查看 KubeEye 巡检结果
$ kubectl get clusterinsight -o yamlapiVersion: v1items:- 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
测试
命令选项
[root@VM-48-7-centos ~]# kubeeye -hKubeEye 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 commandFlags: -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,对集群应用进行配置扫描。
audit
[root@VM-48-7-centos ~]# kubeeye auditKIND NAMESPACE NAME MESSAGEDeployment 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]
添加自定义检查规则
我们利用命令查看预定义 OPA 检查规则。
kubectl get cm -n kube-system node-problem-detector-config -oyaml
同时也可以根据自己业务创建自定义检查规则。
创建 OPA 规则存放目录mkdir opa
添加自定义 OPA 规则文件以下为检查镜像仓库地址规则,保存以下规则到规则文件 imageRegistryRule.rego注意:为检查工作负载设置的 OPA 规则, Package 名称必须是 kubeeye_workloads_rego 为检查 RBAC 设置的 OPA 规则, Package 名称必须是 kubeeye_RBAC_rego 为检查节点设置的 OPA 规则, Package 名称必须是 kubeeye_nodes_rego
package kubeeye_workloads_regodeny[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。kubEye 安装使用默认使用主机$HOME/.kube/config
文件,如果不存在 K8s config 文件,则无法正常运行。