Create multiple kubernetes namespaces - kubernetes

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.

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!

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

Related

How to make Deployment, Ingress and Service Yaml file on one file

I want to make a YAML file with Deployment, Ingress, and Service (maybe with clusterissuer, issuer and cert) on one file, how can I do that? I tried
kubectl apply -f (name_file.yaml)
You can it with three dashes on your yaml file
like this
apiVersion: v1
kind: Service
metadata:
name: mock
spec:
...
---
apiVersion: v1
kind: ReplicationController
metadata:
name: mock
spec:
Source : https://levelup.gitconnected.com/kubernetes-merge-multiple-yaml-into-one-e8844479a73a

no matches for kind "configmap" in version "apps/v1"

Well, I'm using minikube v1.26.0 and kubectl to manage Kubernetes on my local machine, when I decide to create a ConfigMap with kubectl apply -f ConfigMapFile.yaml I'm getting error no matches for kind "configmap" in version "apps/v1"
ConfigMapFile.yaml
apiVersion: apps/v1
kind: ConfigMap
metadata:
name: test-config-map
data:
.........
Seems Like ConfigMap is not allowed or deprecated in Kubernetes apps/v1, but cannot find any solution or tips that would help me with this problem.
you need to use apiVersion: v1 for configmap. You can also check the version of any resource using:
kubectl api-resources |grep -i configmap
configmaps cm v1 true ConfigMap

YAML code to create a configmap with a particular file (.js/.sh)

What will be the equivalent ConfigMap YAML code for the following command line?
kubectl create configmap mongo-initdb --from-file=init-mongo.js
There is actually a kubectl command that lets you get the generated yaml for a created config map. In your case:
kubectl get configmaps mongo-initdb -o yaml
You can use the command as suggested by Mo Xue, however here is YAML,
Example :
apiVersion: v1
kind: ConfigMap
metadata:
name: mongo-initdb
data:
init-mongo.js: |
<Content>
Read more about : https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-directories
Create the configmap from a file.
Just create a sample file like ui.properties
example:
cat ui.properties
1. name=x
2. rollno=y
Command to create a configmap from above file
kubectl create configmap ui-configmap --from-file=ui.properties
Verify the data.
kubectl get configmap ui-configmap -o yaml
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: <>
name: ui-configmap
namespace: default
data:
name: x
role: y

How to patch multi document yaml file on condition using yq?

Having YAML document something like:
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-scraping
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: allow-webhooks
I am trying to get something like
---
apiVersion: **networking.k8s.io/v1beta1**
kind: NetworkPolicy
metadata:
name: allow-scraping
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: allow-webhooks
So basically get document, if document has kind: NetworkPolicy then patch apiVersion: networking.k8s.io/v1beta1.
Ideally one liner, ideally with yq v4, but other solutions will be helpful too.
With mikefarah/yq on versions beyond 4, you could do a select and update |= operation on the required document
yq e 'select(.kind == "NetworkPolicy").apiVersion |= "networking.k8s.io/v1beta1"' yaml
The above works fine on yq version 4.6.0. Use the -i flag to replace the file in-place.
Given that other solutions will be helpful - an alternative solution would be using kustomize:
Create the kustomization.yaml file:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- network-policy.yaml
patches:
- target:
kind: NetworkPolicy
group: networking.k8s.io
version: v1
patch: |
- op: replace
path: /apiVersion
value: networking.k8s.io/v1beta1
Run
kustomize build | kubectl apply -f -
or
kubectl apply -k .

Using Lists or triple dashes to put multiple Kubernetes objects in one YAML file: purely a stylistic choice?

List API objects and triple dashes (---) can both be used to denote multiple objects in a single YAML file. Therefore, why do Lists exist when triple dashes accomplish the same thing (in my opinion) in a cleaner way? Are there any cases in which a List would be preferred over triple dashes, or is this purely a stylistic choice?
For example, these two YAML files both produce the same two ServiceAccount objects (chosen for brevity):
my-serviceaccounts1.yaml
apiVersion: v1
kind: List
metadata: {}
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
name: my-app
- apiVersion: v1
kind: ServiceAccount
metadata:
name: my-other-app
my-serviceaccounts2.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-app
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-other-app
I can think of two reasons:
Because the Kubernetes API works with JSON and in JSON there is no ---
Maybe the kind List is meant only for responses.