'How to have multiple object types in a single openshift yaml template?

Here is the sample template for single PersistentVolumeClaim with name : claim1

apiVersion: "v1"
kind: "PersistentVolumeClaim"
metadata:
  name: "claim1"
spec:
  accessModes:
    - "ReadWriteOnce"
  resources:
    requests:
      storage: "5Gi"
  volumeName: "pv0001"

How can i add multiple PersistentVolumeClaim in the same template file. For example adding claim2

I tried copy paste the same template from below it and change claim1 to claim2 but in openshift UI while importing the template it gives a warning : Duplicated Mapping key kind: PersistentVolumeClaim

Update: I have tried using --- as mentioned in the comments. But that throws a error Expected a single document in the stream but found more in the openshift UI while importing the yaml



Solution 1:[1]

I would recommend writing an actual template: https://docs.okd.io/latest/dev_guide/templates.html#writing-templates

Specifically it would look something like:

apiVersion: v1
kind: Template
metadata:
  name: multiple-pvcs
objects:
- apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata:
    name: "claim1"
  spec:
    accessModes:
      - "ReadWriteOnce"
    resources:
      requests:
        storage: "5Gi"
    volumeName: "pv0001"
- apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata:
    name: "claim2"
  spec:
    accessModes:
      - "ReadWriteOnce"
    resources:
      requests:
        storage: "5Gi"
    volumeName: "pv0002"
parameters: []

Then you can just create the objects with oc process template.yml | oc create -f -

Solution 2:[2]

To separate the objects all you need to add is ---

Yeap exactly this:

---

for example

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: replicaset-example-webapp-0-5
spec:
  selector:
    matchLabels:
      mylabel: mywebapp
  replicas: 6
  template:
    metadata:
      labels:
        mylabel: mywebapp
    spec:
      containers:
        - name: pod-example-0-5
          image: richardchesterwood/k8s-fleetman-webapp-angular:release0-5
---
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: replicaset-example-webapp-0
spec:
  selector:
    matchLabels:
      mylabel: mywebapp
  replicas: 2
  template:
    metadata:
      labels:
        mylabel: mywebapp
    spec:
      containers:
        - name: pod-example-0
          image: richardchesterwood/k8s-fleetman-webapp-angular:release0
---
apiVersion: v1
kind: Pod
metadata:
  name: just-a-queue
  labels:
    mylabelname: myqueueapp
spec:
  containers:
  - name: queue-app-container
    image: richardchesterwood/k8s-fleetman-queue:release1

Also you can have multiple Kubernetes objects under the same yaml file, all you need to do is to separate it with ---

But in OpenShift it's like this

Actually, this works if one creates an array of items:

apiVersion: v1beta3                                                                              
kind: List                                                                                       
items:                                                                                           
  - #list of API objects

Solution 3:[3]

you can split yaml documents by adding on a new line

---

see https://yaml.org/spec/1.2/spec.html#id2760395

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Will Gordon
Solution 2
Solution 3 rkachach