先上结论:
在Job资源的yaml文件,如果你要是用"selector.matchLabels",那就必须同时设置“selector.manual:true”。
Job.selector.manual表示是否可以使用selector选择器选择pod,其默认值是false。
通过yaml文件创建Job资源,执行kubectl create命令报错:
[root@master ~]# kubectl create -f job-demo.yaml
The Job "job-demo2" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"app":"busybox", "controller-uid":"b0e5ed28-fe5e-4562-b934-116a6a5a0b10"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: `selector` not auto-generated
[root@master ~]#
我的job-demo.yaml文件内容如下:
[root@master ~]# cat job-demo.yaml
apiVersion: batch/v1
kind: Job
metadata:
namespace: dev
name: job-demo2
labels:
type: job
env: pc
spec:
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
restartPolicy: OnFailure
containers:
- image: busybox
name: busybox-app
[root@master ~]#
报错信息中显示:'selector' not auto-generated
再次编辑此yaml文件,在'selector'上面增加一行代码:manualSelector:true
最后重新执行kubectl create -f job-demo.yaml命令就不报错了。
附Job.spec.manualSelector的文档:
[root@master ~]# kubectl explain Job.spec.manualSelector
KIND: Job
VERSION: batch/v1
FIELD: manualSelector <boolean>
DESCRIPTION:
manualSelector controls generation of pod labels and pod selectors. Leave
`manualSelector` unset unless you are certain what you are doing. When
false or unset, the system pick labels unique to this job and appends those
labels to the pod template. When true, the user is responsible for picking
unique labels and specifying the selector. Failure to pick a unique label
may cause this and other jobs to not function correctly. However, You may
see `manualSelector=true` in jobs that were created with the old
`extensions/v1beta1` API. More info:
https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#specifying-your-own-pod-selector
[root@master ~]#