2e5c6de820
This PR introduces support for using Nomad on systems with cgroups v2 [1] enabled as the cgroups controller mounted on /sys/fs/cgroups. Newer Linux distros like Ubuntu 21.10 are shipping with cgroups v2 only, causing problems for Nomad users. Nomad mostly "just works" with cgroups v2 due to the indirection via libcontainer, but not so for managing cpuset cgroups. Before, Nomad has been making use of a feature in v1 where a PID could be a member of more than one cgroup. In v2 this is no longer possible, and so the logic around computing cpuset values must be modified. When Nomad detects v2, it manages cpuset values in-process, rather than making use of cgroup heirarchy inheritence via shared/reserved parents. Nomad will only activate the v2 logic when it detects cgroups2 is mounted at /sys/fs/cgroups. This means on systems running in hybrid mode with cgroups2 mounted at /sys/fs/cgroups/unified (as is typical) Nomad will continue to use the v1 logic, and should operate as before. Systems that do not support cgroups v2 are also not affected. When v2 is activated, Nomad will create a parent called nomad.slice (unless otherwise configured in Client conifg), and create cgroups for tasks using naming convention <allocID>-<task>.scope. These follow the naming convention set by systemd and also used by Docker when cgroups v2 is detected. Client nodes now export a new fingerprint attribute, unique.cgroups.version which will be set to 'v1' or 'v2' to indicate the cgroups regime in use by Nomad. The new cpuset management strategy fixes #11705, where docker tasks that spawned processes on startup would "leak". In cgroups v2, the PIDs are started in the cgroup they will always live in, and thus the cause of the leak is eliminated. [1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html Closes #11289 Fixes #11705 #11773 #11933
89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package fingerprint
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
"github.com/hashicorp/nomad/client/lib/cgutil"
|
|
)
|
|
|
|
const (
|
|
cgroupAvailable = "available"
|
|
cgroupUnavailable = "unavailable"
|
|
|
|
cgroupMountPointAttribute = "unique.cgroup.mountpoint"
|
|
cgroupVersionAttribute = "unique.cgroup.version"
|
|
|
|
cgroupDetectInterval = 15 * time.Second
|
|
)
|
|
|
|
type CGroupFingerprint struct {
|
|
logger hclog.Logger
|
|
lastState string
|
|
mountPointDetector MountPointDetector
|
|
versionDetector CgroupVersionDetector
|
|
}
|
|
|
|
// MountPointDetector isolates calls to the cgroup library.
|
|
//
|
|
// This facilitates testing where we can implement fake mount points to test
|
|
// various code paths.
|
|
type MountPointDetector interface {
|
|
// MountPoint returns a cgroup mount-point.
|
|
//
|
|
// In v1, this is one arbitrary subsystem (e.g. /sys/fs/cgroup/cpu).
|
|
//
|
|
// In v2, this is the actual root mount point (i.e. /sys/fs/cgroup).
|
|
MountPoint() (string, error)
|
|
}
|
|
|
|
// DefaultMountPointDetector implements the interface detector which calls the cgroups
|
|
// library directly
|
|
type DefaultMountPointDetector struct {
|
|
}
|
|
|
|
// MountPoint calls out to the default cgroup library.
|
|
func (*DefaultMountPointDetector) MountPoint() (string, error) {
|
|
return cgutil.FindCgroupMountpointDir()
|
|
}
|
|
|
|
// CgroupVersionDetector isolates calls to the cgroup library.
|
|
type CgroupVersionDetector interface {
|
|
// CgroupVersion returns v1 or v2 depending on the cgroups version in use.
|
|
CgroupVersion() string
|
|
}
|
|
|
|
// DefaultCgroupVersionDetector implements the version detector which calls the
|
|
// cgroups library directly.
|
|
type DefaultCgroupVersionDetector struct {
|
|
}
|
|
|
|
func (*DefaultCgroupVersionDetector) CgroupVersion() string {
|
|
if cgutil.UseV2 {
|
|
return "v2"
|
|
}
|
|
return "v1"
|
|
}
|
|
|
|
// NewCGroupFingerprint returns a new cgroup fingerprinter
|
|
func NewCGroupFingerprint(logger hclog.Logger) Fingerprint {
|
|
return &CGroupFingerprint{
|
|
logger: logger.Named("cgroup"),
|
|
lastState: cgroupUnavailable,
|
|
mountPointDetector: new(DefaultMountPointDetector),
|
|
versionDetector: new(DefaultCgroupVersionDetector),
|
|
}
|
|
}
|
|
|
|
// clearCGroupAttributes clears any node attributes related to cgroups that might
|
|
// have been set in a previous fingerprint run.
|
|
func (f *CGroupFingerprint) clearCGroupAttributes(r *FingerprintResponse) {
|
|
r.RemoveAttribute(cgroupMountPointAttribute)
|
|
r.RemoveAttribute(cgroupVersionAttribute)
|
|
}
|
|
|
|
// Periodic determines the interval at which the periodic fingerprinter will run.
|
|
func (f *CGroupFingerprint) Periodic() (bool, time.Duration) {
|
|
return true, cgroupDetectInterval
|
|
}
|