Error while trying to create ReplicaSet on Kubernetes with YAML

I’m a beginner with Kubernetes and YAML. I’ve been trying to deploy a ReplicaSet with YAML. This is the file for the ReplicaSet:

apiVersion: apps/v1
kind: ReplicaSet
metadata: 
  name: myapp-replicaset
  labels:
   app: myapp
spec:
  selector:
     matchLabels: 
      env: production
      name: nginx
 replicas: 3
 template:
    metadata:
      name: nginx
      labels:
        env: production
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
          - containerPort: 80

And this is the Pod file:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: production
    name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    resources:
      limits:
        memory: "128Mi"
        cpu: "500m"
    ports:
      - containerPort: 80

However, when I execute the kubectl create -f replicaset.yml command, I get the following error: The ReplicaSet “myapp-replicaset” is invalid:

  • spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string(nil), MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: empty selector is invalid for deployment
  • spec.template.spec.containers: Required value

Hi @Aditya-Pande

The issue is with the indentation. YAML is extremely fussy about this as it is part of the syntax.
You have incorrect indentation on these lines

 replicas: 3
 template:

Here is the fixed version

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
   app: myapp
spec:
  selector:
     matchLabels:
      env: production
      name: nginx
  replicas: 3
  template:
    metadata:
      name: nginx
      labels:
        env: production
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
          - containerPort: 80

Hi! thanks for replying. Unfortunately, I’m still facing the same issues even after this fix.

Did you paste exactly my code as replicaset.yml?
The errors you posted are about invalid YAML syntax.

If using vi, ensure paste mode is enabled when pasting from an external source, or it can mess up the formatting

root@controlplane ~ ➜  cat replicaset.yml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
   app: myapp
spec:
  selector:
     matchLabels:
      env: production
      name: nginx
  replicas: 3
  template:
    metadata:
      name: nginx
      labels:
        env: production
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
          - containerPort: 80

root@controlplane ~ ➜  kubectl create -f replicaset.yml
replicaset.apps/myapp-replicaset created