feat: manage node-level kernel sysctls on the k3s scheduler
Sysctls the kernel does not namespace, such as `vm.max_map_count`, cannot be set from a pod spec and previously had no answer beyond editing `/etc/sysctl.d` on each host by hand. `scheduler-k3s:node-sysctls:set` now applies them through a privileged daemonset, which reaches nodes joined later and reapplies after a reboot. Sysctls may be scoped to a node profile, with a profile scope inheriting the global values and overriding them on conflict so that every node is covered by exactly one daemonset. Clearing a sysctl stops dokku managing it but does not restore the previous value, which persists until the node reboots.
This commit is contained in:
12 files changed
+964
-20
No files matched your search
@@ -45,6 +45,95 @@ func CommandAnnotationsSet(appName string, processType string, resourceType stri
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandNodeSysctlsSet sets or clears a node-level kernel sysctl for a scope
|
||||
func CommandNodeSysctlsSet(profileName string, key string, value string) error {
|
||||
if key == "" {
|
||||
return fmt.Errorf("Missing sysctl name")
|
||||
}
|
||||
|
||||
if profileName != "" {
|
||||
if err := verifyNodeProfileExists(profileName); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
property := getNodeSysctlsProperty(profileName)
|
||||
if value == "" {
|
||||
if err := common.PropertyMapDelete("scheduler-k3s", "--global", property, key); err != nil {
|
||||
return fmt.Errorf("Unable to delete property map entry: %w", err)
|
||||
}
|
||||
|
||||
common.LogWarn(fmt.Sprintf("Removing %s stops dokku managing it, but does not restore the previous value on affected nodes until they reboot", key))
|
||||
} else {
|
||||
if err := common.PropertyMapSet("scheduler-k3s", "--global", property, key, value); err != nil {
|
||||
return fmt.Errorf("Unable to set property map entry: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return CreateOrUpdateNodeSysctls(context.Background())
|
||||
}
|
||||
|
||||
// CommandNodeSysctlsReport displays the configured node-level kernel sysctls
|
||||
func CommandNodeSysctlsReport(format string) error {
|
||||
if format != "stdout" && format != "json" {
|
||||
return fmt.Errorf("Invalid format: %s", format)
|
||||
}
|
||||
|
||||
scopes, err := resolveNodeSysctlScopes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if format == "json" {
|
||||
output := map[string]map[string]string{}
|
||||
for _, scope := range scopes {
|
||||
key := scope.ProfileName
|
||||
if key == "" {
|
||||
key = "--global"
|
||||
}
|
||||
|
||||
entries := map[string]string{}
|
||||
for _, sysctl := range scope.Sysctls {
|
||||
entries[sysctl.Name] = sysctl.Value
|
||||
}
|
||||
output[key] = entries
|
||||
}
|
||||
|
||||
b, err := json.Marshal(output)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to marshal json: %w", err)
|
||||
}
|
||||
|
||||
fmt.Println(string(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
lines := []string{"scope|sysctl|value"}
|
||||
for _, scope := range scopes {
|
||||
scopeName := scope.ProfileName
|
||||
if scopeName == "" {
|
||||
scopeName = "--global"
|
||||
}
|
||||
|
||||
for _, sysctl := range scope.Sysctls {
|
||||
lines = append(lines, fmt.Sprintf("%s|%s|%s", scopeName, sysctl.Name, sysctl.Value))
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(columnize.SimpleFormat(lines))
|
||||
return nil
|
||||
}
|
||||
|
||||
// verifyNodeProfileExists returns an error when a node profile has not been created
|
||||
func verifyNodeProfileExists(profileName string) error {
|
||||
properties := common.PropertyGetDefault("scheduler-k3s", "--global", fmt.Sprintf("node-profile-%s.json", profileName), "")
|
||||
if properties == "" {
|
||||
return fmt.Errorf("Node profile %s not found", profileName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandAutoscalingAuthSet set or clear a scheduler-k3s autoscaling keda trigger authentication object for an app
|
||||
func CommandAutoscalingAuthSet(appName string, trigger string, metadata map[string]string, global bool) error {
|
||||
if global {
|
||||
@@ -397,6 +486,11 @@ func CommandInitialize(ingressClass string, serverIP string, taintScheduling boo
|
||||
return fmt.Errorf("Unable to install helper commands: %w", err)
|
||||
}
|
||||
|
||||
common.LogInfo2Quiet("Applying node sysctls")
|
||||
if err := CreateOrUpdateNodeSysctls(ctx); err != nil {
|
||||
return fmt.Errorf("Unable to apply node sysctls: %w", err)
|
||||
}
|
||||
|
||||
common.LogVerboseQuiet("Done")
|
||||
|
||||
return nil
|
||||
@@ -1262,6 +1356,10 @@ func CommandProfilesRemove(profileName string) error {
|
||||
return fmt.Errorf("Unable to delete node profile: %w", err)
|
||||
}
|
||||
|
||||
if err := DeleteNodeSysctls(context.Background(), profileName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
common.LogInfo1(fmt.Sprintf("Node profile %s removed", profileName))
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user