Unable to helm install due to deployment manifest issue

12/17/2019

While trying to perform helm install

Error: unable to build kubernetes objects from release manifest: [unable to recognize "": no matches for kind "Service" in version "extensions/v1beta1", error validating "": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec]

My service.yaml looks like below

apiVersion: extensions/v1beta1
kind: Service
metadata:
  name: helm-xxx-helper-api
spec:
  type: NodePort
  ports:
    - nodePort: 31235
      port: 80
      targetPort: 8080
  selector:
     app: helm-xxx-helper

My deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helm-xxx-helper
spec:
  replicas: 2
  selector:
    matchLabels:
    name: helm-xxx-helper
  template:
    metadata:
      labels:
        app: helm-xxx-helper
    spec:
      containers:
      - name: helm-xxx-helper
        image: xxxxxxxxx:5001/devops/xxx-helper:latest
        imagePullPolicy: Always
        env:
          - name: XXX_STAGE
            value: "DEV"
        ports:
        - containerPort: 8080

What could be the issue here?

-- Hari Ennekat
kubernetes
kubernetes-helm

2 Answers

12/17/2019

As you received this error it means that you are using version Kubernetes 1.16 or newer.

Issue 1 - With Service

In this version many apiVersion has been changed (Deployments, StatefulSet, Service). More details can be found here.

In Kubernetes 1.16 you need to use apiVersion: v1 for service. Otherwise you will receive errors like

error: unable to recognize "STDIN": no matches for kind "Service" in version "extensions/v1beta1"
error: unable to recognize "STDIN": no matches for kind "Service" in version "extensions/v1"
error: unable to recognize "STDIN": no matches for kind "Service" in version "apps/v1"

Issue 2 - With Deployment.

  • spec.selector.matchLabels does not contain value like name. You need to use value from labels. So in this case instead of name: helm-xxx-helper you need to use app: helm-xxx-helper otherwise you will receive error like:
The Deployment "helm-xxx-helper" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"helm-xxx-helper"}: `selector` does not match template `labels`
  • wrong YAML format. In your code you have
...
selector:
  matchLabels:
  name: helm-xxx-helper
...

Value for matchLabels should be under 3rd letter (t). Also as I mentioned in previous point you need to change name to app

Proper format with correct valye of matchLables:

...
selector:
  matchLabels:
    app: helm-xxx-helper
...

You can read about Labels and Selectors here.

As you mentioned it is HELM, you will need to change Kubernetes version to older than 1.16 or change apiVersion in each object YAML in template directory. There was already a similar case. Please check this thread for more information.

Below both YAMLs which will create Service and Deployment. Tested on Kubernetes 1.16.1.

apiVersion: v1
kind: Service
metadata:
  name: helm-xxx-helper-api
spec:
  type: NodePort
  ports:
    - nodePort: 31235
      port: 80
      targetPort: 8080
  selector:
    app: helm-xxx-helper
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helm-xxx-helper
spec:
  replicas: 2
  selector:
    matchLabels:
      app: helm-xxx-helper
  template:
    metadata:
      labels:
        app: helm-xxx-helper
    spec:
      containers:
      - name: helm-xxx-helper
        image: nginx # As I dont have your image ive put nginx
        imagePullPolicy: Always
        env:
          - name: XXX_STAGE
            value: "DEV"
        ports:
        - containerPort: 8080
-- PjoterS
Source: StackOverflow

12/17/2019

try this in selector

  selector:
    matchLabels:
      app: helm-xxx-helper  
-- shubham_asati
Source: StackOverflow