在 HPC 作业队列上安装

在这里,我们将提供在 HPC 作业队列系统(如 PBSSlurm)上安装和配置 dask-gateway-server 的说明。请注意,dask-gateway-server 只需安装在一个节点上(通常是边缘节点)。

目前仅支持 PBSSlurm,但计划支持更多后端。如果您对此感兴趣,请提交一个 issue

创建一个用户账号

在安装任何东西之前,您需要创建一个用于运行 dask-gateway-server 进程的用户账号。用户的名称不重要,只有其拥有的权限重要。这里我们将使用 dask

$ adduser dask

创建安装目录

dask-gateway-server 安装有三种类型的文件,在安装前需要为其创建单独的目录:

  • 软件文件:这包括一个 Python 环境和所有必需的库。这里我们使用 /opt/dask-gateway

  • 配置文件:这里我们使用 /etc/dask-gateway

  • 运行时文件:这里我们使用 /var/dask-gateway

# Software files
$ mkdir -p /opt/dask-gateway

# Configuration files
$ mkdir /etc/dask-gateway

# Runtime files
$ mkdir /var/dask-gateway
$ chown dask /var/dask-gateway

安装 Python 环境

为了避免系统 Python 安装与 dask-gateway-server 之间的交互,我们将一个完整的 Python 环境安装到软件目录中。最简单的方法是使用 miniconda,但这并非硬性要求。

$ curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o /tmp/miniconda.sh
$ bash /tmp/miniconda.sh -b -p /opt/dask-gateway/miniconda
$ rm /tmp/miniconda.sh

我们还建议将 miniconda 添加到 root 用户的 PATH 中,以方便后续命令。

$ echo 'export PATH="/opt/dask-gateway/miniconda/bin:$PATH"' >> /root/.bashrc
$ source /root/.bashrc

安装 dask-gateway-server

现在我们可以安装 dask-gateway-server 及其依赖项了。

$ conda install -y -c conda-forge dask-gateway-server-jobqueue

dask 用户启用权限

为了使 dask-gateway-server 正常工作,您需要为 dask 用户账号启用 sudo 权限。最低限度,dask 账号需要无密码权限,以便以任何 dask-gateway 用户的身份运行 dask-gateway-jobqueue-launcher 命令(上面作为 dask-gateway-server 的一部分安装)。dask-gateway-jobqueue-launcher 脚本负责启动、跟踪和停止单个用户的批处理作业,因此需要以这些用户的身份执行 sudo,以便正确传递权限。

sudoers 中的一个示例条目可能如下所示:

Cmnd_Alias DASK_GATEWAY_JOBQUEUE_LAUNCHER = /opt/dask-gateway/miniconda/bin/dask-gateway-jobqueue-launcher

dask ALL=(%dask_users,!root) NOPASSWD:DASK_GATEWAY_JOBQUEUE_LAUNCHER

此外,在使用 PBS 时,您需要使 dask 用户成为 PBS 操作员。

$ qmgr -c "set server operators += dask@pbs"

PBS 中需要操作员级别的权限,以便 dask-gateway-server 能够更有效地跟踪所有用户作业的状态。

配置 dask-gateway-server

现在我们准备配置 dask-gateway-server 的安装。配置写在一个 Python 文件中(通常是 /etc/dask-gateway/dask_gateway_config.py)。选项被分配给一个配置对象 c,该对象随后在网关启动时加载。您可以在此文件中自由使用任何 Python 语法/库,对于 dask-gateway-server 来说,只有在 c 配置对象上设置的值重要。

在这里,我们将介绍一些您可能想要设置的常见配置选项。

指定后端

首先,您需要通过设置 c.DaskGateway.backend_class 来指定使用哪个后端。您有几个选项:

  • PBS: dask_gateway_server.backends.jobqueue.pbs.PBSBackend

  • Slurm: dask_gateway_server.backends.jobqueue.slurm.SlurmBackend

例如,这里我们配置网关使用 PBS 后端:

# Configure the gateway to use PBS
c.DaskGateway.backend_class = (
    "dask_gateway_server.backends.jobqueue.pbs.PBSBackend"
)

配置服务器地址 (可选)

默认情况下,dask-gateway-server 将通过 0.0.0.0:8000 提供所有流量。这包括 HTTP(S) 请求(REST API、仪表板等)和 Dask 调度器流量。

如果您想在不同的地址提供服务,或者在不同的端口上提供 Web 和调度器流量,您可以配置以下字段:

  • c.Proxy.address - 提供 HTTP(S) 流量,默认为 :8000

  • c.Proxy.tcp_address - 提供 Dask 客户端到调度器的 TCP 流量,默认为 c.Proxy.address

这里我们配置 Web 流量在 8000 端口提供,调度器流量在 8001 端口提供:

c.Proxy.address = ':8000'
c.Proxy.tcp_address = ':8001'

指定用户 Python 环境

由于 Dask worker/scheduler 将运行在集群中不同的节点上,您需要提供一种方式使 Python 环境在每个节点上可用。您有以下几种选择:

  • 使用每个节点上都有的 Python 环境的固定路径

  • 允许用户指定 Python 环境的位置 (推荐)

无论哪种情况,Python 环境都至少需要安装 dask-gateway 包才能正常工作。

使用固定环境路径

如果在每个节点上都有相同的 Python 环境(本地磁盘或 NFS 挂载),您只需配置 dask-gateway-server 使用提供的 Python。这可以通过几种不同的方式完成:

# Configure the paths to the dask-scheduler/dask-worker CLIs
c.JobQueueClusterConfig.scheduler_cmd = "/path/to/dask-scheduler"
c.JobQueueClusterConfig.worker_cmd = "/path/to/dask-worker"

