Getting label ValidationError Error while executing manifest file in kubernetes

1/20/2020

I am trying to deploy a kubernetes manifest file, but its failing with the below ValidationError

error:

error validating data: ValidationError(Deployment.spec.selector): unknown field "app" in io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector; if you choose to ignore these errors, turn validation off with --validate=false

Manifest File:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: elastickibana
spec:
  replicas: 1
  selector:
    app: elastickibana
  template:
    metadata:
    spec:
      containers:
      - name: elk
        image: daniccan/kibana-plugin-tester:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 5601
        - containerPort: 9200
        env:
        - name: KIBANA_VERSION
          value: 6.8.2
        - name: PLUGIN_FILE_NAME
          value: kibana-c8-plugin-Alpha-0.0.1-Kibana-6.8.2.zip
        - name: KIBANA_PLUGIN_PATH
          value: /home/sreenivasa/Projects/c8/infrastructure/capsule8-sandbox
---
kind: Service
apiVersion: v1
metadata:
  name: elk-service
  labels:
    app: elk-kibana
spec:
  type: LoadBalancer
  externalIPs:
  - 10.0.2.15
  selector:
    name: elk-kibana
  ports:
  - port: 5601
    name: elasticservice
  ports:
  - port: 9200
    name: serving-http

Kindly validate if my manifest file is correct and let me know if I miss something.

-- SREENIVASA REDDY S
elk
kubernetes
manifest

2 Answers

1/20/2020

Firstly, you need to use the selector only when you have a label. You have to define labels in the deployment metadata and use it in the replica set.

apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: elastickibana
      labels:
        app: elastickibana
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: elastickibana
      template:
        metadata:
          labels:
            app: elastickibana
        spec:
          containers:
          - name: elk
            image: daniccan/kibana-plugin-tester:latest
            imagePullPolicy: Always
            ports:
            - containerPort: 5601
            - containerPort: 9200
            env:
            - name: KIBANA_VERSION
              value: 6.8.2
            - name: PLUGIN_FILE_NAME
              value: kibana-c8-plugin-Alpha-0.0.1-Kibana-6.8.2.zip
            - name: KIBANA_PLUGIN_PATH
              value: /home/sreenivasa/Projects/c8/infrastructure/capsule8-sandbox
    ---
    kind: Service
    apiVersion: v1
    metadata:
      name: elk-service
      labels:
        app: elastickibana
    spec:
      type: LoadBalancer
      externalIPs:
      - 10.0.2.15
      selector:
        app: elastickibana
      ports:
      - port: 5601
        name: elasticservice
      ports:
      - port: 9200
        name: serving-http

Your service was also incorrect: it needs to match the labels of the pods, if it doesn't match service will not assign to any endpoints(pod IPs)

$ kubectl get ep elk-service 
NAME          ENDPOINTS   AGE
elk-service   <none>      9m27s

If you assign the right label selector:

$ kubectl get ep elk-service 
NAME          ENDPOINTS         AGE
elk-service   172.17.0.6:9200   10m

Reference: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

-- suresh Palemoni
Source: StackOverflow

1/20/2020

You are missing label selectors in your deployment, labels under the template, in the deployment too, and your service label selector should match these labels.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: elastickibana
spec:
  replicas: 1
  selector:
    matchlabels:                     <--missing this
      app: elastickibana
  template:
    metadata:
      labels:                        <--and this
        app: elastickibana
    spec:
      containers:
      - name: elk
        image: daniccan/kibana-plugin-tester:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 5601
        - containerPort: 9200
        env:
        - name: KIBANA_VERSION
          value: 6.8.2
        - name: PLUGIN_FILE_NAME
          value: kibana-c8-plugin-Alpha-0.0.1-Kibana-6.8.2.zip
        - name: KIBANA_PLUGIN_PATH
          value: /home/sreenivasa/Projects/c8/infrastructure/capsule8-sandbox

kind: Service
apiVersion: v1
metadata:
  name: elk-service
  labels:
    app: elk-kibana
spec:
  type: LoadBalancer
  externalIPs:
  - 10.0.2.15
  selector:
    app: elastickibana             <--and this should be like this
  ports:
  - port: 5601
    name: elasticservice
  ports:
  - port: 9200
    name: serving-http

Now, try to do your investigation before posting any questions. These are basics that you would find out by just matching a Deployment and Service examples from the docs.

-- suren
Source: StackOverflow