container

简介

container-executor 是NodeManager管理Container很重要的一个工具,是深入学习Yarn作业调度不可缺少的一个知识点,值得深入学习。本文只描述比较基础的功能点(目前不会包含Docker相关)。

核心功能点

checksetup

主要是检查container-executor的配置是否ok,没有其他功能。核心代码如下:

代码语言:c代码运行次数:0运行复制
case CHECK\_SETUP:

  //we already did this 

  exit\_code = 0;

  break;

mount-cgroups

在配置项feature.mount-cgroup.enabled为true的时候为nodemanager挂载cgroup。核心是调用系统函数mount。下面代码中的是配置的挂载点。由命令行参数传入。

代码语言:c代码运行次数:0运行复制
if (mount("none", mount\_path, "cgroup", 0, controller) == 0) {

  // 挂载成功

    if (mkdirs(hier\_path, perms) == 0) {

        change\_owner(hier\_path, nm\_uid, nm\_gid);

        // 修改子目录权限。

        chown\_dir\_contents(hier\_path, nm\_uid, nm\_gid);

    }

}

exec-container

**前提条件**:配置feature.terminal.enabled=true

当前功能的核心实现在container-executor.c的函数int exec\_container(const char \*command\_file)中。

在非Docker模式下,主要步骤如下:

代码语言:c代码运行次数:0运行复制
// 切换用户

if (change\_user(user\_detail->pw\_uid, user\_detail->pw\_gid) != 0) {

  \_exit(DOCKER\_EXEC\_FAILED);

}

// 切换工作目录

ret = chdir(workdir);

if (ret != 0) {

  fprintf(ERRORFILE, "chdir failed - %s", strerror(errno));

  \_exit(DOCKER\_EXEC\_FAILED);

}

// 执行启动脚本。

execve(binary, args, env);

fprintf(ERRORFILE, "exec failed - %s\n", strerror(errno));

\_exit(DOCKER\_EXEC\_FAILED);

最后会执行配置launch-command中的命令。当前步骤的核心应该主要是判断当前用户是否有权限启动Container。

启动Container

真正启动Container,参数格式如下:

container-executor <user> <yarn-user> <command> <command-args>

源代码中的解释如下:

代码语言:c代码运行次数:0运行复制
fprintf(stream,

    "       container-executor <user> <yarn-user> <command> <command-args>\n"

    "       where command and command-args: \n" \

    "            initialize container:  %2d appid containerid tokens nm-local-dirs "

    "nm-log-dirs cmd...\n"

    "            launch container:      %2d appid containerid workdir "

    "container-script tokens http-option pidfile nm-local-dirs nm-log-dirs resources ",

    INITIALIZE\_CONTAINER, LAUNCH\_CONTAINER);

可以看出提供了两个功能:

  • 初始化Container。
  • 启动Container。