Skip to content

Commit 585a6b5

Browse files
committed
Default cgroup driver to systemd from k8s 1.20
Currently, kOps uses cgroupfs cgroup driver for the kubelet and CRIs. This PR defaults the cgroup driver to systemd for clusters created with k8s versions >= 1.20. Using systemd as the cgroup-driver is the recommended way as per https://kubernetes.io/docs/setup/production-environment/container-runtimes/
1 parent 53f0ffd commit 585a6b5

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

docs/cluster_spec.md

+33
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ spec:
932932
### Configuration
933933

934934
It is possible to override the [containerd](https://github.com/containerd/containerd/blob/master/README.md) daemon options for all the nodes in the cluster. See the [API docs](https://pkg.go.dev/k8s.io/kops/pkg/apis/kops#ContainerdConfig) for the full list of options.
935+
Overriding the configuration of containerd has to be done with care as the default config may change with releases which can lead to incompatibilities.
935936

936937
```yaml
937938
spec:
@@ -1178,3 +1179,35 @@ spec:
11781179
```
11791180

11801181
which would end up in a drop-in file on all masters and nodes of the cluster.
1182+
1183+
## cgroupDriver
1184+
1185+
From k8s 1.20, kOps will default the cgroup driver of the kubelet and all the CRIs to use systemd as the default cgroup driver
1186+
as opposed to cgroup fs.
1187+
1188+
It is important to ensure that the kubelet and the CRI being used are using the same cgroup driver. Below are examples showing
1189+
how to set the cgroup driver for kubelet and the CRIs currently supported by kOps (docker and containerd).
1190+
1191+
Warning: Overriding the configuration of containerd has to be done with care as the default config may change with releases which can lead to incompatibilities.
1192+
1193+
Setting kubelet to use cgroupfs
1194+
```yaml
1195+
spec:
1196+
kubelet:
1197+
cgroupDriver: cgroupfs
1198+
```
1199+
1200+
Setting docker to use cgroupfs
1201+
```yaml
1202+
spec:
1203+
docker:
1204+
execOpt:
1205+
- native.cgroupdriver=cgroupfs
1206+
```
1207+
1208+
To set containerd cgroup-driver to cgroupfs, just override the config like below
1209+
```yaml
1210+
spec:
1211+
containerd:
1212+
configOverride: ""
1213+
```

nodeup/pkg/model/docker_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func TestDockerBuilder_BuildFlags(t *testing.T) {
9797
kops.DockerConfig{Bridge: fi.String("br0")},
9898
"--bridge=br0",
9999
},
100+
{
101+
kops.DockerConfig{ExecOpt: []string{"native.cgroupdriver=systemd"}},
102+
"--exec-opt=native.cgroupdriver=systemd",
103+
},
100104
}
101105

102106
for _, g := range grid {

nodeup/pkg/model/kubelet.go

+5
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ func (b *KubeletBuilder) buildManifestDirectory(kubeletConfig *kops.KubeletConfi
176176

177177
// buildSystemdEnvironmentFile renders the environment file for the kubelet
178178
func (b *KubeletBuilder) buildSystemdEnvironmentFile(kubeletConfig *kops.KubeletConfigSpec) (*nodetasks.File, error) {
179+
// Use systemd as the default cgroup driver from k8s 1.20
180+
if b.IsKubernetesGTE("1.20") && kubeletConfig.CgroupDriver == "" {
181+
kubeletConfig.CgroupDriver = "systemd"
182+
}
183+
179184
// @step: ensure the masters do not get a bootstrap configuration
180185
if b.UseBootstrapTokens() && b.IsMaster {
181186
kubeletConfig.BootstrapKubeconfig = ""

pkg/model/components/containerd.go

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package components
1818

1919
import (
2020
"fmt"
21+
"k8s.io/klog/v2"
2122

2223
"github.com/blang/semver/v4"
2324
"github.com/pelletier/go-toml"
@@ -61,7 +62,16 @@ func (b *ContainerdOptionsBuilder) BuildOptions(o interface{}) error {
6162
for name, endpoints := range containerd.RegistryMirrors {
6263
config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "registry", "mirrors", name, "endpoint"}, endpoints)
6364
}
65+
66+
//default cgroup-driver to systemd from k8s 1.20 onwards
67+
if b.IsKubernetesGTE("1.20") {
68+
config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", "runc", "runtime_type"}, "io.containerd.runc.v2")
69+
}
70+
6471
containerd.ConfigOverride = fi.String(config.String())
72+
} else {
73+
klog.Warning("Overriding the configuration of containerd has to be done with care as the default config may" +
74+
" change with releases which can lead to incompatibilities.")
6575
}
6676

6777
} else if clusterSpec.ContainerRuntime == "docker" {

pkg/model/components/docker.go

+5
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,10 @@ func (b *DockerOptionsBuilder) BuildOptions(o interface{}) error {
7373
// and it is an error to specify the flag twice.
7474
docker.Storage = fi.String("overlay2,overlay,aufs")
7575

76+
// default systemd as cgroup driver in docker from k8s 1.20
77+
if b.IsKubernetesGTE("1.20") && len(docker.ExecOpt) == 0 {
78+
docker.ExecOpt = append(docker.ExecOpt, "native.cgroupdriver=systemd")
79+
}
80+
7681
return nil
7782
}

pkg/model/components/kubelet.go

+5
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,10 @@ func (b *KubeletOptionsBuilder) BuildOptions(o interface{}) error {
213213
}
214214
}
215215

216+
// default to systemd as cgroup driver for kubelet from k8s 1.20
217+
if b.IsKubernetesGTE("1.20") && clusterSpec.Kubelet.CgroupDriver == "" {
218+
clusterSpec.Kubelet.CgroupDriver = "systemd"
219+
}
220+
216221
return nil
217222
}

0 commit comments

Comments
 (0)