CodeBlanks Logo Icon CodeBlanks

Creating a Namespace

Learn to create a Namespace with kubectl create namespace.

Creating namespaces

A namespace is created with a command or with YAML. The name must consist of lowercase letters, digits and hyphens. Objects are placed in a namespace in YAML with metadata.namespace:

kubectl create namespace dev

apiVersion: v1
kind: Namespace
metadata:
  name: prod
  labels:
    ortam: uretim
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: api-ayar
  namespace: prod
data:
  LOG_LEVEL: warn

Resource quota

A ResourceQuota limits the total CPU, memory and object count a namespace can consume. While a quota exists, every container in the namespace must write requests/limits; a LimitRange gives defaults for those that don't:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: dev-kota
  namespace: dev
spec:
  hard:
    requests.cpu: "4"
    requests.memory: 8Gi
    pods: "20"

Practice An environment namespace

Write two objects in a single YAML file separated by ---: the prod Namespace labelled ortam: uretim; the api-ayar ConfigMap in the prod namespace with LOG_LEVEL: warn under data.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: v1
kind: Namespace
metadata:
  name: prod
  labels:
    ortam: uretim
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: api-ayar
  namespace: prod
data:
  LOG_LEVEL: warn

Expected output

namespace/prod created
configmap/api-ayar created

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens to the objects in a namespace when it is deleted?

  • They stop but aren't deleted
  • All of them are deleted Correct answer
  • They move to default
  • They are kept

Explanation: kubectl delete namespace can't be undone; it's used with care.

2 Place the object in a namespace in YAML.

Question
metadata:
  name: api-ayar
  ____: prod
Answer
metadata:
  name: api-ayar
  namespace: prod

Explanation: metadata.namespace is the object's namespace.

3 Match.

Term Meaning
create namespace Create a namespace
metadata.namespace Object's location
ResourceQuota Total limit
LimitRange Default request/limit

All lessons in Intermediate