k8s schema validationにkubeconformが便利という話
こんにちは、たけるです。
皆さんk8sのマニフェストが構文が正しい、ということをどうやって確認していますか?
必須フィールドに抜けがないのかということを確認していますか?
マニフェストのValidationをする方法としては以下があると思います(他にもあるかもしれません)
- kubectl apply --dry-run=client
- kubectl apply --dry-run=server
- kubeval
- kubeconform
kubectl applyのdry runはserverモードだと、色々な設定ミスを検出できます(たぶん)が、実際に動いているKubernetes環境が必要で色々と面倒です
kubevalやkubeconformはKubernetes環境がなくてもKubernetesマニフェストを検証できるツールです。
たとえばこんな感じのマニフェストがあった
として
apiVersion: apps/v1
kind: Deployment
metaData:
name: test
namespace: test
labels:
app: test
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: "80"
kubeconformでValidationすると
kubeconform -strict test.yaml
test.yaml - Deployment test is invalid: For field (root): Additional property metaData is not allowed - For field spec.template.spec.containers.0.ports.0.containerPort: Invalid type. Expected: integer, given: string
のようにcontainerPortがintegerにする必要があるよ〜とか指摘してくれます(便利)
kubeconformはkubevalを意識して作られた後発のOSSですが、kubevalと比較して以下の改善があるらしいです
high performance: will validate & download manifests over multiple routines, caching downloaded files in memory
configurable list of remote, or local schemas locations, enabling validating Kubernetes custom resources (CRDs) and offline validation capabilities
uses by default a self-updating fork of the schemas registry maintained by the kubernetes-json-schema project - which guarantees up-to-date schemas for all recent versions of Kubernetes.
引用
https://github.com/yannh/kubeconform#kubeconform
kubeconformはkubevalは対応していないCRDサポートしていたり、新しいKubernetesのバージョンのスキーマに対応していてスッと検証できるのが最高です