Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

minikube - create Replicaset with separate pods in Kubernetes

I want to create a ReplicaSet with separate pods.

Pods

apiVersion: v1
kind: Pod
metadata:
  name: user-pod
  labels:
    app: user-pod
spec:
  containers:
    - name: user-container
      image: kia9372/store-user

Replicaset

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: user-replicaset
  labels:
    app: user-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: user-pod

but when i execute the following command, it throws the foloowing error:

kubectl create -f user-replicaset.yml 
 
>error: error validating "user-replicaset.yml": 
error validating data: ValidationError(ReplicaSet.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

What's the problem? how can I solve this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can't create replicaset without spec.template.spec.containers field. as this one is by default required to create replica-set in k8s. so you must need to add template field . and you can actually do what you want , just use your pod as here in template spec. template is the actual pod spec with which you are going to create your replica-set's pods.

this one is going to do the exact thing you are asking for:


apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: user-replicaset
  labels:
    app: user-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: user-pod
  template:
    metadata:
      labels:
        app: user-pod
    spec:
      containers:
      - name: user-container
        image: kia9372/store-user

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...