公众号关注 「奇妙的 Linux 世界」

设为「星标」,每天带你玩转 Linux !

在 Kubernetes 的世界中,我们使用 YAML 文件,对其进行部署以创建各种 Kubernetes 对象,但挑战在于编写它们时是否遵循最佳实践?我们使用的是正确的标准配置集吗?在部署应用程序甚至 Helm 图表之前,可以检查 YAML 吗?所有这些问题的答案都是肯定的,我们可以。2020年10月28日,StackRox 引入了一个名为 KubeLinter 的新开源工具,旨在识别 YAML 文件中的任何错误配置。

根据定义,KubeLinter 是一个静态分析工具,用于检查 Kubernetes YAML 文件和 Helm 图表,以确保其中所代表的应用程序遵循最佳实践。将 YAML 文件提供给该工具后,它将通过内置检查运行,然后详细报告任何错误以及解决这些错误的补救措施。关于此工具的最好之处在于它是可配置和可扩展的:可以启用或禁用内置检查,并且您可以定义和使用自己的自定义检查。

用法

我将在 Mac 上安装 KubeLinter,但只需下载适用于您操作系统的发行版,即可将相同的说明用于Linux。

你可以从https://github.com/stackrox/kube-linter/releases/下载 KubeLinter CLI,如下所示:

$ curl -LO https://github.com/stackrox/kube-linter/releases/download/0.1.1/kube-linter-darwin.zip
$ unzip kube-linter-darwin.zip
$ mv kube-linter /usr/local/bin#check if it's working
$ kube-linter version
0.1.1

现在,为了简单地检查单个 YAML 文件,只需提供 YAML 文件名。假设 deploy.yaml 您要检查以下文件,以检查其当前目录中保存的最佳安全性和配置做法:

apiVersion: apps/v1
kind: Deployment
metadata:name: portainernamespace: portainerlabels:io.portainer.kubernetes.application.stack: portainerapp.kubernetes.io/name: portainerapp.kubernetes.io/instance: portainerapp.kubernetes.io/version: "2.0.0"
spec:replicas: 1strategy:type: "Recreate"selector:matchLabels:app.kubernetes.io/name: portainerapp.kubernetes.io/instance: portainertemplate:metadata:labels:app.kubernetes.io/name: portainerapp.kubernetes.io/instance: portainerspec:serviceAccountName: portainer-sa-clusteradminvolumes:- name: "data"persistentVolumeClaim:claimName: portainercontainers:- name: portainerimage: "portainer/portainer-ce:latest"imagePullPolicy: IfNotPresentargs:  [ '--tunnel-port','30776' ]volumeMounts:- name: datamountPath: /dataports:- name: httpcontainerPort: 9000protocol: TCP- name: tcp-edgecontainerPort: 8000protocol: TCPlivenessProbe:httpGet:path: /port: 9000readinessProbe:httpGet:path: /port: 9000resources:{}

让我们进行测试 kube-linter lint deploy.yaml。我们应该得到如下输出:

deploy.yaml: (object: portainer/portainer apps/v1, Kind=Deployment) container "portainer" 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.)deploy.yaml: (object: portainer/portainer apps/v1, Kind=Deployment) serviceAccount "portainer-sa-clusteradmin" not found (check: non-existent-service-account, remediation: Make sure to create the service account, or to refer to an existing service account.)deploy.yaml: (object: portainer/portainer apps/v1, Kind=Deployment) container "portainer" 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.io/docs/tasks/configure-pod-container/security-context/ for more details.)deploy.yaml: (object: portainer/portainer apps/v1, Kind=Deployment) container "portainer" has cpu request 0 (check: unset-cpu-requirements, remediation: Set your container's CPU requests and limits depending on its requirements. See https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for more details.)deploy.yaml: (object: portainer/portainer apps/v1, Kind=Deployment) container "portainer" has cpu limit 0 (check: unset-cpu-requirements, remediation: Set your container's CPU requests and limits depending on its requirements. See https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for more details.)deploy.yaml: (object: portainer/portainer apps/v1, Kind=Deployment) container "portainer" has memory request 0 (check: unset-memory-requirements, remediation: Set your container's memory requests and limits depending on its requirements. See https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for more details.)deploy.yaml: (object: portainer/portainer apps/v1, Kind=Deployment) container "portainer" has memory limit 0 (check: unset-memory-requirements, remediation: Set your container's memory requests and limits depending on its requirements. See https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for more details.)Error: found 7 lint errors

