Create multiple kubernetes namespaces

10/19/2021

I was wondering if anyone has any ideas on the best way to create 200 namespaces within a cluster. Ideally a simple bash loop to create kubectl create namespaces would be good.

-- Max
kubernetes
namespaces

2 Answers

10/19/2021

You can dynamically create a YAML file using any programming language you are most comfortable with (bash or python), consisting of a list of k8s namespaces with the following format:

$ 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

Then execute the following command to create them all in one shot! kubectl apply -f namespaces-list.yaml

Hope this helped!

-- user17194172
Source: StackOverflow

12/5/2021

I eventually came up with something as simple this

#!/usr/bin/env bash
  for i in $(seq 100); do
    kubectl create namespace test-${i}
         
done
-- Max
Source: StackOverflow