Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mount task_local and alloc dir to docker containers #290

Merged
merged 5 commits into from
Oct 16, 2015
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion client/alloc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ func (r *AllocRunner) Run() {
// Create the execution context
if r.ctx == nil {
allocDir := allocdir.NewAllocDir(filepath.Join(r.config.AllocDir, r.alloc.ID))
allocDir.Build(tg.Tasks)
if err := allocDir.Build(tg.Tasks); err != nil {
r.logger.Printf("[WARN] client: failed to build task directories: %v", err)
r.setStatus(structs.AllocClientStatusFailed, fmt.Sprintf("failed to build task dirs for '%s'", alloc.TaskGroup))
return
}
r.ctx = driver.NewExecContext(allocDir)
}

Expand Down
71 changes: 54 additions & 17 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"encoding/json"
"fmt"
"log"
"path/filepath"
"strconv"
"strings"

docker "github.com/fsouza/go-dockerclient"

"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/args"
"github.com/hashicorp/nomad/nomad/structs"
)

Expand Down Expand Up @@ -97,12 +100,33 @@ func (d *DockerDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool
return true, nil
}

// We have to call this when we create the container AND when we start it so
// we'll make a function.
func createHostConfig(task *structs.Task) *docker.HostConfig {
// hostConfig holds options for the docker container that are unique to this
// machine, such as resource limits and port mappings
return &docker.HostConfig{
func containerBinds(alloc *allocdir.AllocDir, task *structs.Task) ([]string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this isn't a method of the Docker Driver? It seems unlikely to be shared

shared := alloc.SharedDir
local, ok := alloc.TaskDirs[task.Name]
if !ok {
return nil, fmt.Errorf("Failed to find task local directory: %v", task.Name)
}

return []string{
fmt.Sprintf("%s:%s", shared, allocdir.SharedAllocName),
fmt.Sprintf("%s:%s", local, allocdir.TaskLocal),
}, nil
}

// createContainer initializes a struct needed to call docker.client.CreateContainer()
func createContainer(ctx *ExecContext, task *structs.Task, logger *log.Logger) (docker.CreateContainerOptions, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, would this ever be shared?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can update these. Just an artifact of how it was written.

var c docker.CreateContainerOptions
if task.Resources == nil {
logger.Printf("[ERR] driver.docker: task.Resources is empty")
return c, fmt.Errorf("task.Resources is nil and we can't constrain resource usage. We shouldn't have been able to schedule this in the first place.")
}

binds, err := containerBinds(ctx.AllocDir, task)
if err != nil {
return c, err
}

hostConfig := &docker.HostConfig{
// Convert MB to bytes. This is an absolute value.
//
// This value represents the total amount of memory a process can use.
Expand Down Expand Up @@ -131,20 +155,16 @@ func createHostConfig(task *structs.Task) *docker.HostConfig {
// - https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt
// - https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt
CPUShares: int64(task.Resources.CPU),
}
}

// createContainer initializes a struct needed to call docker.client.CreateContainer()
func createContainer(ctx *ExecContext, task *structs.Task, logger *log.Logger) (docker.CreateContainerOptions, error) {
var c docker.CreateContainerOptions
if task.Resources == nil {
logger.Printf("[ERR] driver.docker: task.Resources is empty")
return c, fmt.Errorf("task.Resources is nil and we can't constrain resource usage. We shouldn't have been able to schedule this in the first place.")
// Binds are used to mount a host volume into the container. We mount a
// local directory for storage and a shared alloc directory that can be
// used to share data between different tasks in the same task group.
Binds: binds,
}

hostConfig := createHostConfig(task)
logger.Printf("[DEBUG] driver.docker: using %d bytes memory for %s", hostConfig.Memory, task.Config["image"])
logger.Printf("[DEBUG] driver.docker: using %d cpu shares for %s", hostConfig.CPUShares, task.Config["image"])
logger.Printf("[DEBUG] driver.docker: binding directories %#v for %s", hostConfig.Binds, task.Config["image"])

mode, ok := task.Config["network_mode"]
if !ok || mode == "" {
Expand Down Expand Up @@ -198,14 +218,31 @@ func createContainer(ctx *ExecContext, task *structs.Task, logger *log.Logger) (
hostConfig.PortBindings = dockerPorts
}

// Create environment variables.
env := TaskEnvironmentVariables(ctx, task)
env.SetAllocDir(filepath.Join("/", allocdir.SharedAllocName))
env.SetTaskLocalDir(filepath.Join("/", allocdir.TaskLocal))

config := &docker.Config{
Env: TaskEnvironmentVariables(ctx, task).List(),
Env: env.List(),
Image: task.Config["image"],
}

rawArgs, hasArgs := task.Config["args"]
parsedArgs, err := args.ParseAndReplace(rawArgs, env.Map())
if err != nil {
return c, err
}

// If the user specified a custom command to run, we'll inject it here.
if command, ok := task.Config["command"]; ok {
config.Cmd = strings.Split(command, " ")
cmd := []string{command}
if hasArgs {
cmd = append(cmd, parsedArgs...)
}
config.Cmd = cmd
} else if hasArgs {
logger.Println("[DEBUG] driver.docker: ignoring args because command not specified")
}

return docker.CreateContainerOptions{
Expand Down
78 changes: 76 additions & 2 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package driver

import (
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"reflect"
"testing"
"time"

"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/environment"
"github.com/hashicorp/nomad/nomad/structs"
)

Expand Down Expand Up @@ -102,7 +107,8 @@ func TestDockerDriver_Start_Wait(t *testing.T) {
Name: "redis-demo",
Config: map[string]string{
"image": "redis",
"command": "redis-server -v",
"command": "redis-server",
"args": "-v",
},
Resources: &structs.Resources{
MemoryMB: 256,
Expand Down Expand Up @@ -140,6 +146,61 @@ func TestDockerDriver_Start_Wait(t *testing.T) {
}
}

func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
if !dockerLocated() {
t.SkipNow()
}

exp := []byte{'w', 'i', 'n'}
file := "output.txt"
task := &structs.Task{
Name: "redis-demo",
Config: map[string]string{
"image": "redis",
"command": "/bin/bash",
"args": fmt.Sprintf(`-c "sleep 1; echo -n %s > $%s/%s"`, string(exp), environment.AllocDir, file),
},
Resources: &structs.Resources{
MemoryMB: 256,
CPU: 512,
},
}

driverCtx := testDockerDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
d := NewDockerDriver(driverCtx)

handle, err := d.Start(ctx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()

select {
case err := <-handle.WaitCh():
if err != nil {
t.Fatalf("err: %v", err)
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout")
}

// Check that data was written to the shared alloc directory.
outputFile := filepath.Join(ctx.AllocDir.SharedDir, file)
act, err := ioutil.ReadFile(outputFile)
if err != nil {
t.Fatalf("Couldn't read expected output: %v", err)
}

if !reflect.DeepEqual(act, exp) {
t.Fatalf("Command outputted %v; want %v", act, exp)
}
}

func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
if !dockerLocated() {
t.SkipNow()
Expand All @@ -149,7 +210,8 @@ func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
Name: "redis-demo",
Config: map[string]string{
"image": "redis",
"command": "sleep 10",
"command": "/bin/sleep",
"args": "10",
},
Resources: basicResources,
}
Expand Down Expand Up @@ -188,6 +250,7 @@ func TestDockerDriver_Start_Kill_Wait(t *testing.T) {

func taskTemplate() *structs.Task {
return &structs.Task{
Name: "redis-demo",
Config: map[string]string{
"image": "redis",
},
Expand Down Expand Up @@ -242,6 +305,11 @@ func TestDocker_StartN(t *testing.T) {
t.Log("==> All tasks are started. Terminating...")

for idx, handle := range handles {
if handle == nil {
t.Errorf("Bad handle for task #%d", idx+1)
continue
}

err := handle.Kill()
if err != nil {
t.Errorf("Failed stopping task #%d: %s", idx+1, err)
Expand Down Expand Up @@ -291,6 +359,11 @@ func TestDocker_StartNVersions(t *testing.T) {
t.Log("==> All tasks are started. Terminating...")

for idx, handle := range handles {
if handle == nil {
t.Errorf("Bad handle for task #%d", idx+1)
continue
}

err := handle.Kill()
if err != nil {
t.Errorf("Failed stopping task #%d: %s", idx+1, err)
Expand All @@ -306,6 +379,7 @@ func TestDockerHostNet(t *testing.T) {
}

task := &structs.Task{
Name: "redis-demo",
Config: map[string]string{
"image": "redis",
"network_mode": "host",
Expand Down
8 changes: 8 additions & 0 deletions client/driver/environment/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const (
// group.
AllocDir = "NOMAD_ALLOC_DIR"

// The path to the tasks local directory where it can store data that is
// persisted to the alloc is removed.
TaskLocalDir = "NOMAD_TASK_DIR"

// The tasks memory limit in MBs.
MemLimit = "NOMAD_MEMORY_LIMIT"

Expand Down Expand Up @@ -70,6 +74,10 @@ func (t TaskEnvironment) SetAllocDir(dir string) {
t[AllocDir] = dir
}

func (t TaskEnvironment) SetTaskLocalDir(dir string) {
t[TaskLocalDir] = dir
}

func (t TaskEnvironment) SetMemLimit(limit int) {
t[MemLimit] = strconv.Itoa(limit)
}
Expand Down
4 changes: 2 additions & 2 deletions client/driver/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestExecDriver_Start_Wait(t *testing.T) {
Name: "sleep",
Config: map[string]string{
"command": "/bin/sleep",
"args": "1",
"args": "2",
},
Resources: basicResources,
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestExecDriver_Start_Wait(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
case <-time.After(2 * time.Second):
case <-time.After(4 * time.Second):
t.Fatalf("timeout")
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"os/exec"
"runtime"
"strings"
"strings"

"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
Expand Down
1 change: 1 addition & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ gox \
-arch="${XC_ARCH}" \
-osarch="!linux/arm !darwin/386" \
-ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \
-cgo \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we specifically were avoiding cgo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't be. user.Lookup which is part of the stdlib uses cgo.

-output "pkg/{{.OS}}_{{.Arch}}/nomad" \
.

Expand Down
3 changes: 3 additions & 0 deletions website/source/docs/drivers/docker.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ The `docker` driver supports the following configuration in the job specificatio

* `command` - (Optional) The command to run when starting the container.

* `args` - (Optional) Arguments to the optional `command`. If no `command` is
present, `args` are ignored.

* `network_mode` - (Optional) The network mode to be used for the container.
Valid options are `default`, `bridge`, `host` or `none`. If nothing is
specified, the container will start in `bridge` mode. The `container`
Expand Down
21 changes: 21 additions & 0 deletions website/source/docs/jobspec/environment.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ exported as environment variables for consistency, e.g. `NOMAD_PORT_5000`.

Please see the relevant driver documentation for details.

### Task Directories

Nomad makes the following two directories available to tasks:

* `alloc/`: This directory is shared across all tasks in a task group and can be
used to store data that needs to be used by multiple tasks, such as a log
shipper.
* `local/`: This directory is private to each task. It can be used to store
arbitrary data that shouldn't be shared by tasks in the task group.

Both these directories are persisted until the allocation is removed, which
occurs hours after all the tasks in the task group enter terminal states. This
gives time to view the data produced by tasks.

Depending on the driver and operating system being targeted, the directories are
made available in various ways. For example, on `docker` the directories are
binded to the container, while on `exec` on Linux the directories are mounted into the
chroot. Regardless of how the directories are made available, the path to the
directories can be read through the following environment variables:
`NOMAD_ALLOC_DIR` and `NOMAD_TASK_DIR`.

## Meta

The job specification also allows you to specify a `meta` block to supply arbitrary
Expand Down