# Spawn notebook on different namespace on Kubernetes

**URL:** https://discourse.jupyter.org/t/spawn-notebook-on-different-namespace-on-kubernetes/26977
**Category:** JupyterHub
**Tags:** how-to, help-wanted, jupyterhub, community
**Created:** [July 15, 2024, 2:22pm UTC](https://discourse.jupyter.org/t/spawn-notebook-on-different-namespace-on-kubernetes/26977 "2024-07-15T14:22:59Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![moriabs88](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/moriabs88/32/13475_2.png) [@moriabs88](https://discourse.jupyter.org/u/moriabs88)
#### Post date: [July 15, 2024, 2:22pm UTC](https://discourse.jupyter.org/t/spawn-notebook-on-different-namespace-on-kubernetes/26977/1 "2024-07-15T14:22:59Z")

</div>

hi, I’m deploying jupyterhub on Kubernetes cluster with the helm chart,  
I’m using custom **pre\_spawn\_hook** to determine the namespace-  
in my conf.py file:  
async def custom\_pre\_spawn\_hook(spawner):  
if :  
spawner.namespace = namespce\_1  
else:  
spawner.namespace = namespce\_2

c.kubespawner.pre\_spawn\_hook = custom\_pre\_spawn\_hook

with this configuration the pod is spawn successfully on the other namespace but the hub don’t get the pod and in the ui I’m stack on state of server quested with no events.

when I’m changing the value of **c.kubespawner.namespce** to the wanted namespace its working.

what is the difference between the spawner.namespace inside the custom\_pre\_spawn\_hook and the c.kubespawner.namespce. and how can i change it dynamically?

---

<div class="post-metadata">

### Author: ![minrk](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/minrk/32/13_2.png) [@minrk](https://discourse.jupyter.org/u/minrk)
#### Post date: [July 31, 2024, 1:06pm UTC](https://discourse.jupyter.org/t/spawn-notebook-on-different-namespace-on-kubernetes/26977/2 "2024-07-31T13:06:57Z")

</div>

I think you’ll want to set `KubeSpawner.enable_user_namespaces = True`, at which point the namespace is governed by `KubeSpawner.user_namespace_template`:

```yaml
hub:
  config:
    KubeSpawner:
      enable_user_namespaces: true

```

The reason changing namespace in hooks doens’t work is that kubespawner uses _reflectors_ to watch for resources changes, which are tied to namespaces. Changing the namespace after the reflectors start means resources won’t be found.
