jupyterhub - 在 Jupyterhub 中将 k8s secret 中的环境变量添加到 notebook
问题描述
我正在使用Zero to Jupyterhub Helm 包来将 Jupyterhub 部署到我们的 kubernetes 集群中。单个笔记本图像需要一些额外的环境变量(主要是数据库连接信息),我希望它们从 k8s 命名空间中的现有秘密中提取值。我该怎么做呢?
使用以下配置的天真方法不起作用:
singleuser:
extraEnv:
SECURE_ENVIRONMENT_VARIABLE:
valueFrom:
secretKeyRef:
name: secret
value: key
结果SECURE_ENVIRONMENT_VARIABLE
被设置为map[valueFrom:map[secretKeyRef:map[name:secret value:key]]]
.
我也尝试过按照KubeSpawner config docssingleuser.extraConfig
进行设置,但是如果你使用它来设置它显然会覆盖现有的环境变量,这会破坏系统:c.KubeSpawner.extra_container_config
env
extraConfig: |
c.KubeSpawner.extra_container_config = {
"env": [
{
"name": "SECURE_ENVIRONMENT_VARIABLE",
"value": "test" # even a hardcoded value results in the container failing
}
]
}
作为记录,我可以通过创建部署 .yamlhelm upgrade --debug --dry-run
并在必要时手动编辑它,我只是不知道如何将此信息获取到动态生成的 pod 上。
解决方案
在这里https://github.com/jupyterhub/kubespawner/issues/306#issuecomment-474934945 我提供了使用非字符串值设置环境变量的解决方案。
基本思想是使用 c.KubeSpawner.modify_pod_hook 将变量添加到 pod 规范中。
hub:
extraConfig:
ipaddress: |
from kubernetes import client
def modify_pod_hook(spawner, pod):
pod.spec.containers[0].env.append(client.V1EnvVar("MY_POD_IP", None, client.V1EnvVarSource(None, client.V1ObjectFieldSelector(None, "status.podIP"))))
return pod
c.KubeSpawner.modify_pod_hook = modify_pod_hook
推荐阅读
- python - Python3.8:最后一个输出文件未正确存储在磁盘上
- java - 如何使用邮递员在 JSON 对象数组中搜索特定对象
- twig - 在树枝中使用字符串变量作为对象名称
- docker - 使用新的 env_file 更新 in-docker-compose 容器
- r - 使用 geom_vline 在多个面的密度图中插入平均线
- python - 生成一个包含 100 个元素的列表,每个元素有 50% 的可能性为 0,有 50% 的可能性为 0 到 1 之间的随机数
- mysql - 从用户是公司所有者的表中选择记录
- php - 防止 PHP 中的直接访问表单成功页面
- reactjs - 查询内部/外部 React 上下文
- c++ - shell命令行中的“-o”是什么意思?