在 K8s 上部署 JupyterHub 时,可以让用户在启动 Notebook 时选择不同的资源配置(如 4G/8G 内存,不同节点类型)。

配置方式

通过 KubeSpawner.profile_list 配置多个资源配置选项。

values.yaml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
hub:
extraConfig:
profileList: |
c.KubeSpawner.profile_list = [
{
'display_name': '小型环境 (4G 内存)',
'description': '适合轻量级数据分析任务',
'kubespawner_override': {
'cpu_limit': 1,
'cpu_guarantee': 0.5,
'mem_limit': '4G',
'mem_guarantee': '2G',
'node_selector': {
'disktype': 'ssd',
'workload': 'general'
}
}
},
{
'display_name': '大型环境 (8G 内存)',
'description': '适合大规模数据处理和模型训练',
'kubespawner_override': {
'cpu_limit': 2,
'cpu_guarantee': 1,
'mem_limit': '8G',
'mem_guarantee': '4G',
'node_selector': {
'disktype': 'ssd',
'workload': 'compute'
}
}
},
{
'display_name': 'GPU 环境',
'description': '配备 GPU 用于深度学习',
'kubespawner_override': {
'cpu_limit': 4,
'mem_limit': '16G',
'extra_resource_limits': {
'nvidia.com/gpu': '1'
},
'node_selector': {
'accelerator': 'nvidia-tesla-v100'
}
}
}
]

配置项说明

资源限制

配置项 说明 示例
cpu_limit CPU 上限 2 (2 核)
cpu_guarantee CPU 保证值 1 (1 核)
mem_limit 内存上限 '8G'
mem_guarantee 内存保证值 '4G'

节点选择

1
2
3
4
5
'node_selector': {
'disktype': 'ssd', # 磁盘类型
'workload': 'compute', # 工作负载类型
'zone': 'us-east-1a' # 可用区
}

高级配置

Tolerations(容忍度)

1
2
3
4
5
6
7
8
'tolerations': [
{
'key': 'gpu',
'operator': 'Equal',
'value': 'true',
'effect': 'NoSchedule'
}
]

Affinity(亲和性)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'extra_pod_config': {
'affinity': {
'nodeAffinity': {
'requiredDuringSchedulingIgnoredDuringExecution': {
'nodeSelectorTerms': [{
'matchExpressions': [{
'key': 'disktype',
'operator': 'In',
'values': ['ssd']
}]
}]
}
}
}
}

部署配置

使用 Helm 部署:

1
2
3
4
5
6
7
8
RELEASE=jhub
NAMESPACE=jhub

helm upgrade --install $RELEASE jupyterhub/jupyterhub \
--namespace $NAMESPACE \
--create-namespace \
--version=3.0.0 \
--values config.yaml

参考文档

注意事项

  • 节点必须提前打好对应的标签:kubectl label nodes node1 disktype=ssd
  • cpu_limitmem_limit 会限制 Pod 的最大资源使用
  • cpu_guaranteemem_guarantee 保证 Pod 能获得的最小资源
  • GPU 资源需要先安装 NVIDIA Device Plugin