Kubernetes实战(二)-使用Kor过滤Kubernetes未使用资源

1 概述

Kor 是一个找到未使用 Kubernetes 资源的工具。目前,Kor 能够识别并列出以下未使用的资源:

  • ConfigMap

  • Secret

  • Service

  • ServiceAccount

  • Deployment

  • StatefulSet

  • Role

  • HPA

  • PVC

  • Ingresse

  • PDB

Kubernetes实战(二)-使用Kor过滤Kubernetes未使用资源_第1张图片

2 安装部署

安装方法

从发布页面下载对应你操作系统的二进制文件,并添加到系统路径中。

自制

对于 macOS 用户,可以通过自制来安装 Kor:

brew install kor

从源码编译

将二进制文件安装到你的 $GOBIN 或 $GOPATH/bin 目录:

go install github.com/yonahd/kor@latest

Docker

运行已经挂载了你的 kubeconfig 的容器:

docker run --rm -i yonahdissen/kor

docker run --rm -i -v "/path/to/.kube/config:/root/.kube/config" yonahdissen/kor all

Helm

在集群中作为一个定时任务运行:

helm upgrade -i kor \
    --namespace kor \
    --create-namespace \
    ./charts/kor

更多集群使用详情请查看:

https://github.com/yonahd/kor#in-cluster-usage

3 使用说明

Kor 提供多种子命令来识别和列出未使用的资源,包括:

  • all:列出指定命名空间或所有命名空间内的所有未使用资源。

  • configmap:列出指定命名空间或所有命名空间内未使用的 ConfigMap。

  • secret:列出指定命名空间或所有命名空间内未使用的 Secret。

  • services:列出指定命名空间或所有命名空间内未使用的 Service。

  • serviceaccount:列出指定命名空间或所有命名空间内未使用的 ServiceAccount。

  • deployments:列出指定命名空间或所有命名空间内未使用的 Deployment。

  • statefulsets:列出指定命名空间或所有命名空间内未使用的 StatefulSet。

  • role:列出指定命名空间或所有命名空间内未使用的 Role。

  • hpa:列出指定命名空间或所有命名空间内未使用的 HPA。

  • pvc:列出指定命名空间或所有命名空间内未使用的 PVC。

  • ingress:列出指定命名空间或所有命名空间内未使用的 Ingresse。

  • pdb:列出指定命名空间或所有命名空间内未使用的 PDB。

  • exporter:导出 Prometheus 指标。

Supported Flags

-e, --exclude-namespaces string   Namespaces to be excluded, split by comma. Example: --exclude-namespace ns1,ns2,ns3. If --include-namespace is set, --exclude-namespaces will be ignored.
-h, --help                        help for kor
-n, --include-namespaces string   Namespaces to run on, split by comma. Example: --include-namespace ns1,ns2,ns3.
-k, --kubeconfig string           Path to kubeconfig file (optional)
    --output string               Output format (table or json) (default "table")
    --slack-auth-token string     Slack auth token to send notifications to. --slack-auth-token requires --slack-channel to be set.
    --slack-channel string        Slack channel to send notifications to. --slack-channel requires --slack-auth-token to be set.
    --slack-webhook-url string    Slack webhook URL to send notifications to

要使用特定子命令,请运行 kor [subcommand] [flags]。

kor all --namespace my-namespace

有关每个子命令及其可用标志的更多信息,你可以使用 --help 标志。

kor [subcommand] --help

4 支持的资源和限制

资源 它寻找什么 已知的误报 ⚠️
ConfigMap

在以下位置未被使用的 ConfigMaps:

1、Pod

2、容器

3、作为 Volumes 的 ConfigMap

4、作为环境变量的 ConfigMap

由资源使用但在配置中未明确声明的 ConfigMaps。

例如:动态加载的 Grafana 仪表板、OPA 策略、fluentd 配置。

Secret

在以下位置未被引用的 Secrets:

1、Pod

2、容器

3、通过 Volumes 使用的 Secret

4、通过环境变量使用的 Secret

5、由 Ingress TLS 使用的 Secret

6、由 ServiceAccounts 使用的 Secret

未在配置中明确说明的资源使用的 Secret
Service Service 没有端点
Deployment Deployment 没有副本
ServiceAccount

1、Pod 未引用的 ServiceAccount

2、未被 roleBinding 或 clusterRoleBinding 引用的 ServiceAccount

StatefulSet StatefulSet 没有副本
Role roleBinding 中未使用的 Role
PVC Pod 未使用 PVC
Ingresse Ingresse 未指向任何 Service
HPA

1、未在 Deployment 中使用的 HPA

2、未在 StatefulSet 中使用的 HPA

PDB

1、未在 Deployment 中使用的 PDB

2、未在 StatefulSet 中使用的 PDB

5 忽略资源

若资源标记为 “kor/used = true”,即便它们未被使用,Kor 也不会将其纳入检测范围。如果你不想让某些资源被 Kor 检测,可以为它们加上这个标签。

6 引入选项

也可以把 Kor 当作一个 Go 语言库,用来程序化地查找未使用的资源。通过引入 github.com/yonahd/kor/pkg/kor 这个包,能够调用对应的功能来查找未使用的资源。此库还支持一个特性,即通过设置 outputFormat 参数,可以让查询结果以 JSON 格式展示。

import (
    "github.com/yonahd/kor/pkg/kor"
)

func main() {
    myNamespaces := kor.IncludeExcludeLists{
        IncludeListStr: "my-namespace1, my-namespace2",
    }
    outputFormat := "json" // Set to "json" for JSON output

    if outputFormat == "json" {
        jsonResponse, err := kor.GetUnusedDeploymentsStructured(myNamespaces, kubeconfig, "json")
        if err != nil {
            // Handle error
        }
        // Process the JSON response
        // ...
    } else {
        kor.GetUnusedDeployments(namespace)
    }
}

7 在集群内使用

若要在集群内部使用这个工具,可以将其设置为一个 CronJob。结果可以发送到 Slack Webhook 作为纯文本(注意,纯文本有 4000 字符的限制),或者推荐的方式是将结果作为文件上传到 Slack 频道。你可以执行以下命令来实现:

# Send to a Slack webhook as raw text
helm upgrade -i kor \
    --namespace kor \
    --create-namespace \
    --set cronJob.slackWebhookUrl= \
    ./charts/kor
# Send to a Slack channel by uploading a file
helm upgrade -i kor \
    --namespace kor \
    --create-namespace \
    --set cronJob.slackChannel= \
    --set cronJob.slackToken= \
    ./charts/kor

注意:要将其作为文件发送到 Slack,需要设置 slackToken 和 slackChannel 值。

它默认设定在每周一的凌晨 1 点执行。你可以通过调整 cronJob.schedule 的值来改变定时任务的执行计划。

helm upgrade -i kor \
    --namespace kor \
    --create-namespace \
    --set cronJob.slackChannel= \
    --set cronJob.slackToken= \
    --set cronJob.schedule="0 1 * * 1" \
    ./charts/kor

你可能感兴趣的:(#,Kubernetes系列,kubernetes,容器,云原生)