如果要进行[k8s]干运行,则最好不要使用干运行选项
在这篇文章中介绍的–server-dry-run选项在v1.18及更高版本中需要替换为–dry-run=server。
https://kubernetes.io/blog/2019/01/14/apiserver-dry-run-and-kubectl-diff/
https://kubernetes.io/blog/2019/01/14/apiserver-dry-run-and-kubectl-diff/
当在执行kubectl apply命令时,加上–dry-run选项,可以在不创建对象的情况下检查清单文件的正确性。
$ kubectl apply -f xxx.yaml --dry-run
然而,此选项仅可在本地执行语法检查,例如检查清单文件,无法确认通过 kube-apiserver 的行为。
因此,我建议使用–server-dry-run选项或kubectl diff命令作为替代。
$ kubectl apply -f xxx.yaml --server-dry-run
$ kubectl diff -f xxx.yaml
※ 本功能在 Kubernetes 1.13 及以上版本中已成为 beta 版本。
我实际进行了差异验证。
例如,我们准备一个故意将图像注释掉的部署清单文件,如下所示。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
# image: nginx:1.17.9
ports:
- containerPort: 443
当您尝试比较–dry-run和–server-dry-run / kubectl diff在此状态下的行为时,您会发现以下情况:
$ kubectl apply -f dryrun-nginx.yaml --dry-run
deployment.apps/nginx created (dry run)
$ kubectl apply -f dryrun-nginx.yaml --server-dry-run
The Deployment "nginx" is invalid: spec.template.spec.containers[0].image: Required value
$ kubectl diff -f dryrun-nginx.yaml
The Deployment "nginx" is invalid: spec.template.spec.containers[0].image: Required value
这样,使用 –dry-run 选项无法检测异常,而使用 –server-dry-run / kubectl diff 可以正确检测。
kubectl diff 命令会在应用清单时输出与上次的差异,因此在某些情况下可能更方便使用它。
请参考以下资料
- https://kubernetes.io/blog/2019/01/14/apiserver-dry-run-and-kubectl-diff/