检测类型

如您所见,YAML 文件中发现了错误,每个错误都有明确的补救步骤。

如果您想看一下内置检查,可以在https://github.com/stackrox/kube-linter/blob/main/docs/genic/checks.md中列出所有这些检查,或者也可以使用 KubeLinter 查看清单:

$ kube-linter checks list
Name: dangling-service
Description: Alert on services that don't have any matching deployments
Remediation: Make sure your service's selector correctly matches the labels on one of your deployments.
Template: dangling-service
Parameters: map[]
Enabled by default: true------------------------------Name: default-service-account
Description: Alert on pods that use the default service account
Remediation: Create a dedicated service account for your pod. See https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/ for more details.
Template: service-account
Parameters: map[serviceAccount:^(|default)$]
Enabled by default: false
|
|
|
and many more

忽略检测

您可以使用以下注释忽略针对 YAML 文件的特定检查,也可以根据需要忽略检查组:

ignore-check.kube-linter.io/<check-name>

例如:

ignore-check.kube-linter.io/unset-cpu-requirements

您可以像上面这样在上面的部署文件示例中添加忽略检查规则:

metadata:name: portainernamespace: portainerlabels:io.portainer.kubernetes.application.stack: portainerapp.kubernetes.io/name: portainerapp.kubernetes.io/instance: portainerapp.kubernetes.io/version: "2.0.0"annotations:ignore-check.kube-linter.io/unset-cpu-requirements : "cpu requirements not required"

现在,当您 kube-linter lint deploy.yaml 再次运行时,您应该不会看到与 CPU 要求相关的错误提示。

使用配置运行

KubeLinter 也可以使用配置运行,您可以在其中提供要包含/排除的所有检测信息。以下是存储库中的示例配置:

# customChecks defines custom checks.
customChecks:
- name: "required-label-app"template: "required-label"params:key: "app"
checks:# if doNotAutoAddDefaults is true, default checks are not automatically added.doNotAutoAddDefaults: false# addAllBuiltIn, if set, adds all built-in checks. This allows users to# explicitly opt-out of checks that are not relevant using Exclude.# Takes precedence over doNotAutoAddDefaults, if both are set.addAllBuiltIn: false# include explicitly adds checks, by name. You can reference any of the built-in checks.# Note that customChecks defined above are included automatically.include:- "required-label-owner"# exclude explicitly excludes checks, by name. exclude has the highest priority: if a check is# in exclude, then it is not considered, even if it is in include as well.exclude:- "privileged"

如果你运行kubelinter --config config lint deploy.yaml,其中 config 与上述配置的文件,你应该可以看到添加了需要标签的检查:

