How to create multiple namespaces using single yaml file

#1

Cluster information:

Kubernetes version:1.14
Cloud being used: (put bare-metal if not on a public cloud) baremetal
Installation method:
Host OS: centos 7
CNI and version:
CRI and version:

cat ns.yaml
apiVersion: v1
kind: Namespace
metadata:
    name: Dev
    lablels: Dev
--
kind: Namespace
metadata:
    name: QA
    lables: QA
--
kind: Namepsace
metadata:
    name: Production
    labels: Production

kubectl create -f ns.yaml
error: error parsing ns.yaml: error converting YAML to JSON: yaml: line 7: could not find expected ':'
#2

Hello @apansuria12,

You need to use 3 dashes --- to separate the resources in yaml:

$ k get ns
NAME          STATUS   AGE
default       Active   64d
kube-public   Active   64d
kube-system   Active   64d

$ cat namespaces.yml
---
apiVersion: v1
kind: Namespace
metadata:
    name: namespace1
---
apiVersion: v1
kind: Namespace
metadata:
    name: namespace3
---
apiVersion: v1
kind: Namespace
metadata:
    name: namespace2

$ k apply -f namespaces.yml
namespace/namespace1 created
namespace/namespace3 created
namespace/namespace2 created

$ k get ns
NAME          STATUS   AGE
default       Active   64d
kube-public   Active   64d
kube-system   Active   64d
namespace1    Active   17s
namespace2    Active   14s
namespace3    Active   16s

You can also use a List:

$ cat namespaces-list.yaml
---
apiVersion: v1
kind: List
items:
- apiVersion: v1
  kind: Namespace
  metadata:
      name: namespace-list1
- apiVersion: v1
  kind: Namespace
  metadata:
      name: namespace-list2
- apiVersion: v1
  kind: Namespace
  metadata:
      name: namespace-list3

$ k apply -f namespaces-list.yaml
namespace/namespace-list1 created
namespace/namespace-list2 created
namespace/namespace-list3 created

$ k get ns
NAME              STATUS   AGE
default           Active   64d
kube-public       Active   64d
kube-system       Active   64d
namespace-list1   Active   43s
namespace-list2   Active   41s
namespace-list3   Active   40s
namespace1        Active   2m50s
namespace2        Active   2m47s
namespace3        Active   2m49s
1 Like