2020-02-22 18:39:33 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
package syssetup
|
|
|
|
|
|
|
|
import (
|
2019-05-09 19:23:33 +00:00
|
|
|
"os"
|
2019-01-26 04:52:29 +00:00
|
|
|
"os/exec"
|
2021-05-17 20:30:55 +00:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2021-05-17 20:30:55 +00:00
|
|
|
"github.com/google/cadvisor/machine"
|
|
|
|
"github.com/google/cadvisor/utils/sysfs"
|
2019-01-01 08:23:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-05-17 20:30:55 +00:00
|
|
|
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
|
|
|
|
"k8s.io/kubernetes/pkg/util/sysctl"
|
2019-01-01 08:23:01 +00:00
|
|
|
)
|
|
|
|
|
2019-05-09 19:23:33 +00:00
|
|
|
func loadKernelModule(moduleName string) {
|
|
|
|
if _, err := os.Stat("/sys/module/" + moduleName); err == nil {
|
2020-09-21 16:56:03 +00:00
|
|
|
logrus.Info("Module " + moduleName + " was already loaded")
|
2019-05-09 19:23:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-17 20:30:55 +00:00
|
|
|
if err := exec.Command("modprobe", "--", moduleName).Run(); err != nil {
|
|
|
|
logrus.Warnf("Failed to load kernel module %v with modprobe", moduleName)
|
2019-04-18 01:27:20 +00:00
|
|
|
}
|
2019-05-09 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 20:30:55 +00:00
|
|
|
// Configure loads required kernel modules and sets sysctls required for other components to
|
|
|
|
// function properly.
|
|
|
|
func Configure(enableIPv6 bool, config *kubeproxyconfig.KubeProxyConntrackConfiguration) {
|
2019-05-09 19:23:33 +00:00
|
|
|
loadKernelModule("overlay")
|
|
|
|
loadKernelModule("nf_conntrack")
|
2019-11-04 18:35:14 +00:00
|
|
|
loadKernelModule("br_netfilter")
|
2020-10-26 19:02:47 +00:00
|
|
|
loadKernelModule("iptable_nat")
|
2021-04-21 22:56:20 +00:00
|
|
|
if enableIPv6 {
|
|
|
|
loadKernelModule("ip6table_nat")
|
|
|
|
}
|
2019-05-09 19:23:33 +00:00
|
|
|
|
2019-12-11 00:13:19 +00:00
|
|
|
// Kernel is inconsistent about how devconf is configured for
|
|
|
|
// new network namespaces between ipv4 and ipv6. Make sure to
|
2021-04-21 22:56:20 +00:00
|
|
|
// enable forwarding on all and default for both ipv4 and ipv6.
|
2021-05-17 20:30:55 +00:00
|
|
|
sysctls := map[string]int{
|
|
|
|
"net/ipv4/conf/all/forwarding": 1,
|
|
|
|
"net/ipv4/conf/default/forwarding": 1,
|
|
|
|
"net/bridge/bridge-nf-call-iptables": 1,
|
|
|
|
}
|
|
|
|
|
2021-04-21 22:56:20 +00:00
|
|
|
if enableIPv6 {
|
2021-05-17 20:30:55 +00:00
|
|
|
sysctls["net/ipv6/conf/all/forwarding"] = 1
|
|
|
|
sysctls["net/ipv6/conf/default/forwarding"] = 1
|
|
|
|
sysctls["net/bridge/bridge-nf-call-ip6tables"] = 1
|
2021-09-28 07:42:08 +00:00
|
|
|
sysctls["net/core/devconf_inherit_init_net"] = 1
|
2021-05-17 20:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if conntrackMax := getConntrackMax(config); conntrackMax > 0 {
|
|
|
|
sysctls["net/netfilter/nf_conntrack_max"] = conntrackMax
|
|
|
|
}
|
|
|
|
if config.TCPEstablishedTimeout.Duration > 0 {
|
|
|
|
sysctls["net/netfilter/nf_conntrack_tcp_timeout_established"] = int(config.TCPEstablishedTimeout.Duration / time.Second)
|
|
|
|
}
|
|
|
|
if config.TCPCloseWaitTimeout.Duration > 0 {
|
|
|
|
sysctls["net/netfilter/nf_conntrack_tcp_timeout_close_wait"] = int(config.TCPCloseWaitTimeout.Duration / time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
sys := sysctl.New()
|
|
|
|
for entry, value := range sysctls {
|
|
|
|
if val, _ := sys.GetSysctl(entry); val != value {
|
|
|
|
logrus.Infof("Set sysctl '%v' to %v", entry, value)
|
|
|
|
if err := sys.SetSysctl(entry, value); err != nil {
|
|
|
|
logrus.Errorf("Failed to set sysctl: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConntrackMax is cribbed from kube-proxy, as recent kernels no longer allow non-init namespaces
|
|
|
|
// to set conntrack-related sysctls.
|
|
|
|
// ref: https://github.com/kubernetes/kubernetes/blob/v1.21.1/cmd/kube-proxy/app/server.go#L780
|
|
|
|
// ref: https://github.com/kubernetes-sigs/kind/issues/2240
|
|
|
|
func getConntrackMax(config *kubeproxyconfig.KubeProxyConntrackConfiguration) int {
|
|
|
|
if config.MaxPerCore != nil && *config.MaxPerCore > 0 {
|
|
|
|
floor := 0
|
|
|
|
if config.Min != nil {
|
|
|
|
floor = int(*config.Min)
|
|
|
|
}
|
|
|
|
scaled := int(*config.MaxPerCore) * detectNumCPU()
|
|
|
|
if scaled > floor {
|
|
|
|
logrus.Debugf("getConntrackMax: using scaled conntrack-max-per-core")
|
|
|
|
return scaled
|
|
|
|
}
|
|
|
|
logrus.Debugf("getConntrackMax: using conntrack-min")
|
|
|
|
return floor
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// detectNumCPU is also cribbed from kube-proxy
|
|
|
|
func detectNumCPU() int {
|
|
|
|
// try get numCPU from /sys firstly due to a known issue (https://github.com/kubernetes/kubernetes/issues/99225)
|
|
|
|
_, numCPU, err := machine.GetTopology(sysfs.NewRealSysFs())
|
|
|
|
if err != nil || numCPU < 1 {
|
|
|
|
return runtime.NumCPU()
|
2021-04-21 22:56:20 +00:00
|
|
|
}
|
2021-05-17 20:30:55 +00:00
|
|
|
return numCPU
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|