---
# System prepended metadata

title: jupyter proxy
tags: [NTUToolmenLab]

---

# jupyter proxy
###### tags: `NTUToolmenLab`


using jupyter hub
https://jupyterhub.readthedocs.io/en/stable/quickstart.html#prerequisites
https://jupyterhub.readthedocs.io/en/stable/reference/config-proxy.html

## Install
```
sudo apt update 
sudo apt -y install npm nodejs
sudo python3 -m pip install jupyterhub
sudo npm install -g configurable-http-proxy
```

## run
```
jupyterhub --url http://0.0.0.0:8000/jupyter/
jupyterhub --url http://0.0.0.0:8000/jupyter/$(cat /etc/hostname)
```

## proxy by kubernetes traefik
you set your pod that run jupyterhub
with label `srvname: linnil1155628469899403`
``` yml
apiVersion: v1
kind: Service
metadata:
  namespace: user
  name: linnil1155628469899403
spec:
  selector:
    srvname: linnil1155628469899403
  ports:
  - port: 8000
    targetPort: 8000
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: linnil1155628469899403
  namespace: user
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.frontend.rule.type: PathPrefix
spec:
  rules:
  - http:
      paths:
      - path: /jupyter/linnil1155628469899403
        backend:
          serviceName: linnil1155628469899403
          servicePort: 8000
```

## create by script
rbac
``` yml
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods", "services"]
  verbs: ["get", "list", "create", "delete"]

- apiGroups: ["extensions"]
  resources: ["ingresses"]
  verbs: ["get", "list", "create", "delete"]
```

create by api
``` python
from kubernetes import client, config
config.load_incluster_config()
v1 = client.CoreV1Api()
v1.delete_namespaced_service("linnil1155628469899403", 'user')
v1.create_namespaced_service('user', a)

v1beta = client.ExtensionsV1beta1Api()
v1beta.delete_namespaced_ingress('linnil1155628469899403', 'user')
v1beta.create_namespaced_ingress('user', a)

```