ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] 想验证 manifests 文件编写是否正确的话,得知道kubernetes定义的yaml格式描述定义规范。GitHub上有人维护kubernetes-json-schema库 - 新版本(1.19~new,持续更新):https://github.com/yannh/kubernetes-json-schema - 老版本(1.5~1.18,不含所有版本):https://github.com/instrumenta/kubernetes-json-schema # Kubeval安装 ```shell $ curl -O -L https://github.com/instrumenta/kubeval/releases/download/v0.16.1/kubeval-linux-amd64.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 2814k 100 2814k 0 0 989k 0 0:00:02 0:00:02 --:--:-- 2472k $ tar xvf kubeval-linux-amd64.tar.gz LICENSE README.md kubeval $ mv kubeval /usr/local/bin/ ``` 测试yaml文件 ```yaml cat <<'EOF' | tee nginx.yaml > /dev/null --- apiVersion: apps/v1 kind: Deployment metadata: name: nginx namespace: default spec: selector: matchExpressions: - key: app operator: In values: - "nginx" template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - name: nginx_port containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx namespace: default spec: type: ClusterIP selector: app: nginx ports: - name: nginx port: 80 targetPort: 80 EOF ``` # kubeval在线使用 ```shell $ kubeval --strict nginx.yaml PASS - nginx.yaml contains a valid Deployment (default.nginx) PASS - nginx.yaml contains a valid Service (default.nginx) ``` # kubeval离线使用 1. 下载kubernetes-json-schema库 > 目前下载压缩包(2.1G),解压后16G ```shell # 方法1:git方式下载(推荐) $ cd /data/kubernetes-json-schema $ git init Initialized empty Git repository in /data/kubernetes-json-schema/.git/ $ git pull https://github.com/yannh/kubernetes-json-schema.git remote: Enumerating objects: 314118, done. remote: Counting objects: 100% (112319/112319), done. remote: Compressing objects: 100% (728/728), done. remote: Total 314118 (delta 111900), reused 111712 (delta 111591), pack-reused 201799 Receiving objects: 100% (314118/314118), 82.48 MiB | 5.73 MiB/s, done. Resolving deltas: 100% (312604/312604), done. From https://github.com/yannh/kubernetes-json-schema * branch HEAD -> FETCH_HEAD # 方法2:curl方法下载 $ curl -O -L https://github.com/yannh/kubernetes-json-schema/archive/refs/heads/master.zip $ unzip -d /data master.zip Archive: master.zip b21b5fc193af54c09295e3572ef21cc76baa0c6b creating: /data/kubernetes-json-schema-master/ creating: /data/kubernetes-json-schema-master/.github/ creating: /data/kubernetes-json-schema-master/.github/workflows/ $ mv /data/kubernetes-json-schema-master /data/kubernetes-json-schema ``` 2. 搭建文件服务器 ```shell docker run -d --name fileserver -p 80:5100 -v /data:/app/caddy/data jiaxzeng/fileserver:v1.1.0-alpine3.16 ``` 3. 验证 ```shell $ alias kubeval='kubeval -s http://192.168.32.127/kubernetes-json-schema' $ kubeval nginx.yaml -v 1.23.3 PASS - nginx.yaml contains a valid Deployment (default.nginx) PASS - nginx.yaml contains a valid Service (default.nginx) ``` 推荐使用 kubeconform 工具,性能更优。请参考博客 https://www.cnblogs.com/jiaxzeng/p/18066255