作者: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/磁盘压力,意外的内核谬误日志等。

查看项

是/否查看项形容级别
PrivilegeEscalationAllowed容许特权降级紧急
CanImpersonateUserrole/clusterrole 有伪装成其余用户权限正告
CanDeleteResourcesrole/clusterrole 有删除 Kubernetes 资源权限正告
CanModifyWorkloadsrole/clusterrole 有批改 Kubernetes 资源权限正告
NoCPULimits资源没有设置 CPU 应用限度紧急
NoCPURequests资源没有设置预留 CPU紧急
HighRiskCapabilities开启了高危性能,例如 ALL/SYS_ADMIN/NET_ADMIN紧急
HostIPCAllowed开启了主机 IPC紧急
HostNetworkAllowed开启了主机网络紧急
HostPIDAllowed开启了主机PID紧急
HostPortAllowed开启了主机端口紧急
ImagePullPolicyNotAlways镜像拉取策略不是 always正告
ImageTagIsLatest镜像标签是 latest正告
ImageTagMiss镜像没有标签紧急
InsecureCapabilities开启了不平安的性能,例如 KILL/SYS_CHROOT/CHOWN正告
NoLivenessProbe没有设置存活状态查看正告
NoMemoryLimits资源没有设置内存应用限度紧急
NoMemoryRequests资源没有设置预留内存紧急
NoPriorityClassName没有设置资源调度优先级告诉
PrivilegedAllowed以特权模式运行资源紧急
NoReadinessProbe没有设置就绪状态查看正告
NotReadOnlyRootFilesystem没有设置根文件系统为只读正告
NotRunAsNonRoot没有设置禁止以 root 用户启动过程正告
CertificateExpiredPeriod将查看 API Server 证书的到期日期少于30天紧急
EventAudit事件查看正告
NodeStatus节点状态查看正告
DockerStatusDocker 状态查看正告
KubeletStatusKubelet 状态查看正告

部署

KubeEye 自身应用 Golang 编写,可应用编译好的二进制可执行文件进行相干组件装置。

装置

二进制装置

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 规定文件
留神:为查看工作负载设置的 OPA 规定, Package 名称必须是 kubeeye_workloads_rego 为查看 RBAC 设置的 OPA 规定, Package 名称必须是 kubeeye_RBAC_rego 为查看节点设置的 OPA 规定, Package 名称必须是 kubeeye_nodes_rego
  • 以下为查看镜像仓库地址规定,保留以下规定到规定文件 imageRegistryRule.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 文件,则无奈失常运行。

参考链接

  • https://github.com/kubesphere...

    本文由博客一文多发平台 OpenWrite 公布!