Unable to deploy yaml to Kubernetes cluster in Azure

5/24/2019

I have a single image that I'm trying to deploy to an AKS cluster. The image is stored in Azure container registry and I'm simply trying to apply the YAML file to get it loaded into AKS using the following command:

kubectl apply -f myPath\myimage.yaml

kubectl keeps complaining that I'm missing the required "selector" field and that the field "spec" is unknown. This seems like a basic image configuration so I don't know what else to try.

kubectl : error: error validating "myimage.yaml": error validating data: [ValidationError(Deployment.spec): unknown field "spec" in io.k8s.api.apps.v1.DeploymentSpec, ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec]; if you choose to ignore these errors, turn validation off with --validate=false At line:1 char:1

apiVersion: apps/v1
kind: Deployment
metadata:
    name: myimage
spec:
    replicas: 1
    template:
        metadata:
            labels:
                app: myimage
    spec:
        containers:
        - name: myimage
          image: mycontainers.azurecr.io/myimage:v1
          ports:
          - containerPort: 5000
-- Geekn
azure
kubernetes

2 Answers

5/24/2019

You have incorrect indentation of second spec field and also you missed selector in first spec:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: myimage
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myimage
  template:
    metadata:
      labels:
        app: myimage
    spec:
      containers:
      - name: myimage
        image: mycontainers.azurecr.io/myimage:v1
        ports:
        - containerPort: 5000
-- Vasily Angapov
Source: StackOverflow

5/24/2019

As specified in the error message, deployments require a selector field inside their spec. You can look at the link for some examples.

Also, do note that there are two spec fields. One for the deployment and one for the pod used as template. Your spec for the pod is misaligned. It should be one level deeper.

-- Alassane Ndiaye
Source: StackOverflow