SwarmSpawner:一个使用Docker Swarm服务的JupyterHub启动器
项目描述
SwarmSpawner 允许 JupyterHub 在Docker服务中启动单个用户笔记本服务器。
有关Docker服务的更多信息 请在此处查看。
先决条件
需要Python版本3.3及以上。
安装
pip install swarmspawner
从GitHub安装
git clone https://github.com/cassinyio/SwarmSpawner
cd SwarmSpawner
python setup.py install
配置
您可以在示例中找到jupyter_config.py的示例。
启动器
与Docker容器相比,Swarm模式下的Docker Engine和相关服务的工作方式不同。
通过在您的 jupyterhub_config.py 中添加以下行来告诉JupyterHub使用SwarmSpawner:
c.JupyterHub.spawner_class = 'cassinyspawner.SwarmSpawner'
c.JupyterHub.hub_ip = '0.0.0.0'
# This should be the name of the jupyterhub service
c.SwarmSpawner.jupyterhub_service_name = 'NameOfTheService'
什么是 jupyterhub_service_name?
在Swarm模式下的Docker引擎中,服务使用 name 而不是 ip 来相互通信。‘jupyterhub_service_name’ 是JupyterHub服务的名称。
网络
将JupyterHub服务(以及代理)和运行jupyter notebook的服务放在同一网络中非常重要,否则它们无法相互通信。由于SwarmSpawner使用服务的名称而不是服务的ip,因此JupyterHub和服务器应该共享相同的overlay网络(跨越节点的网络)。
#list of networks
c.SwarmSpawner.networks = ["mynetwork"]
在jupyterhub_config.py中定义服务
您可以在 jupyterhub_config.py 中定义 container_spec、resource_spec 和 networks。
Container_spec
命令 和 参数 取决于您所使用的镜像。
如果您使用 Jupyter docker-stack 中的其中一个镜像,则需要将 参数 指定如下:/usr/local/bin/start-singleuser.sh
如果您使用特定的镜像,那么指定正确的命令就是您的责任。
c.SwarmSpawner.container_spec = {
# The command to run inside the service
# 'args' : ['/usr/local/bin/start-singleuser.sh'], # (list)
'Image' : 'YourImage',
'mounts' : mounts
}
注意:在容器规范中,参数 设置 Dockerfile 中 CMD 的等效项,命令 设置 ENTRYPOINT 的等效项。笔记本服务器命令不应该设置为 ENTRYPOINT,因此通常使用 参数,而不是 命令,来指定如何启动笔记本服务器。
有关更多信息,请参阅这个 问题。
绑定主机目录
使用 'type':'bind',您可以将主机上的本地目录挂载到容器内部。
请记住,源应该在您创建服务的节点上存在。
notebook_dir = os.environ.get('NOTEBOOK_DIR') or '/home/jovyan/work'
c.SwarmSpawner.notebook_dir = notebook_dir
mounts = [{'type' : 'bind',
'source' : 'MountPointOnTheHost',
'target' : 'MountPointInsideTheContainer',}]
挂载命名卷
使用 'type':'volume',您可以将 Docker 卷挂载到容器内部。如果该卷不存在,则会创建。
mounts = [{'type' : 'volume',
'source' : 'NameOfTheVolume',
'target' : 'MountPointInsideTheContainer',}]
命名路径
对于卷和绑定两种类型,您都可以在源中指定一个 {username}。
mounts = [{'type' : 'volume',
'source' : 'jupyterhub-user-{username}',
'target' : 'MountPointInsideTheContainer',}]
用户名将是用户名的哈希版本。
挂载匿名卷
此类卷将在服务删除时被删除。
mounts = [{'type' : 'volume',
'target' : 'MountPointInsideTheContainer',}]
资源规范
您还可以为每个服务指定一些资源。
c.SwarmSpawner.resource_spec = {
'cpu_limit' : 1000, # (int) – CPU limit in units of 10^9 CPU shares.
'mem_limit' : int(512 * 1e6), # (int) – Memory limit in Bytes.
'cpu_reservation' : 1000, # (int) – CPU reservation in units of 10^9 CPU shares.
'mem_reservation' : int(512 * 1e6), # (int) – Memory reservation in Bytes
}
使用用户选项
您可以使用 user_options 设置参数。
# To use user_options in service creation
c.SwarmSpawner.use_user_options = False
要控制服务的创建,您有两种方式:使用 jupyterhub_config.py 或 user_options。
请记住,您最终只是使用 Docker 引擎 API。
如果使用,user_options 将会覆盖 jupyter_config.py 中的服务设置。
如果您设置 c.SwarmSpawner.use_user_option = True,则启动器将使用通过表单或 JSON 主体传递的字典进行操作时使用的 Hub API。
启动器期望一个包含以下键的字典
user_options = {
'container_spec' : {
# (string or list) command to run in the image.
'args' : ['/usr/local/bin/start-singleuser.sh'],
# name of the image
'Image' : '',
'mounts' : mounts,
'resource_spec' : {
# (int) – CPU limit in units of 10^9 CPU shares.
'cpu_limit': int(1 * 1e9),
# (int) – Memory limit in Bytes.
'mem_limit': int(512 * 1e6),
# (int) – CPU reservation in units of 10^9 CPU shares.
'cpu_reservation': int(1 * 1e9),
# (int) – Memory reservation in bytes
'mem_reservation': int(512 * 1e6),
},
# list of constrains
'placement' : [],
# list of networks
'network' : [],
# name of service
'name' : ''
}
}
在 Swarm 模式下 Docker 引擎中 Jupyter notebook 服务的名称
当 JupyterHub 启动一个新的 Jupyter notebook 服务器时,服务的名称将是 {service_prefix}-{service_owner}-{service_suffix}
您可以通过这种方式更改 service_prefix
Docker 服务的名称前缀
c.SwarmSpawner.service_prefix = "jupyterhub"
service_owner 是哈希过的 user.name 的 hexdigest()。
在具有命名服务器的情况下(用户有多个服务器),service_suffix 是服务器的名称,否则始终为 1。
下载镜像
Swarm 模式下的 Docker 引擎会自动从仓库下载镜像。如果镜像在远程仓库或本地可用,否则会出错。
由于在启动服务之前需要完成镜像的下载,所以更长的超时(默认为 30 秒)会更好。
c.SwarmSpawner.start_timeout = 60 * 5
您可以使用 Jupyter docker-stacks 中的所有 Docker 镜像。
贡献
如果您想为项目做出贡献,请阅读 贡献文档。
对于 开发安装,请克隆 存储库,然后从源安装。
git clone https://github.com/cassiny/SwarmSpawner
cd SwarmSpawner
pip install -r requirements/base.txt -e .
致谢
许可
所有代码均按照修订版的 BSD 许可协议授权。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源代码分发
构建分发
swarmspawner-0.1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0d194f03a8899b6ca645c13177d1a627d2399ddaf01256050d1c8b856a5c51d4 |
|
MD5 | 35391ef7f5963568f02d26fb02f16e47 |
|
BLAKE2b-256 | 2061ed8f82498e874fdf1941f24777dae9e3234dd5edb5367af8891f8892e66f |
swarmspawner-0.1.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5a8046cfb921e1af25f8a56fdabc056736dd10f38e45a210c240cebb426a9d41 |
|
MD5 | f37e6eb17038d81664f5bed990fee8e2 |
|
BLAKE2b-256 | 60ac418d2d1ad7a40b351f776da4ec4c730d5677c69d94c17e17f70fcf66b4ac |