# OR
# Activate a local conda environment before startup
c.JobQueueClusterConfig.scheduler_setup = 'source /path/to/miniconda/bin/activate /path/to/environment'
c.JobQueueClusterConfig.worker_setup = 'source /path/to/miniconda/bin/activate /path/to/environment'

# OR
# Activate a virtual environment before startup
c.JobQueueClusterConfig.scheduler_setup = 'source /path/to/your/environment/bin/activate'
c.JobQueueClusterConfig.worker_setup = 'source /path/to/your/environment/bin/activate'

用户可配置的 Python 环境

或者,您可能希望允许用户提供自己的 Python 环境。这很有用,因为它允许用户自己管理包版本,而无需联系管理员寻求支持。

这可以通过在 c.Backend.cluster_options 中暴露 Python 环境选项来完成。暴露集群选项在 暴露集群选项 中有详细讨论 - 这里我们只提供一个实现此目的的简短示例。有关更多信息,请参阅 暴露集群选项

from dask_gateway_server.options import Options, String

def options_handler(options):
    # Fill in environment activation command template with the users
    # provided environment name. This command is then used as the setup
    # script for both the scheduler and workers.
    setup = "source ~/miniconda/bin/activate %s" % options.environment
    return {"scheduler_setup": setup, "worker_setup": setup}

# Provide an option for users to specify the name or location of a
# conda environment to use for both the scheduler and workers.
# If not specified, the default environment of ``base`` is used.
c.Backend.cluster_options = Options(
    String("environment", default="base", label="Conda Environment"),
    handler=options_handler,
)

额外的配置选项

dask-gateway-server 有几个额外的配置字段。有关所有可用选项的更多信息,请参阅配置参考文档(特别是作业队列配置文档)。最低限度,您可能需要配置 worker 资源限制。

# The resource limits for a worker
c.JobQueueClusterConfig.worker_memory = '4 G'
c.JobQueueClusterConfig.worker_cores = 2

如果您的集群负载较高(且作业启动可能较慢),您可能还需要增加集群/worker 的超时值。

# Increase startup timeouts to 5 min (600 seconds) each
c.JobQueueClusterBackend.cluster_start_timeout = 600
c.JobQueueClusterBackend.worker_start_timeout = 600

示例

总而言之,一个用于 PBS 的 dask_gateway_config.py 配置示例可能如下所示:

# Configure the gateway to use PBS as the backend
c.DaskGateway.backend_class = "dask_gateway_server.backends.pbs.PBSBackend"

# Configure the paths to the dask-scheduler/dask-worker CLIs
c.PBSClusterConfig.scheduler_cmd = "~/miniconda/bin/dask-scheduler"
c.PBSClusterConfig.worker_cmd = "~/miniconda/bin/dask-worker"

# Limit resources for a single worker
c.PBSClusterConfig.worker_memory = '4 G'
c.PBSClusterConfig.worker_cores = 2

# Specify the PBS queue to use
c.PBSClusterConfig.queue = 'dask'

# Increase startup timeouts to 5 min (600 seconds) each
c.PBSClusterBackend.cluster_start_timeout = 600
c.PBSClusterBackend.worker_start_timeout = 600

开放相关端口

为了让用户访问网关服务器,他们需要访问上面配置服务器地址 (可选)中设置的公共端口(默认情况下是端口 8000)。如何暴露端口取决于系统 - 集群管理员应确定执行此任务的最佳方式。

启动 dask-gateway-server

此时,您应该能够以 dask 用户身份使用创建的配置文件启动网关服务器。dask-gateway-server 进程将是一个长时间运行的进程 - 您打算如何管理它(supervisord 等)取决于系统。要求是:

  • dask 用户启动

  • /var/dask-gateway 作为工作目录启动

  • /opt/dask-gateway/miniconda/bin 添加到 PATH

  • 使用 -f /etc/dask-gateway/dask_gateway_config.py 指定配置文件位置

为了方便起见,我们建议创建一个小型 bash 脚本并存储在 /opt/dask-gateway/start-dask-gateway 中来设置这些:

#!/usr/bin/env bash

export PATH="/opt/dask-gateway/miniconda/bin:$PATH"
cd /var/dask-gateway
dask-gateway-server -f /etc/dask-gateway/dask_gateway_config.py

对于测试,您可以这样手动启动 dask-gateway-server

$ cd /var/dask-gateway
$ sudo -iu dask /opt/dask-gateway/start-dask-gateway

验证是否正常工作

如果服务器启动时没有错误,您会想要检查是否一切正常。最简单的方法是尝试以用户身份连接。

用户环境需要安装 dask-gateway 库。

# Install the dask-gateway client library
$ conda create -n dask-gateway -c conda-forge dask-gateway

您可以通过创建一个 dask_gateway.Gateway 对象并指定公共地址来连接到网关(请注意,如果您配置了 c.Proxy.tcp_address,您还需要指定 proxy_address)。

>>> from dask_gateway import Gateway
>>> gateway = Gateway("http://public-address")

您现在应该能够进行 API 调用了。尝试 dask_gateway.Gateway.list_clusters(),这应该返回一个空列表。

>>> gateway.list_clusters()
[]

接下来,看看您是否可以创建一个集群。这可能需要几分钟。

>>> cluster = gateway.new_cluster()

您要检查的最后一件事是是否可以成功连接到新创建的集群。

>>> client = cluster.get_client()

如果一切正常,您可以使用 dask_gateway.GatewayCluster.shutdown() 关闭您的集群。

>>> cluster.shutdown()

作者:Jim Crist-Harif
© 版权所有 2021, Jim Crist-Harif。