Kubernetes is a powerful tool to host and orchestrate containerized backend solutions for production. What about our staging environments? Or small other apps we need to host somewhere? 🤔
In this article, I’ll use kubernetes namespaces and contexts to host multiple projects on the same kubernetes cluster.
Namespaces allow me to work on my current project and hide the other deployments. In a single namespace, you can’t have two resources with the same name, names have to be unique. But it’s allowed to have two resources with the same name, one on each namespace (for instance, the “web-api” service). It’s pretty handy. 🖐
kubectl create namespace my-namespace-name
kubectl -n my-namespace-name .....
Now that we know how to use namespaces for basic commands. Let’s use a context.
For namespaces, kubernetes contexts are awesome. They allow us to set a current-context that includes a namespace we want to work on, so we can get rid of the
-n my-namespace-name option after the
kubectl config current-context
# kubesys is the new context name
kubectl config use-context kubesys
My own use case of namespaces and contexts is to work on multiple projects, for example two websites. In an other article, I explained how to route your URLs/domains to multiple services. Basically, we need one ingress definition file, which will tell the ingress controller how to handle the routes.
It might depend on the cloud provider, but on a microk8s cluster, you can use the included ingress controller with multiple namespaces. All you need is a single ingress definition file for each namespace. The ingress controller will automatically merge them and route the incoming requests to the requested services.
Following is an example for two ingress files, for two namespaces/domains/projects. I noticed that it doesn’t seem to work if I try to use two namespaces with only one domain, though.