1. 程式人生 > 其它 >K8S建立Deployment,StatefulSet報錯:missing required field "selector" in io.k8s.api.apps.v1.StatefulSetSpec; if you choose to ignore these errors, turn validation off with --validate=false

K8S建立Deployment,StatefulSet報錯:missing required field "selector" in io.k8s.api.apps.v1.StatefulSetSpec; if you choose to ignore these errors, turn validation off with --validate=false

部分yaml指令碼在修改了修改 apiVersion 選項: apps/v1版本之後之後報錯

示例:apiVersion: apps/v1beta1 改成 apiVersion: apps/v1

報錯資訊:

[root@k8s-master rabbitmq-cluster-on-k8s]# kubectl apply -f rabbitmq_statefulsets.yaml 
service/rabbitmq created
configmap/rabbitmq-config created
error: unable to recognize "rabbitmq_statefulsets.yaml": no matches for kind "StatefulSet" in version "apps/v1beta1"
[root@k8s-master rabbitmq-cluster-on-k8s]# vi rabbitmq_statefulsets.yaml 
[root@k8s-master rabbitmq-cluster-on-k8s]# kubectl apply -f rabbitmq_statefulsets.yaml 
service/rabbitmq unchanged
configmap/rabbitmq-config unchanged
error: error validating "rabbitmq_statefulsets.yaml": error validating data: ValidationError(StatefulSet.spec): missing required field "selector" in io.k8s.api.apps.v1.StatefulSetSpec; if you choose to ignore these errors, turn validation off with --validate=false

處理方法:
yaml 增加選擇器

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rabbitmq
#  namespace: test-rabbitmq
spec:
  serviceName: rabbitmq
  selector:
    matchLabels:
      app: rabbitmq
  replicas: 3

效果: