diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/YarnConfiguration.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/YarnConfiguration.java index 4ea2188aafb59..77c860bb8c54d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/YarnConfiguration.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/YarnConfiguration.java @@ -848,7 +848,23 @@ public class YarnConfiguration extends Configuration { /** The arguments to pass to the health check script.*/ public static final String NM_HEALTH_CHECK_SCRIPT_OPTS = NM_PREFIX + "health-checker.script.opts"; - + + /** The Docker image name(For DockerContainerExecutor).*/ + public static final String NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME = + NM_PREFIX + "docker-container-executor.image-name"; + + /** Args passed to docker run(For DockerContainerExecutor).*/ + public static final String NM_DOCKER_CONTAINER_EXECUTOR_RUN_ARGS = + NM_PREFIX + "docker-container-executor.run-args"; + + /** The name of the docker executor (For DockerContainerExecutor).*/ + public static final String NM_DOCKER_CONTAINER_EXECUTOR_EXEC_NAME = + NM_PREFIX + "docker-container-executor.exec-name"; + + /** The default docker executor (For DockerContainerExecutor).*/ + public static final String NM_DEFAULT_DOCKER_CONTAINER_EXECUTOR_EXEC_NAME = + "/usr/bin/docker"; + /** The path to the Linux container executor.*/ public static final String NM_LINUX_CONTAINER_EXECUTOR_PATH = NM_PREFIX + "linux-container-executor.path"; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/resources/yarn-default.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/resources/yarn-default.xml index 866aee71e401c..df92ab7994777 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/resources/yarn-default.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/resources/yarn-default.xml @@ -23,7 +23,7 @@ - + Factory to create client IPC classes. @@ -1113,6 +1113,32 @@ ${hadoop.tmp.dir}/yarn-nm-recovery + + + yarn.nodemanager.docker-container-executor.image-name + + + This image is used by all nodemanagers to launch containers. + This maybe modified by the users. + + + + + yarn.nodemanager.docker-container-executor.run-args + --rm --net=host + + This arguments to pass to the 'docker run' invocation. + + + + + yarn.nodemanager.docker-container-executor.exec-name + /usr/bin/docker + + Name or path to the Docker client. + + + yarn.nodemanager.aux-services.mapreduce_shuffle.class diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/DockerContainerExecutor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/DockerContainerExecutor.java new file mode 100644 index 0000000000000..a518bdb6ec90f --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/DockerContainerExecutor.java @@ -0,0 +1,559 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.nodemanager; + +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.fs.CommonConfigurationKeys; +import org.apache.hadoop.fs.FileContext; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.UnsupportedFileSystemException; +import org.apache.hadoop.fs.permission.FsPermission; +import org.apache.hadoop.io.IOUtils; +import org.apache.hadoop.util.Shell; +import org.apache.hadoop.util.Shell.ShellCommandExecutor; +import org.apache.hadoop.util.StringUtils; +import org.apache.hadoop.yarn.api.records.ContainerId; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider; +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container; +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerDiagnosticsUpdateEvent; +import org.apache.hadoop.yarn.server.nodemanager.containermanager.launcher.ContainerLaunch; +import org.apache.hadoop.yarn.server.nodemanager.containermanager.localizer.ContainerLocalizer; +import org.apache.hadoop.yarn.util.ConverterUtils; + +import java.util.regex.Pattern; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumSet; +import java.util.List; + +import static org.apache.hadoop.fs.CreateFlag.CREATE; +import static org.apache.hadoop.fs.CreateFlag.OVERWRITE; + +/** + * This executor will launch a docker container and run the task inside the container. + */ +public class DockerContainerExecutor extends ContainerExecutor { + + private static final Log LOG = LogFactory + .getLog(DockerContainerExecutor.class); + public static final String DOCKER_CONTAINER_EXECUTOR_SCRIPT = "docker_container_executor"; + public static final String DOCKER_IMAGE_PATTERN = "^(([\\w\\.-]+)(:\\d+)*\\/)?[\\w:-]+$"; + + private final FileContext lfs; + private final Pattern dockerImagePattern; + + public DockerContainerExecutor() { + try { + this.lfs = FileContext.getLocalFSFileContext(); + this.dockerImagePattern = Pattern.compile(DOCKER_IMAGE_PATTERN); + } catch (UnsupportedFileSystemException e) { + throw new RuntimeException(e); + } + } + + @Override + public void init() throws IOException { + String auth = getConf().get(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION); + if (auth != null && !auth.equals("simple")) { + throw new IllegalStateException("DockerContainerExecutor only works with simple authentication mode"); + } + } + + @Override + public synchronized void startLocalizer(Path nmPrivateContainerTokensPath, + InetSocketAddress nmAddr, String user, String appId, String locId, + List localDirs, List logDirs) + throws IOException, InterruptedException { + + ContainerLocalizer localizer = + new ContainerLocalizer(lfs, user, appId, locId, getPaths(localDirs), + RecordFactoryProvider.getRecordFactory(getConf())); + + createUserLocalDirs(localDirs, user); + createUserCacheDirs(localDirs, user); + createAppDirs(localDirs, user, appId); + createAppLogDirs(appId, logDirs); + + Path appStorageDir = getFirstApplicationDir(localDirs, user, appId); + String tokenFn = String.format(ContainerLocalizer.TOKEN_FILE_NAME_FMT, locId); + Path tokenDst = new Path(appStorageDir, tokenFn); + lfs.util().copy(nmPrivateContainerTokensPath, tokenDst); + lfs.setWorkingDirectory(appStorageDir); + localizer.runLocalization(nmAddr); + } + + @Override + public int launchContainer(Container container, + Path nmPrivateContainerScriptPath, Path nmPrivateTokensPath, + String userName, String appId, Path containerWorkDir, + List localDirs, List logDirs) throws IOException { + String containerImageName = container.getLaunchContext().getEnvironment() + .get(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME); + if (LOG.isDebugEnabled()) { + LOG.debug("containerImageName from launchContext: " + containerImageName); + } + containerImageName = containerImageName == null ? + getConf().get(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME) + : containerImageName; + Preconditions.checkArgument(!Strings.isNullOrEmpty(containerImageName), "Container image must not be null"); + Preconditions.checkArgument(saneDockerImage(containerImageName), "Image: " + containerImageName + " is not a proper docker image"); + String containerArgs = Strings.nullToEmpty(getConf().get(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_RUN_ARGS)); + String dockerExecutor = getConf().get(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_EXEC_NAME, + YarnConfiguration.NM_DEFAULT_DOCKER_CONTAINER_EXECUTOR_EXEC_NAME); + + FsPermission dirPerm = new FsPermission(APPDIR_PERM); + ContainerId containerId = container.getContainerId(); + + // create container dirs on all disks + String containerIdStr = ConverterUtils.toString(containerId); + String appIdStr = + ConverterUtils.toString( + containerId.getApplicationAttemptId(). + getApplicationId()); + for (String sLocalDir : localDirs) { + Path usersdir = new Path(sLocalDir, ContainerLocalizer.USERCACHE); + Path userdir = new Path(usersdir, userName); + Path appCacheDir = new Path(userdir, ContainerLocalizer.APPCACHE); + Path appDir = new Path(appCacheDir, appIdStr); + Path containerDir = new Path(appDir, containerIdStr); + createDir(containerDir, dirPerm, true); + } + + // Create the container log-dirs on all disks + createContainerLogDirs(appIdStr, containerIdStr, logDirs); + + Path tmpDir = new Path(containerWorkDir, + YarnConfiguration.DEFAULT_CONTAINER_TEMP_DIR); + createDir(tmpDir, dirPerm, false); + + // copy launch script to work dir + Path launchDst = + new Path(containerWorkDir, ContainerLaunch.CONTAINER_SCRIPT); + lfs.util().copy(nmPrivateContainerScriptPath, launchDst); + + // copy container tokens to work dir + Path tokenDst = + new Path(containerWorkDir, ContainerLaunch.FINAL_CONTAINER_TOKENS_FILE); + lfs.util().copy(nmPrivateTokensPath, tokenDst); + + // Create new local launch wrapper script + LocalWrapperScriptBuilder sb = + new UnixLocalWrapperScriptBuilder(containerWorkDir); + + String localDirMount = toMount(localDirs); + String logDirMount = toMount(logDirs); + String containerWorkDirMount = toMount(Collections.singletonList(containerWorkDir.toUri().getPath())); + StringBuilder commands = new StringBuilder(); + String commandStr = commands.append(dockerExecutor) + .append(" ") + .append("run") + .append(" --name " + containerIdStr) + .append(localDirMount) + .append(logDirMount) + .append(containerWorkDirMount) + .append(" ") + .append(containerArgs) + .append(" ") + .append(containerImageName) + .toString(); + Path pidFile = getPidFilePath(containerId); + if (pidFile != null) { + sb.writeLocalWrapperScript(launchDst, pidFile, commandStr); + } else { + LOG.info("Container " + containerIdStr + + " was marked as inactive. Returning terminated error"); + return ExitCode.TERMINATED.getExitCode(); + } + + // create log dir under app + // fork script + ShellCommandExecutor shExec = null; + try { + lfs.setPermission(launchDst, + ContainerExecutor.TASK_LAUNCH_SCRIPT_PERMISSION); + lfs.setPermission(sb.getWrapperScriptPath(), + ContainerExecutor.TASK_LAUNCH_SCRIPT_PERMISSION); + + // Setup command to run + String[] command = getRunCommand(sb.getWrapperScriptPath().toString(), + containerIdStr, this.getConf()); + if (LOG.isDebugEnabled()) { + LOG.debug("launchContainer: " + commandStr + " " + Joiner.on(" ").join(command)); + } + shExec = new ShellCommandExecutor( + command, + new File(containerWorkDir.toUri().getPath()), + container.getLaunchContext().getEnvironment()); // sanitized env + if (isContainerActive(containerId)) { + shExec.execute(); + } else { + LOG.info("Container " + containerIdStr + + " was marked as inactive. Returning terminated error"); + return ExitCode.TERMINATED.getExitCode(); + } + } catch (IOException e) { + if (null == shExec) { + return -1; + } + int exitCode = shExec.getExitCode(); + LOG.warn("Exit code from container " + containerId + " is : " + exitCode); + // 143 (SIGTERM) and 137 (SIGKILL) exit codes means the container was + // terminated/killed forcefully. In all other cases, log the + // container-executor's output + if (exitCode != ExitCode.FORCE_KILLED.getExitCode() + && exitCode != ExitCode.TERMINATED.getExitCode()) { + LOG.warn("Exception from container-launch with container ID: " + + containerId + " and exit code: " + exitCode, e); + logOutput(shExec.getOutput()); + String diagnostics = "Exception from container-launch: \n" + + StringUtils.stringifyException(e) + "\n" + shExec.getOutput(); + container.handle(new ContainerDiagnosticsUpdateEvent(containerId, + diagnostics)); + } else { + container.handle(new ContainerDiagnosticsUpdateEvent(containerId, + "Container killed on request. Exit code is " + exitCode)); + } + return exitCode; + } finally { + ; // + } + return 0; + } + + private boolean saneDockerImage(String containerImageName) { + return dockerImagePattern.matcher(containerImageName).matches(); + } + + @Override + public boolean signalContainer(String user, String pid, Signal signal) throws IOException { + return false; + } + + @Override + public void deleteAsUser(String user, Path subDir, Path... basedirs) throws IOException, InterruptedException { + + } + + @Override + public boolean isContainerProcessAlive(String user, String pid) throws IOException { + return false; + } + + private String toMount(List dirs) { + StringBuilder builder = new StringBuilder(); + for (String dir : dirs) { + builder.append(" -v " + dir + ":" + dir); + } + return builder.toString(); + } + + private abstract class LocalWrapperScriptBuilder { + + private final Path wrapperScriptPath; + + public Path getWrapperScriptPath() { + return wrapperScriptPath; + } + + public void writeLocalWrapperScript(Path launchDst, Path pidFile, String commandStr) throws IOException { + DataOutputStream out = null; + PrintStream pout = null; + + try { + out = lfs.create(wrapperScriptPath, EnumSet.of(CREATE, OVERWRITE)); + pout = new PrintStream(out); + writeLocalWrapperScript(launchDst, pidFile, pout, commandStr); + } finally { + IOUtils.cleanup(LOG, pout, out); + } + } + + protected abstract void writeLocalWrapperScript(Path launchDst, Path pidFile, + PrintStream pout, String commandStr); + + protected LocalWrapperScriptBuilder(Path containerWorkDir) { + this.wrapperScriptPath = new Path(containerWorkDir, + Shell.appendScriptExtension(DOCKER_CONTAINER_EXECUTOR_SCRIPT)); + } + } + + private final class UnixLocalWrapperScriptBuilder + extends LocalWrapperScriptBuilder { + + public UnixLocalWrapperScriptBuilder(Path containerWorkDir) { + super(containerWorkDir); + } + + @Override + public void writeLocalWrapperScript(Path launchDst, Path pidFile, + PrintStream pout, String commandStr) { + + // We need to do a move as writing to a file is not atomic + // Process reading a file being written to may get garbled data + // hence write pid to tmp file first followed by a mv + pout.println("#!/usr/bin/env bash"); + pout.println(); + + pout.println("echo $$ > " + pidFile.toString() + ".tmp"); + pout.println("/bin/mv -f " + pidFile.toString() + ".tmp " + pidFile); + String exec = commandStr; + pout.println(exec + " bash \"" + + launchDst.toUri().getPath().toString() + "\""); + } + } + + private void createDir(Path dirPath, FsPermission perms, + boolean createParent) throws IOException { + lfs.mkdir(dirPath, perms, createParent); + if (!perms.equals(perms.applyUMask(lfs.getUMask()))) { + lfs.setPermission(dirPath, perms); + } + } + + /** + * Initialize the local directories for a particular user. + *
    .mkdir + *
  • $local.dir/usercache/$user
  • + *
+ */ + void createUserLocalDirs(List localDirs, String user) + throws IOException { + boolean userDirStatus = false; + FsPermission userperms = new FsPermission(USER_PERM); + for (String localDir : localDirs) { + // create $local.dir/usercache/$user and its immediate parent + try { + createDir(getUserCacheDir(new Path(localDir), user), userperms, true); + } catch (IOException e) { + LOG.warn("Unable to create the user directory : " + localDir, e); + continue; + } + userDirStatus = true; + } + if (!userDirStatus) { + throw new IOException("Not able to initialize user directories " + + "in any of the configured local directories for user " + user); + } + } + + + /** + * Initialize the local cache directories for a particular user. + *
    + *
  • $local.dir/usercache/$user
  • + *
  • $local.dir/usercache/$user/appcache
  • + *
  • $local.dir/usercache/$user/filecache
  • + *
+ */ + void createUserCacheDirs(List localDirs, String user) + throws IOException { + if (LOG.isDebugEnabled()) { + LOG.debug("Initializing user " + user); + } + boolean appcacheDirStatus = false; + boolean distributedCacheDirStatus = false; + FsPermission appCachePerms = new FsPermission(APPCACHE_PERM); + FsPermission fileperms = new FsPermission(FILECACHE_PERM); + + for (String localDir : localDirs) { + // create $local.dir/usercache/$user/appcache + Path localDirPath = new Path(localDir); + final Path appDir = getAppcacheDir(localDirPath, user); + try { + createDir(appDir, appCachePerms, true); + appcacheDirStatus = true; + } catch (IOException e) { + LOG.warn("Unable to create app cache directory : " + appDir, e); + } + // create $local.dir/usercache/$user/filecache + final Path distDir = getFileCacheDir(localDirPath, user); + try { + createDir(distDir, fileperms, true); + distributedCacheDirStatus = true; + } catch (IOException e) { + LOG.warn("Unable to create file cache directory : " + distDir, e); + } + } + if (!appcacheDirStatus) { + throw new IOException("Not able to initialize app-cache directories " + + "in any of the configured local directories for user " + user); + } + if (!distributedCacheDirStatus) { + throw new IOException( + "Not able to initialize distributed-cache directories " + + "in any of the configured local directories for user " + + user); + } + } + + /** + * Initialize the local directories for a particular user. + *
    + *
  • $local.dir/usercache/$user/appcache/$appid
  • + *
+ * + * @param localDirs + */ + void createAppDirs(List localDirs, String user, String appId) + throws IOException { + boolean initAppDirStatus = false; + FsPermission appperms = new FsPermission(APPDIR_PERM); + for (String localDir : localDirs) { + Path fullAppDir = getApplicationDir(new Path(localDir), user, appId); + // create $local.dir/usercache/$user/appcache/$appId + try { + createDir(fullAppDir, appperms, true); + initAppDirStatus = true; + } catch (IOException e) { + LOG.warn("Unable to create app directory " + fullAppDir.toString(), e); + } + } + if (!initAppDirStatus) { + throw new IOException("Not able to initialize app directories " + + "in any of the configured local directories for app " + + appId.toString()); + } + } + + + /** + * Create application log directories on all disks. + */ + void createContainerLogDirs(String appId, String containerId, + List logDirs) throws IOException { + + boolean containerLogDirStatus = false; + FsPermission containerLogDirPerms = new FsPermission(LOGDIR_PERM); + for (String rootLogDir : logDirs) { + // create $log.dir/$appid/$containerid + Path appLogDir = new Path(rootLogDir, appId); + Path containerLogDir = new Path(appLogDir, containerId); + try { + createDir(containerLogDir, containerLogDirPerms, true); + } catch (IOException e) { + LOG.warn("Unable to create the container-log directory : " + + appLogDir, e); + continue; + } + containerLogDirStatus = true; + } + if (!containerLogDirStatus) { + throw new IOException( + "Not able to initialize container-log directories " + + "in any of the configured local directories for container " + + containerId); + } + } + + /** + * Permissions for user dir. + * $local.dir/usercache/$user + */ + static final short USER_PERM = (short) 0750; + /** + * Permissions for user appcache dir. + * $local.dir/usercache/$user/appcache + */ + static final short APPCACHE_PERM = (short) 0710; + /** + * Permissions for user filecache dir. + * $local.dir/usercache/$user/filecache + */ + static final short FILECACHE_PERM = (short) 0710; + /** + * Permissions for user app dir. + * $local.dir/usercache/$user/appcache/$appId + */ + static final short APPDIR_PERM = (short) 0710; + /** + * Permissions for user log dir. + * $logdir/$user/$appId + */ + static final short LOGDIR_PERM = (short) 0710; + + private Path getFirstApplicationDir(List localDirs, String user, + String appId) { + return getApplicationDir(new Path(localDirs.get(0)), user, appId); + } + + private Path getApplicationDir(Path base, String user, String appId) { + return new Path(getAppcacheDir(base, user), appId); + } + + private Path getUserCacheDir(Path base, String user) { + return new Path(new Path(base, ContainerLocalizer.USERCACHE), user); + } + + private Path getAppcacheDir(Path base, String user) { + return new Path(getUserCacheDir(base, user), + ContainerLocalizer.APPCACHE); + } + + private Path getFileCacheDir(Path base, String user) { + return new Path(getUserCacheDir(base, user), + ContainerLocalizer.FILECACHE); + } + + /** + * Create application log directories on all disks. + */ + void createAppLogDirs(String appId, List logDirs) + throws IOException { + + boolean appLogDirStatus = false; + FsPermission appLogDirPerms = new FsPermission(LOGDIR_PERM); + for (String rootLogDir : logDirs) { + // create $log.dir/$appid + Path appLogDir = new Path(rootLogDir, appId); + try { + createDir(appLogDir, appLogDirPerms, true); + } catch (IOException e) { + LOG.warn("Unable to create the app-log directory : " + appLogDir, e); + continue; + } + appLogDirStatus = true; + } + if (!appLogDirStatus) { + throw new IOException("Not able to initialize app-log directories " + + "in any of the configured local directories for app " + appId); + } + } + + /** + * @return the list of paths of given local directories + */ + private static List getPaths(List dirs) { + List paths = new ArrayList(dirs.size()); + for (int i = 0; i < dirs.size(); i++) { + paths.add(new Path(dirs.get(i))); + } + return paths; + } + +} \ No newline at end of file diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java index cee6a40a5585b..fd8b6e31986e7 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java @@ -20,7 +20,7 @@ import static org.apache.hadoop.fs.CreateFlag.CREATE; import static org.apache.hadoop.fs.CreateFlag.OVERWRITE; - +import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.EnumSet; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -256,6 +257,31 @@ public Integer call() { lfs.create(nmPrivateContainerScriptPath, EnumSet.of(CREATE, OVERWRITE)); + // Set the HADOOP_* prefix environment variables + // from the host to the container. + Map orderedEnv = new LinkedHashMap(); + Map allNMEnv = System.getenv(); + for(Environment env: ApplicationConstants.Environment.values()) { + if (env == Environment.CLASSPATH){ + // Skip CLASSPATH so that it's at the end + continue; + } + putEnvIfNotNull(orderedEnv, + env.key(), + allNMEnv.get(env.key())); + } + for(Map.Entry entry: allNMEnv.entrySet()){ + if (entry.getKey().matches("^HADOOP_\\w+")){ + putEnvIfNotNull(orderedEnv, + entry.getKey(), + entry.getValue() + ); + } + } + if (LOG.isDebugEnabled()){ + LOG.debug("Environment: " + orderedEnv + " and "+ environment); + } + // Set the token location too. environment.put( ApplicationConstants.CONTAINER_TOKEN_FILE_ENV_NAME, @@ -264,11 +290,11 @@ public Integer call() { // Sanitize the container's environment sanitizeEnv(environment, containerWorkDir, appDirs, containerLogDirs, localResources); - + orderedEnv.putAll(environment); // Write out the environment - writeLaunchEnv(containerScriptOutStream, environment, localResources, + writeLaunchEnv(containerScriptOutStream, orderedEnv, localResources, launchContext.getCommands()); - + // /////////// End of writing out container-script // /////////// Write out the container-tokens in the nmPrivate space. @@ -798,13 +824,24 @@ static void writeLaunchEnv(OutputStream out, sb.command(command); PrintStream pout = null; + ByteArrayOutputStream baos = null; try { pout = new PrintStream(out); sb.write(pout); + if(LOG.isDebugEnabled()) { + baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + sb.write(ps); + LOG.debug("container script: " + baos.toString()); + + } } finally { if (out != null) { out.close(); } + if (baos != null){ + baos.close(); + } } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/TestDockerContainerExecutor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/TestDockerContainerExecutor.java new file mode 100644 index 0000000000000..56a7cf988b7af --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/TestDockerContainerExecutor.java @@ -0,0 +1,244 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.nodemanager; + +import com.google.common.base.Strings; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileContext; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.permission.FsPermission; +import org.apache.hadoop.util.Shell; +import org.apache.hadoop.yarn.api.ApplicationConstants; +import org.apache.hadoop.yarn.api.records.ContainerId; +import org.apache.hadoop.yarn.api.records.ContainerLaunchContext; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container; +import org.apache.hadoop.yarn.util.ConverterUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * This is intended to test the DockerContainerExecutor code, but it requires docker + * to be installed. + *
    + *
  1. Install docker, and Compile the code with docker-service-url set to the host and port + * where docker service is running. + *
    
    + * > mvn clean install -Ddocker-service-url=tcp://0.0.0.0:4243
    + *                          -DskipTests
    + * 
    + */ +public class TestDockerContainerExecutor { + private static final Log LOG = LogFactory + .getLog(TestDockerContainerExecutor.class); + private static File workSpace = null; + private DockerContainerExecutor exec = null; + private LocalDirsHandlerService dirsHandler; + private Path workDir; + private FileContext lfs; + private String yarnImage; + + private int id = 0; + private String appSubmitter; + private String dockerUrl; + private String testImage = "centos"; + private String dockerExec; + private String containerIdStr; + + + private ContainerId getNextContainerId() { + ContainerId cId = mock(ContainerId.class, RETURNS_DEEP_STUBS); + String id = "CONTAINER_" + System.currentTimeMillis(); + when(cId.toString()).thenReturn(id); + return cId; + } + + @Before + public void setup() { + try { + lfs = FileContext.getLocalFSFileContext(); + workDir = new Path("/tmp/temp-" + System.currentTimeMillis()); + workSpace = new File(workDir.toUri().getPath()); + lfs.mkdir(workDir, FsPermission.getDirDefault(), true); + } catch (IOException e) { + throw new RuntimeException(e); + } + Configuration conf = new Configuration(); + yarnImage = "yarnImage"; + long time = System.currentTimeMillis(); + conf.set(YarnConfiguration.NM_LOCAL_DIRS, "/tmp/nm-local-dir" + time); + conf.set(YarnConfiguration.NM_LOG_DIRS, "/tmp/userlogs" + time); + + dockerUrl = System.getProperty("docker-service-url"); + LOG.info("dockerUrl: " + dockerUrl); + if (Strings.isNullOrEmpty(dockerUrl)) { + return; + } + dockerUrl = " -H " + dockerUrl; + dockerExec = "docker " + dockerUrl; + conf.set(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, yarnImage); + conf.set(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_EXEC_NAME, dockerExec); + exec = new DockerContainerExecutor(); + dirsHandler = new LocalDirsHandlerService(); + dirsHandler.init(conf); + exec.setConf(conf); + appSubmitter = System.getProperty("application.submitter"); + if (appSubmitter == null || appSubmitter.isEmpty()) { + appSubmitter = "nobody"; + } + shellExec(dockerExec + " pull " + testImage); + + } + + private Shell.ShellCommandExecutor shellExec(String command) { + try { + + Shell.ShellCommandExecutor shExec = new Shell.ShellCommandExecutor( + command.split("\\s+"), + new File(workDir.toUri().getPath()), + System.getenv()); + shExec.execute(); + return shExec; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private boolean shouldRun() { + return exec != null; + } + + private int runAndBlock(ContainerId cId, Map launchCtxEnv, String... cmd) throws IOException { + String appId = "APP_" + System.currentTimeMillis(); + Container container = mock(Container.class); + ContainerLaunchContext context = mock(ContainerLaunchContext.class); + + when(container.getContainerId()).thenReturn(cId); + when(container.getLaunchContext()).thenReturn(context); + when(cId.getApplicationAttemptId().getApplicationId().toString()).thenReturn(appId); + when(context.getEnvironment()).thenReturn(launchCtxEnv); + + String script = writeScriptFile(launchCtxEnv, cmd); + + Path scriptPath = new Path(script); + Path tokensPath = new Path("/dev/null"); + Path workDir = new Path(workSpace.getAbsolutePath()); + Path pidFile = new Path(workDir, "pid.txt"); + + exec.activateContainer(cId, pidFile); + return exec.launchContainer(container, scriptPath, tokensPath, + appSubmitter, appId, workDir, dirsHandler.getLocalDirs(), + dirsHandler.getLogDirs()); + } + + private String writeScriptFile(Map launchCtxEnv, String... cmd) throws IOException { + File f = File.createTempFile("TestDockerContainerExecutor", ".sh"); + f.deleteOnExit(); + PrintWriter p = new PrintWriter(new FileOutputStream(f)); + for(Map.Entry entry: launchCtxEnv.entrySet()) { + p.println("export " + entry.getKey() + "=\"" + entry.getValue() + "\""); + } + for (String part : cmd) { + p.print(part.replace("\\", "\\\\").replace("'", "\\'")); + p.print(" "); + } + p.println(); + p.close(); + return f.getAbsolutePath(); + } + + @After + public void tearDown() { + try { + lfs.delete(workDir, true); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Test + public void testLaunchContainer() throws IOException { + if (!shouldRun()) { + LOG.warn("Docker not installed, aborting test."); + return; + } + + Map env = new HashMap(); + env.put(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage); + String touchFileName = "touch-file-" + System.currentTimeMillis(); + File touchFile = new File(dirsHandler.getLocalDirs().get(0), touchFileName); + ContainerId cId = getNextContainerId(); + int ret = runAndBlock( + cId, env, "touch", touchFile.getAbsolutePath(), "&&", "cp", touchFile.getAbsolutePath(), "/"); + + assertEquals(0, ret); + //get the script + //you've launched containers. Now you should verify + //whether docker actually launched any containers, and how they exited + containerIdStr = ConverterUtils.toString(cId); + String dockerExitCode = shellExec(dockerExec + + " inspect --format {{.State.ExitCode}} " + containerIdStr).getOutput(); + assertEquals("0", dockerExitCode.trim()); + String touchFilePathLocal = "/tmp"; + //copy the touch file to local + shellExec(dockerExec + " cp " + containerIdStr + + ":/" + touchFileName + " " + touchFilePathLocal); + assertEquals(0, shellExec("ls " + touchFilePathLocal + "/" + touchFileName).getExitCode()); + } + + @Test + public void testLaunchContainerJava() throws IOException { + if (!shouldRun()) { + LOG.warn("Docker not installed, aborting test."); + return; + } + testImage = "java:8"; + Map env = new HashMap(); + env.put(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage); + env.put(ApplicationConstants.Environment.JAVA_HOME.name(), "/usr/lib/jvm/java-8-openjdk-amd64"); + ContainerId cId = getNextContainerId(); + int ret = runAndBlock( + cId, env, "$JAVA_HOME/bin/java", "-version"); + + assertEquals(0, ret); + } + + +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/TestDockerContainerExecutorWithMocks.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/TestDockerContainerExecutorWithMocks.java new file mode 100644 index 0000000000000..f44cdce9dafdd --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/TestDockerContainerExecutorWithMocks.java @@ -0,0 +1,317 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.nodemanager; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.CommonConfigurationKeys; +import org.apache.hadoop.fs.FileContext; +import org.apache.hadoop.fs.FileUtil; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.permission.FsPermission; +import org.apache.hadoop.util.Shell; +import org.apache.hadoop.yarn.api.records.ContainerId; +import org.apache.hadoop.yarn.api.records.ContainerLaunchContext; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.LineNumberReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; + +/** + * Mock tests for docker container executor + */ +public class TestDockerContainerExecutorWithMocks { + + private static final Log LOG = LogFactory + .getLog(TestDockerContainerExecutorWithMocks.class); + public static final String DOCKER_LAUNCH_COMMAND = "/bin/true"; + public static final String DOCKER_LAUNCH_ARGS = "-args"; + private DockerContainerExecutor dockerContainerExecutor = null; + private LocalDirsHandlerService dirsHandler; + private Path workDir; + private FileContext lfs; + private String yarnImage; + + @Before + public void setup() { + assumeTrue(!Path.WINDOWS); + File f = new File("./src/test/resources/mock-container-executor"); + if(!FileUtil.canExecute(f)) { + FileUtil.setExecutable(f, true); + } + String executorPath = f.getAbsolutePath(); + Configuration conf = new Configuration(); + yarnImage = "yarnImage"; + long time = System.currentTimeMillis(); + conf.set(YarnConfiguration.NM_LINUX_CONTAINER_EXECUTOR_PATH, executorPath); + conf.set(YarnConfiguration.NM_LOCAL_DIRS, "/tmp/nm-local-dir" + time); + conf.set(YarnConfiguration.NM_LOG_DIRS, "/tmp/userlogs" + time); + conf.set(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, yarnImage); + conf.set(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_EXEC_NAME , DOCKER_LAUNCH_COMMAND); + conf.set(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_RUN_ARGS, DOCKER_LAUNCH_ARGS); + dockerContainerExecutor = new DockerContainerExecutor(); + dirsHandler = new LocalDirsHandlerService(); + dirsHandler.init(conf); + dockerContainerExecutor.setConf(conf); + lfs = null; + try { + lfs = FileContext.getLocalFSFileContext(); + workDir = new Path("/tmp/temp-"+ System.currentTimeMillis()); + lfs.mkdir(workDir, FsPermission.getDirDefault(), true); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + @After + public void tearDown() { + try { + lfs.delete(workDir, true); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Test(expected = IllegalStateException.class) + public void testContainerInitSecure() throws IOException { + dockerContainerExecutor.getConf().set( + CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos"); + dockerContainerExecutor.init(); + } + + @Test(expected = IllegalArgumentException.class) + public void testContainerLaunchNullImage() throws IOException { + String appSubmitter = "nobody"; + String appId = "APP_ID"; + String containerId = "CONTAINER_ID"; + String testImage = ""; + + Container container = mock(Container.class, RETURNS_DEEP_STUBS); + ContainerId cId = mock(ContainerId.class, RETURNS_DEEP_STUBS); + ContainerLaunchContext context = mock(ContainerLaunchContext.class); + HashMap env = new HashMap(); + + when(container.getContainerId()).thenReturn(cId); + when(container.getLaunchContext()).thenReturn(context); + when(cId.getApplicationAttemptId().getApplicationId().toString()).thenReturn(appId); + when(cId.toString()).thenReturn(containerId); + + when(context.getEnvironment()).thenReturn(env); + env.put(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage); + dockerContainerExecutor.getConf() + .set(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage); + Path scriptPath = new Path("file:///bin/echo"); + Path tokensPath = new Path("file:///dev/null"); + + Path pidFile = new Path(workDir, "pid.txt"); + + dockerContainerExecutor.activateContainer(cId, pidFile); + dockerContainerExecutor.launchContainer(container, scriptPath, tokensPath, + appSubmitter, appId, workDir, dirsHandler.getLocalDirs(), + dirsHandler.getLogDirs()); + } + + @Test(expected = IllegalArgumentException.class) + public void testContainerLaunchInvalidImage() throws IOException { + String appSubmitter = "nobody"; + String appId = "APP_ID"; + String containerId = "CONTAINER_ID"; + String testImage = "testrepo.com/test-image rm -rf $HADOOP_PREFIX/*"; + + Container container = mock(Container.class, RETURNS_DEEP_STUBS); + ContainerId cId = mock(ContainerId.class, RETURNS_DEEP_STUBS); + ContainerLaunchContext context = mock(ContainerLaunchContext.class); + HashMap env = new HashMap(); + + when(container.getContainerId()).thenReturn(cId); + when(container.getLaunchContext()).thenReturn(context); + when(cId.getApplicationAttemptId().getApplicationId().toString()).thenReturn(appId); + when(cId.toString()).thenReturn(containerId); + + when(context.getEnvironment()).thenReturn(env); + env.put(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage); + dockerContainerExecutor.getConf() + .set(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage); + Path scriptPath = new Path("file:///bin/echo"); + Path tokensPath = new Path("file:///dev/null"); + + Path pidFile = new Path(workDir, "pid.txt"); + + dockerContainerExecutor.activateContainer(cId, pidFile); + dockerContainerExecutor.launchContainer(container, scriptPath, tokensPath, + appSubmitter, appId, workDir, dirsHandler.getLocalDirs(), + dirsHandler.getLogDirs()); + } + + @Test + public void testContainerLaunchYarnImage() throws IOException { + String appSubmitter = "nobody"; + String appId = "APP_ID"; + String containerId = "CONTAINER_ID"; + + Container container = mock(Container.class, RETURNS_DEEP_STUBS); + ContainerId cId = mock(ContainerId.class, RETURNS_DEEP_STUBS); + ContainerLaunchContext context = mock(ContainerLaunchContext.class); + HashMap env = new HashMap(); + + when(container.getContainerId()).thenReturn(cId); + when(container.getLaunchContext()).thenReturn(context); + when(cId.getApplicationAttemptId().getApplicationId().toString()).thenReturn(appId); + when(cId.toString()).thenReturn(containerId); + + when(context.getEnvironment()).thenReturn(env); + Path scriptPath = new Path("file:///bin/echo"); + Path tokensPath = new Path("file:///dev/null"); + + Path pidFile = new Path(workDir, "pid.txt"); + + dockerContainerExecutor.activateContainer(cId, pidFile); + int ret = dockerContainerExecutor.launchContainer(container, scriptPath, tokensPath, + appSubmitter, appId, workDir, dirsHandler.getLocalDirs(), + dirsHandler.getLogDirs()); + assertEquals(0, ret); + //get the script + Path wrapperScriptPath = new Path(workDir, + Shell.appendScriptExtension( + DockerContainerExecutor.DOCKER_CONTAINER_EXECUTOR_SCRIPT)); + LineNumberReader lnr = new LineNumberReader(new FileReader(wrapperScriptPath.toString())); + boolean cmdFound = false; + List localDirs = dirsToMount(dirsHandler.getLocalDirs()); + List logDirs = dirsToMount(dirsHandler.getLogDirs()); + List workDirMount = dirsToMount(Collections.singletonList(workDir.toUri().getPath())); + List expectedCommands = new ArrayList( + Arrays.asList(DOCKER_LAUNCH_COMMAND, "run", "--name", containerId)); + expectedCommands.addAll(localDirs); + expectedCommands.addAll(logDirs); + expectedCommands.addAll(workDirMount); + String shellScript = workDir + "/launch_container.sh"; + expectedCommands.addAll(Arrays.asList(DOCKER_LAUNCH_ARGS, yarnImage, + "bash","\"" + shellScript + "\"")); + while(lnr.ready()){ + String line = lnr.readLine(); + LOG.info("line: " + line); + if (line.startsWith(DOCKER_LAUNCH_COMMAND)){ + List command = new ArrayList(); + for( String s :line.split("\\s+")){ + command.add(s.trim()); + } + + assertEquals(expectedCommands, command); + cmdFound = true; + break; + } + + } + assertTrue(cmdFound); + } + + @Test + public void testContainerLaunch() throws IOException { + String appSubmitter = "nobody"; + String appId = "APP_ID"; + String containerId = "CONTAINER_ID"; + String testImage = "testrepo.com/test-image"; + + Container container = mock(Container.class, RETURNS_DEEP_STUBS); + ContainerId cId = mock(ContainerId.class, RETURNS_DEEP_STUBS); + ContainerLaunchContext context = mock(ContainerLaunchContext.class); + HashMap env = new HashMap(); + + when(container.getContainerId()).thenReturn(cId); + when(container.getLaunchContext()).thenReturn(context); + when(cId.getApplicationAttemptId().getApplicationId().toString()).thenReturn(appId); + when(cId.toString()).thenReturn(containerId); + + when(context.getEnvironment()).thenReturn(env); + env.put(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage); + Path scriptPath = new Path("file:///bin/echo"); + Path tokensPath = new Path("file:///dev/null"); + + Path pidFile = new Path(workDir, "pid.txt"); + + dockerContainerExecutor.activateContainer(cId, pidFile); + int ret = dockerContainerExecutor.launchContainer(container, scriptPath, tokensPath, + appSubmitter, appId, workDir, dirsHandler.getLocalDirs(), + dirsHandler.getLogDirs()); + assertEquals(0, ret); + //get the script + Path wrapperScriptPath = new Path(workDir, + Shell.appendScriptExtension( + DockerContainerExecutor.DOCKER_CONTAINER_EXECUTOR_SCRIPT)); + LineNumberReader lnr = new LineNumberReader(new FileReader(wrapperScriptPath.toString())); + boolean cmdFound = false; + List localDirs = dirsToMount(dirsHandler.getLocalDirs()); + List logDirs = dirsToMount(dirsHandler.getLogDirs()); + List workDirMount = dirsToMount(Collections.singletonList(workDir.toUri().getPath())); + List expectedCommands = new ArrayList( + Arrays.asList(DOCKER_LAUNCH_COMMAND, "run", "--name", containerId)); + expectedCommands.addAll(localDirs); + expectedCommands.addAll(logDirs); + expectedCommands.addAll(workDirMount); + String shellScript = workDir + "/launch_container.sh"; + expectedCommands.addAll(Arrays.asList(DOCKER_LAUNCH_ARGS, testImage, + "bash","\"" + shellScript + "\"")); + while(lnr.ready()){ + String line = lnr.readLine(); + LOG.debug("line: " + line); + if (line.startsWith(DOCKER_LAUNCH_COMMAND)){ + List command = new ArrayList(); + for( String s :line.split("\\s+")){ + command.add(s.trim()); + } + + assertEquals(expectedCommands, command); + cmdFound = true; + break; + } + + } + assertTrue(cmdFound); + } + + private List dirsToMount(List dirs) { + List localDirs = new ArrayList(); + for(String dir: dirs){ + localDirs.add("-v"); + localDirs.add(dir + ":" + dir); + } + return localDirs; + } + +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/apt/DockerContainerExecutor.vm b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/apt/DockerContainerExecutor.vm new file mode 100644 index 0000000000000..6e30aece7663d --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/apt/DockerContainerExecutor.vm @@ -0,0 +1,257 @@ + +~~ Licensed under the Apache License, Version 2.0 (the "License"); +~~ you may not use this file except in compliance with the License. +~~ You may obtain a copy of the License at +~~ +~~ http://www.apache.org/licenses/LICENSE-2.0 +~~ +~~ Unless required by applicable law or agreed to in writing, software +~~ distributed under the License is distributed on an "AS IS" BASIS, +~~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +~~ See the License for the specific language governing permissions and +~~ limitations under the License. See accompanying LICENSE file. + + --- + Hadoop Map Reduce Next Generation-${project.version} - Docker Container Executor + --- + --- + ${maven.build.timestamp} + +Docker Container Executor + +%{toc|section=1|fromDepth=0} + +* {Overview} + + Docker (https://www.docker.io/) combines an easy-to-use interface to +Linux containers with easy-to-construct image files for those +containers. In short, Docker launches very light weight "virtual +machines. + + The Docker Container Executor (DCE) allows the YARN NodeManager to +launch YARN containers into Docker containers. Users can specify the +Docker Images they want for their YARN containers. These containers +provide a custom software environment in which the user's code runs, +isolated from the software environment of the NodeManager. These +containers can include special libraries needed by the application, +and they can have different versions of Perl, Python, and even Java +than what is installed on the NodeManager. Indeed, these containers +can run a different flavor of Linux than what is running on the +NodeManager -- although the YARN container will share the kernel of +the NodeManager, and (for MapReduce and Tez) JAVA_HOME of the YARN +container must match that of the NodeManager. In cases where the JAVA_HOME +values do not match(for instance, if running an ubuntu container within +a centos NodeManager), user/Administrator must mount the $JAVA_HOME on the +container from the NodeManager, or pass the JAVA_HOME of the container as +part of the ApplicationMaster, mapper, and reducer settings(see example +below). + + Docker for YARN provides both consistency (all YARN containers will +have the same software environment) and isolation (no interference +with whatever is installed on the physical machine). + +* {Cluster Configuration} + + Docker Container Executor runs in non-secure mode of HDFS and +YARN. It will not run in secure mode, and will exit if it detects +secure mode. + + The DockerContainerExecutor requires Docker daemon to be running on +the NodeManager, and the Docker client installed and able to start Docker +containers. To prevent timeouts while starting jobs, the Docker +images to be used by a job should already be downloaded in the +NodeManagers. Here's an example of how this can be done: +---- +sudo docker pull centos +---- + +This should be done as part of the Nodemanager startup. + + The following properties must be set in yarn-site.xml: +---- + + yarn.nodemanager.docker-container-executor.image-name + busybox + + This image is used by all nodemanagers to launch containers. + This maybe modified by the users(see below) + + +---- + + yarn.nodemanager.docker-container-executor.run-args + --rm --net=host + + This arguments to pass to the 'docker run' invocation. + + +---- + + yarn.nodemanager.docker-container-executor.exec-name + /usr/bin/docker + + Name or path to the Docker client. This is a required parameter. If this is empty, + user must pass an image name as part of the job invocation(see below). + + +---- + + yarn.nodemanager.container-executor.class + org.apache.hadoop.yarn.server.nodemanager.DockerContainerExecutor + + This is the container executor setting that ensures that all +jobs are started with the DockerContainerExecutor. + + +---- + + + Administrators are expected to include both --rm and --net=host in +yarn.nodemanager.docker-run.args. --rm ensures that all containers +that are killed are cleaned up. --net=host makes the container share +the host's network, which allows all Hadoop processes (including the +Application Master) to talk to the containers as if they where just +processes on the same host as the container's NodeManager. + + Administrators should be aware that DCE doesn't currently provide +user name-space isolation. This means, in particular, that software +running as root in the YARN container will have root privileges in the +underlying NodeManager. Put differently, DCE currently provides no +better security guarantees than YARN's Default Container Executor. In +fact, DockerContainerExecutor will exit if it detects secure hadoop. + +* {Tips for connecting to a secure docker repository} + + By default, docker images are pulled from the docker public repository. The +format of a docker image url is: /. For example, +zhaowh/centos-java is an image in docker public repository that contains java on +centos. + +If you want your own private repository, you provide the repository url instead of +your username. Therefore, the image url becomes: /. +For example, if your repository is on localhost:8080, your images would be like: + localhost:8080/centos-java + +To connect to a secure docker repository, you can use the following invocation: +---- +docker login [OPTIONS] [SERVER] + +Register or log in to a Docker registry server, if no server is specified +"https://index.docker.io/v1/" is the default. + + -e, --email="" Email + -p, --password="" Password + -u, --username="" Username +---- + +If you want to login to a self-hosted registry you can specify this by adding +the server name. + +---- +docker login localhost:8080 +---- + +This needs to be run as part of the NodeManager startup, or as a cron job if +the login session expires periodically. You can login to multiple docker repositories +from the same NodeManager, but all your users will have access to all your repositories, +as at present the DockerContainerExecutor does not support per-job docker login. + +* {Job Configuration} + +Currently you cannot configure any of the Docker settings with the job configuration. +You can provide Mapper, Reducer, and ApplicationMaster environment overrides for the +docker images, using the following 3 JVM properties respectively(only for MR jobs): + * mapreduce.map.env: You can override the mapper's image by passing +yarn.nodemanager.docker-container-executor.image-name= +to this JVM property. + * mapreduce.reduce.env: You can override the reducer's image by passing +yarn.nodemanager.docker-container-executor.image-name= +to this JVM property. + * yarn.app.mapreduce.am.env: You can override the ApplicationMaster's image +by passing yarn.nodemanager.docker-container-executor.image-name= +to this JVM property. + +* {Docker Image requirements} + + The Docker Images used for YARN containers must meet the following +requirements: + + The distro and version of Linux in your Docker Image can be quite different +from that of your NodeManager. (Docker does have a few limitations in this +regard, but you're not likely to hit them.) However, if you're using the +MapReduce framework, then your image will need to be configured for running +Hadoop. Java must either be installed in the container, in the same location +as the NodeManager's java, or JAVA_HOME must be mounted. + + +* {Working example of yarn launched docker containers.} + +The following example shows how to run teragen using DockerContainerExecutor. + + * First ensure that YARN is properly configured with DockerContainerExecutor(see above). +If you use an image that does not have hadoop in it, you will need to mount the hadoop jars. +In the following example, hadoop is installed at /opt/hadoop-3.0.0. +See below: + + + yarn.nodemanager.docker-container-executor.run-args + + --rm --net=host + -v $JAVA_HOME:$JAVA_HOME + -v /opt/hadoop-3.0.0:/opt/hadoop + -v /opt/hadoop-3.0.0:/opt/hadoop-3.0.0 + + + These are arguments to pass to the 'docker run' invocation. + We are mounting hadoop and JAVA_HOME + + +---- + + yarn.nodemanager.docker-container-executor.exec-name + docker -H=tcp://0.0.0.0:4243 + + Name or path to the Docker client. The tcp socket must be + where docker daemon is listening. + + +---- + + yarn.nodemanager.container-executor.class + org.apache.hadoop.yarn.server.nodemanager.DockerContainerExecutor + + This is the container executor setting that ensures that all +jobs are started with the DockerContainerExecutor. + + +---- + + * Pick a custom Docker image if you want. Ensure that it has the java binary in the same +location as the NodeManager node. In this example, we'll use ubuntu and centos from the +docker hub repository. These docker images have no hadoop jars or java in it, therefore those +must be mounted. + * Run: +---- +hadoop jar $HADOOP_INSTALLATION_DIR/share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar \ +teragen \ +-Dmapreduce.map.env="yarn.nodemanager.docker-container-executor.image-name=ubuntu" \ +-Dyarn.app.mapreduce.am.env="yarn.nodemanager.docker-container-executor.image-name=centos" \ +1000 \ +teragen_out_dir +---- + +If you want to use your own jvm image, you need to pass the JAVA_HOME environment variable to the +ApplicationMaster, mapper, and reducer: + +---- +hadoop jar $HADOOP_INSTALLATION_DIR/share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar \ +teragen \ +-Dmapreduce.map.env="yarn.nodemanager.docker-container-executor.image-name=java:8,JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64" \ +-Dyarn.app.mapreduce.am.env="yarn.nodemanager.docker-container-executor.image-name=java:8,JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64" \ +1000 teragen_out_dir +---- + +Once it succeeds, you can check the yarn debug logs to verify that docker indeed has launched containers. +Alternatively, if you launch your container without the --rm option(yarn.nodemanager.docker-container-executor.run-arg) +you can see containers that ran by running 'docker ps -a' on the Nodemanager where the containers ran. +----