使用 KubeLinter 分析 Kubernetes 文件中的错误

使用 KubeLinter 查找并修复 Helm charts 和 Kubernetes 配置文件中的错误。
81 位读者喜欢这篇文章。
magnifying glass on computer screen, finding a bug in the code

Opensource.com

KubeLinter 是 Stackrox 发布的开源项目,用于分析 Kubernetes YAML 文件中的安全问题和错误代码。该工具涵盖 Helm charts 和 Kubernetes 配置文件,包括 Knative 文件。使用它可以改进云原生开发,缩短开发时间,并鼓励 DevOps 最佳实践。

下载和安装

在本教程中,我使用了 Pop_OS! 20.10、Helm 3、Go 1.13.8 和 Kubernetes 1.19 的 Minikube。

您有多种安装 KubeLinter 的选项。

您可以从 Git 存储库手动安装

$ git clone git@github.com:stackrox/kube-linter.git
$ cd kube-linter && make build
$ .gobin/kube-linter version

如果您使用 Homebrew,则可以使用 brew 命令安装它

$ brew install kube-linter

您也可以使用 Go 安装它(我就是这样做的)

$ GO111MODULE=on go get golang.stackrox.io/kube-linter/cmd/kube-linter
go: finding golang.stackrox.io/kube-linter latest
go: downloading golang.stackrox.io/kube-linter v0.0.0-20201204022312-475075c74675
go: extracting golang.stackrox.io/kube-linter v0.0.0-20201204022312-475075c74675
[...]

安装后,您必须在 ~/.bashrc 中创建一个别名

$ echo "alias kube-linter=$HOME/go/bin/kube-linter" >> ~/.bashrc
$ source ~/.bashrc

Helm 与 KubeLinter

现在工具已安装,请在 Helm chart 上试用它。首先,使用干净的构建和一些小的配置更改启动 Minikube

$ minikube config set kubernetes-version v1.19.0
$ minikube config set memory 8000
❗  These changes will take effect upon a minikube delete and then a minikube start
$ minikube config set cpus 12
❗  These changes will take effect upon a minikube delete and then a minikube start
$ minikube delete
?  Deleting "minikube" in docker ...
?  Deleting container "minikube" ...
?  Removing /home/jess/.minikube/machines/minikube ...
?  Removed all traces of the "minikube" cluster.

$ minikube start
?  minikube v1.14.2 on Debian bullseye/sid
✨  Using the docker driver based on user configuration
?  Starting control plane node minikube in cluster minikube
?  minikube 1.15.1 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.15.1
?  To disable this notice, run: 'minikube config set WantUpdateNotification false'

?  Downloading Kubernetes v1.19.0 preload ...

一切运行后,创建一个名为 first_test 的示例 Helm chart

$ helm create first_test
Creating first_test
$ ls
first_test

针对新的、未经编辑的 chart 测试 KubeLinter。运行 kube-linter 命令以查看可用的命令和标志

$ kube-linter
Usage:
  /home/jess/go/bin/kube-linter [command]

Available Commands:
  checks      View more information on lint checks
  help        Help about any command
  lint        Lint Kubernetes YAML files and Helm charts
  templates   View more information on check templates
  version     Print version and exit

Flags:
  -h, --help   help for /home/jess/go/bin/kube-linter

Use "/home/jess/go/bin/kube-linter [command] --help" for more information about a command.

然后测试基本的 lint 命令对您的示例 chart 的作用。您最终会遇到许多错误,所以我将抓取一些问题的片段

$ kube-linter lint first_test/

