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

drivers/raw_exec: enable setting cgroup override values #20481

Merged
merged 8 commits into from
May 7, 2024

Conversation

shoenig
Copy link
Contributor

@shoenig shoenig commented Apr 23, 2024

This PR enables configuration of cgroup override values on the raw_exec
task driver. WARNING: setting cgroup override values eliminates any
guarantee Nomad can make about resource availability for any task on
the client node.

For cgroup v2 systems, set a single unified cgroup path using cgroup_v2_override.
The path may be either absolute or relative to the cgroup root.

config {
  cgroup_v2_override = "custom.slice/app.scope"
}

or

config {
  cgroup_v2_override = "/sys/fs/cgroup/custom.slice/app.scope"
}

For cgroup v1 systems, set a per-controller path for each controller using
cgroup_v1_override. The path(s) may be either absolute or relative to
the controller root.

config {
  cgroup_v1_override = {
    "pids": "custom/app",
    "cpuset": "custom/app",
  }
}

or

config {
  cgroup_v1_override = {
    "pids": "/sys/fs/cgroup/pids/custom/app",
    "cpuset": "/sys/fs/cgroup/cpuset/custom/app",
  }
}

@shoenig
Copy link
Contributor Author

shoenig commented Apr 24, 2024

Example (cgroups v1):

job "sleep" {
  group "group" {
    task "task" {
      driver = "raw_exec"
      config {
        command = "sleep"
        args    = ["infinity"]
        cgroup_v1_override = {
          "pids" : "custom/app",
          "cpuset" : "custom/app",
        }
      }
    }
  }
}

Ensure our custom cgroup under the pids controller exists.

➜ sudo mkdir -p /sys/fs/cgroup/pids/custom/app

Ensure our custom cgroup under the cpuset controller exists and has cpus and mems available.

➜ sudo mkdir -p /sys/fs/cgroup/cpuset/custom/app
➜ echo "0-3" | sudo tee /sys/fs/cgroup/cpuset/custom/cpuset.cpus
➜ echo "0-3" | sudo tee /sys/fs/cgroup/cpuset/custom/app/cpuset.cpus
➜ echo "0" | sudo tee /sys/fs/cgroup/cpuset/custom/cpuset.mems
➜ echo "0" | sudo tee /sys/fs/cgroup/cpuset/custom/app/cpuset.mems

Run job.

Our custom cgroups are in use.

➜ cat /sys/fs/cgroup/pids/custom/app/cgroup.procs
135047
135063
➜ cat /sys/fs/cgroup/cpuset/custom/app/cgroup.procs
135047
135063

@shoenig shoenig force-pushed the cgroup-override-raw-exec branch from f69b8d0 to 546c7cb Compare April 24, 2024 16:27
@shoenig shoenig changed the title wip: cgroup override on raw_exec drivers/raw_exec: enable setting cgroup override values Apr 24, 2024
@shoenig
Copy link
Contributor Author

shoenig commented Apr 24, 2024

Example (cgroups v2)

job "sleep" {
  group "group" {
    task "task" {
      driver = "raw_exec"
      config {
        command            = "sleep"
        args               = ["infinity"]
        cgroup_v2_override = "custom.slice/app.scope"
      }
    }
  }
}

Ensure our custom unified cgroup exists.

➜ sudo mkdir -p /sys/fs/cgroup/custom.slice/app.scope

Run job.

Ensure our custom cgroup is in use.

➜ pcat custom.slice/app.scope/cgroup.procs
124226

This PR enables configuration of cgroup override values on the `raw_exec`
task driver. WARNING: setting cgroup override values eliminates any
gauruntee Nomad can make about resource availability for *any* task on
the client node.

For cgroup v2 systems, set a single unified cgroup path using `cgroup_v2_override`.
The path may be either absolute or relative to the cgroup root.

config {
  cgroup_v2_override = "custom.slice/app.scope"
}

or

config {
  cgroup_v2_override = "/sys/fs/cgroup/custom.slice/app.scope"
}

For cgroup v1 systems, set a per-controller path for each controller using
`cgroup_v1_override`. The path(s) may be either absolute or relative to
the controller root.

config {
  cgroup_v1_override = {
    "pids": "custom/app",
    "cpuset": "custom/app",
  }
}

or

config {
  cgroup_v1_override = {
    "pids": "/sys/fs/cgroup/pids/custom/app",
    "cpuset": "/sys/fs/cgroup/cpuset/custom/app",
  }
}
Copy link
Member

@schmichael schmichael left a comment

Choose a reason for hiding this comment

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

Just to confirm: this doesn't appear to create cgroups if they don't exist or remove cgroups on text exit, correct?

for controller, path := range e.command.OverrideCgroupV1 {
absPath := cgroupslib.CustomPathCG1(controller, path)
ed := cgroupslib.OpenPath(absPath)
_ = ed.Write("cgroup.procs", strconv.Itoa(pid))
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to check the return value here or risk a task running without being added to its intended cgroup?

@shoenig
Copy link
Contributor Author

shoenig commented Apr 29, 2024

Making sure task does not start if cgroup override does not exist

Cgroups v1

2024-04-29T14:07:26Z  Driver Failure   failed to launch command with executor: rpc error: code = Unknown desc = unable to configure cgroups: unable to write to custom cgroup: open /sys/fs/cgroup/pids/invalid/app/cgroup.procs: no such file or directory
2024-04-29T14:07:26Z  Task Setup       Building Task Directory
2024-04-29T14:07:26Z  Received         Task received by client

Cgroups v2

2024-04-29T14:09:17Z  Driver Failure  failed to launch command with executor: rpc error: code = Unknown desc = unable to configure cgroups: no such file or directory
2024-04-29T14:09:17Z  Task Setup      Building Task Directory
2024-04-29T14:09:17Z  Received        Task received by client

@shoenig
Copy link
Contributor Author

shoenig commented Apr 29, 2024

Making sure config allows for only one of v1 or v2 override being set

config {
  command         = "sleep"
  args            = ["infinity"]
  cgroup_v1_override = {
    "pids": "custom/app",
    "cpuset": "custom/app",
  }
  cgroup_v2_override = "custom.slice/app.scope"
}
2024-04-29T14:19:04Z  Driver Failure   only one of cgroups_v1_override and cgroups_v2_override may be set
2024-04-29T14:19:04Z  Task Setup       Building Task Directory
2024-04-29T14:19:04Z  Received         Task received by client

@@ -222,10 +239,11 @@ func (e *UniversalExecutor) configureCG1(cgroup string, command *ExecCommand) {
ed = cgroupslib.OpenPath(cpusetPath)
_ = ed.Write("cpuset.cpus", cpuSet)
Copy link
Member

Choose a reason for hiding this comment

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

Is it safe to ignore this error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Historically we had to ignore these errors because people keep running Nomad as non-root or on systems where only some of the cgroup controllers are enabled. But they still want raw_exec to Just Work for them.

@schmichael schmichael merged commit 14a022c into main May 7, 2024
21 checks passed
@schmichael schmichael deleted the cgroup-override-raw-exec branch May 7, 2024 23:46
@schmichael schmichael added the backport/1.7.x backport to 1.7.x release line label May 7, 2024
@schmichael schmichael removed the backport/1.7.x backport to 1.7.x release line label May 8, 2024
Copy link

github-actions bot commented Jan 9, 2025

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants