skip to Main Content

I tried to create replicaset application file in the terminal but ends up with an error saying Error from server (BadRequest): error when creating "replicaset-definition.yaml": ReplicaSet in version "v1" cannot be handled as a ReplicaSet: strict decoding error: unknown field "replicas", unknown field "selector". Can anyone help me fix this issue? Thanks

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
      app: myapp
      type: front-end

spec:
  template:

    metadata:
     name: myapp-pod
     labels:
        app: myapp
        type: front-end
    spec:
      containers:
      - name: nginx-container
        image: nginx

replicas: 3
selector:
   matchLabels:
      type: front-end

2

Answers


  1. You can type

    kubectl explain replicaset
    

    to get a description for the ReplicaSet type.

    Your fields replicas and selector needs one more indentation level.

    E.g.

    Like this:

    spec:
        template:
        replicas:
        selector:
    
    Login or Signup to reply.
  2. In your case it should looks like:

    
    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      name: myapp-replicaset
      labels:
          app: myapp
          type: front-end
    
    spec:
      template:
    
        metadata:
         name: myapp-pod
         labels:
            app: myapp
            type: front-end
        spec:
          containers:
          - name: nginx-container
            image: nginx
    
      replicas: 3
      selector:
        matchLabels:
          type: front-end
    

    You can use one of k8s syntax validation tools, for example kubeval, for checking yaml files for this kind of issues.

    Alexanders-MacBook-Pro:~ alexander$ kubeval tst.yaml
    PASS - tst.yaml contains a valid ReplicaSet (myapp-replicaset)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search