Kubernetes: Namespace

Overview

Teaching: 0 min
Exercises: 10 min
Questions
  • How do we seperate things??
Objectives
  • Creating a Namespace.

Namespace

We start with the basics, a personal namespace: cafe-${myname}. CESSDA uses Namespaces to logically seperate products.

mkdir -p misc/k8s

Now into misc/k8s/template-namespace.yaml we put

apiVersion: v1
kind: Namespace
metadata:
  name: NAMESPACE

Note the capital NAMESPACE at the end. This will be transformed into your namespace later. For this, we need the script misc-creation.sh

#!/bin/bash
set -eu

sed "s/NAMESPACE/${product_name}/g" "misc/k8s/template-namespace.yaml" > "misc/k8s/${product_name}-namespace.yaml"

# Namespace
kubectl apply -f "misc/k8s/${product_name}-namespace.yaml"

This script uses the variable $product_name to replace the string NAMESPACE above and then create the namespace.

We will later see how this integrates with Jenkins. So set let’s set the variable:

export product_name=cafe-${myname}

and run the script

bash misc-creation.sh

Check the results

kubectl get namespace

Let’s make sure we use it for all commands now, even if the scripts have a safety built in:

kubectl config set-context --current --namespace=${product_name}