first_test/first_test/templates/deployment.yaml: (object: <no namespace>/test-release-first_test apps/v1, Kind=Deployment) container "first_test" does not have a read-only root file system (check: no-read-only-root-fs, remediation: Set readOnlyRootFilesystem to true in your container's securityContext.)

first_test/first_test/templates/deployment.yaml: (object: <no namespace>/test-release-first_test apps/v1, Kind=Deployment) container "first_test" is not set to runAsNonRoot (check: run-as-non-root, remediation: Set runAsUser to a non-zero number, and runAsNonRoot to true, in your pod or container securityContext. See https://kubernetes.ac.cn/docs/tasks/configure-pod-container/security-context/ for more details.)
[...]
Error: found 12 lint errors

为了简洁起见,我选择了两个易于我修复的安全问题。随着时间的推移,随着您进行更多测试,您将能够修复发现的任何问题。

kube-linter 输出提供了有关所需修复的提示。例如,第一个错误以以下内容结尾

remediation: Set readOnlyRootFilesystem to true in your container's securityContext.

下一步很明确:在文本编辑器中打开 values.yaml 文件(我使用 Vi,但您可以使用任何您喜欢的编辑器)并找到 securityContext 部分

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

取消注释该部分并删除大括号

securityContext: 
   capabilities:
     drop:
     - ALL
   readOnlyRootFilesystem: true
   runAsNonRoot: true
   runAsUser: 1000

保存文件并重新运行 linter。这些错误不再显示在列表中,并且错误计数会发生变化。

Error: found 10 lint errors

恭喜!您已解决安全问题!

Kubernetes 与 KubeLinter

此示例使用来自我之前关于 Knative 的文章中的应用程序文件来针对 Kubernetes 配置文件进行测试。我已经启动并运行了 Knative,因此如果您的系统上没有运行 Knative,您可能需要查看该文章。

我下载了此示例的 Kourier 服务 YAML 文件

$ ls
kourier.yaml   first_test

首先针对 kourier.yaml 运行 linter。同样,存在一些问题。我将重点关注资源问题

$ kube-linter lint kourier.yaml 

kourier.yaml: (object: kourier-system/3scale-kourier-gateway apps/v1, Kind=Deployment) container "kourier-gateway" has cpu limit 0 (check: unset-cpu-requirements, remediation: Set your container's CPU requests and limits depending on its requirements. See https://kubernetes.ac.cn/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for more details.)

kourier.yaml: (object: kourier-system/3scale-kourier-gateway apps/v1, Kind=Deployment) container "kourier-gateway" has memory request 0 (check: unset-memory-requirements, remediation: Set your container's memory requests and limits depending on its requirements. See https://kubernetes.ac.cn/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for more details.)

Error: found 12 lint errors

由于这是一个单一部署文件,您可以直接编辑它。在文本编辑器中打开它并更改文件中的值。该文件很长,所以我只包含需要更改的部分。

从部署开始

apiVersion: apps/v1
kind: Deployment
metadata:
  name: 3scale-kourier-gateway
  namespace: kourier-system
  labels:
    networking.knative.dev/ingress-provider: kourier
[...]

容器部分存在一些问题

    spec:
      containers:
      - args:
        - --base-id 1
        - -c /tmp/config/envoy-bootstrap.yaml
        - --log-level info
        command:
        - /usr/local/bin/envoy
        image: docker.io/maistra/proxyv2-ubi8:1.1.5
        imagePullPolicy: Always
        name: kourier-gateway
        ports:
        - name: http2-external
          containerPort: 8080
          protocol: TCP
        - name: http2-internal
          containerPort: 8081
          protocol: TCP
        - name: https-external
          containerPort: 8443
          protocol: TCP

向容器配置添加一些规范

    spec:
      containers:
      - args:
        - --base-id 1
        - -c /tmp/config/envoy-bootstrap.yaml
        - --log-level info
        command:
        - /usr/local/bin/envoy
        image: docker.io/maistra/proxyv2-ubi8:1.1.5
        imagePullPolicy: Always
        name: kourier-gateway
        ports:
        - name: http2-external
          containerPort: 8080
          protocol: TCP
        - name: http2-internal
          containerPort: 8081
          protocol: TCP
        - name: https-external
          containerPort: 8443
          protocol: TCP
 resources:
    limits:
      cpu: 100m
      memory: 128Mi
    requests:
      cpu: 100m
      memory: 128Mi

当您重新运行 linter 时,您会注意到这些问题不再显示在输出中,并且错误计数会发生变化

Error: found 8 lint errors

恭喜!您已修复 Kubernetes 文件中的资源问题!

最后的想法

KubeLinter 是一个很棒的工具,也是启动新的 DevOps 流程以保护和资源管理您的所有 Kubernetes 和应用程序配置的好机会。将此功能添加到自动化测试步骤中可以提升您的环境和 DevOps 周期。

我认为 KubeLinter 最好的部分是每个错误消息都包含文档,因此即使您不知道错误 linting 输出的含义,文档也可以帮助您学习和提前计划。我推荐这个工具用于日常使用和处理追溯代码。

接下来阅读什么
User profile image.
技术游民,从事我能找到的任何工作。IT 领域孤岛预防、与所有团队共享信息的重要性倡导者。坚信教育所有人以及开源开发。热爱所有技术。热衷于 K8s、混沌以及我能找到的任何新事物和闪亮的东西!Mastodon ID

评论已关闭。

Creative Commons License本作品根据 Creative Commons Attribution-Share Alike 4.0 International License 获得许可。
© . All rights reserved.