deploy.yaml: (object: portainer/portainer apps/v1, Kind=Deployment) no label matching "owner=<any>" found (check: required-label-owner, remediation: Add an email annotation to your object with information about the object's owner.)

当内置到 CI 管道中时,KubeLinter 可以证明是非常有用的工具。可以检查和验证推送到存储库中的编排文件,以获取最佳实践和安全考虑,并在检测到问题时生成警报。

这样,可以将部署到集群的应用程序进行健康的就绪检查。该项目处于 Alpha 阶段,但有望作为 CI 集成工具在实际部署到生产之前验证所有部署。

本文转载自:「云原生技术爱好者社区」,原文:https://tinyurl.com/y6yt5jhb,版权归原作者所有。欢迎投稿,投稿邮箱: editor@hi-linux.com。

你可能还喜欢

点击下方图片即可阅读

推荐 4 款超好用本地 Kubernetes 部署工具

点击上方图片,打开小程序,加入「玩转 Linux」圈子

更多有趣的互联网新鲜事,关注「奇妙的互联网」视频号全了解!

推荐一款 Kubernetes YAML 文件静态分析工具 KubeLinter相关推荐

  1. 推荐一款反编译文件的工具onekey-decompile-apk

    做项目需要,花了一上午的时间研究了下反编译工具,本文主要是如何获取apk文件中res中的资源.新手欢迎大家指点! 附上原文教程:https://code.google.com/p/onekey-dec ...

  2. 轻量却超强——推荐几款好用的截图工具

    轻量却超强--推荐几款好用的截图工具 阅读导览(点击快速跳转): 1.FScapture 2.Snipaste 3.Picpick 相关文件下载及链接 1.FScapture(返回目录) FScapt ...

  3. 好用的linux终端工具,推荐7款好用的终端工具

    原标题:推荐7款好用的终端工具 来源 | HelloCoder作者 | HaC1.Cmder 下载地址:https://cmder.net/ Cmder是一个代替cmd的终端工具.只能操作Window ...

  4. Python 爬虫,推荐一款简单的抓包工具(续)

    点击上方"AirPython",选择"加为星标" 第一时间关注 Python 原创干货! 1. 前言 上篇文章发布之后,有小伙伴后台私信我,说文章只涉及到手机端 ...

  5. 推荐几款图形转代码的工具

    推荐几款图形转代码的工具 2014年08月01日 标签:Geek 昨天,本来想找一款根据字符自动排列图片形状的插件.结果没有找到,反而有意外的收获.分享分享 1. 照片生成代码 链接:http://w ...

  6. 推荐几款非常棒的注释工具

    关注.星标公众号,直达精彩内容 来源 | 安富莱 虽然说最好的注释就是代码本身,但是达到这个水平太难了.在编程时,注释一般都会被认为是好习惯,可以增加代码的可读性,所以给大家推荐几款非常棒的注释工具. ...

  7. 推荐一款Kubernetes可视化管理工具——Kuboard

    现在主要的kubernetes可视化工具主要有: 1. Kubernetes Dashborad: Kubernetes 官方提供的图形化工具 2. Rancher: 目前比较主流的企业级kubern ...

  8. 两个助你高效编写 Kubernetes YAML 文件的神技

    公众号关注 「奇妙的 Linux 世界」 设为「星标」,每天带你玩转 Linux ! 我们在编写 Kubernetes 资源清单的时候可能会经常会忘记要创建的资源名称,即使知道了可能也不记得该资源对象 ...

  9. 值得拥有!精心推荐几款超实用的 CSS 开发工具

    当你开发一个网站或 Web 应用程序的时候,有合适的工具,绝对可以帮助您节省大量的时间.在这篇文章中,我为大家收集了超有用的 CSS 开发工具. 对于 Web 开发人员来说,找到有用的 CSS 开发工 ...

最新文章

  1. [C#]网络编程系列专题二:HTTP协议详解
  2. Design Pattern IDisposable Pattern C
  3. 怎么知道网站是用什么程序做的
  4. 【Python基础】使用Matplotlib可视化数据的5个强大技巧
  5. Tomcat7/8开启WebDAV的支持
  6. 前端学习(2734):重读vue电商网站44之使用 echarts
  7. java jni 原理_JNI的实现原理
  8. 为什么大厂们 一边裁员,一边招人。。
  9. oracle多用户导出导入用法
  10. java的finalize_Java中finalize()方法
  11. Exchange Server 2013系列十二:邮箱的基本管理
  12. word应用2(实用)
  13. 计算机系统导论与计算机导论,计算机系统导论之学习心得
  14. 一个开源「知乎日报」Android 客户端
  15. Precision(精准率) and Recall(召回率)
  16. python数据分析岗位做什么_给力!数据分析岗位内部人的建议,可以少走很多弯路...
  17. 微信js-sdk 微信自定义分享显示图片和描述不显示
  18. php中关于文件操作的面试题,php面试题及答案
  19. PCB抄板最新方法及步骤
  20. 中外对比:国内云计算平台的三大特点

热门文章

  1. 跨境电子商务及支付业务管理体系的构建
  2. Tidy2 for Notepad++ 7 64位
  3. 微信的这几个实用功能,你知道吗?
  4. 计算机公式固定数值符号,中级无纸化机考 数学公式/符号你会输入吗?
  5. 引导最大内存_实际内存不够大,可用内存更加小,这样解决
  6. NLP中数据增强的方法
  7. 中国热处理产业未来发展前景及投资可行性报告2022-2028年
  8. Python 字符串连接的七种方式
  9. 给大家推荐一些精品内容
  10. 安川机器人I/F面板设置 第一讲