Skip to content

Commit

Permalink
Use InPlacePodVerticalScaling feature gate instead of tweaking cgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
norbjd committed Oct 28, 2023
1 parent ee2e940 commit ba76c38
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 385 deletions.
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ The controller messes with cgroups file `cpu.max` to give that boost (or reset t

## Install

Use `ko`. Example on a `kind` cluster with cgroups v2 (default on latest operating systems):
Use `ko`. Example on a `kind` cluster:

```sh
KO_DOCKER_REPO=kind.local ko resolve -f config/ | \
CGROUP_VERSION=v2 K8S_DISTRIBUTION=kind envsubst | \
kubectl apply -f -
KO_DOCKER_REPO=kind.local ko apply -f config/
```

## Test/Demo
Expand All @@ -46,12 +44,10 @@ docker pull python:3.11-alpine
kind load docker-image python:3.11-alpine
```

Install `k8s-pod-cpu-booster` (change `CGROUP_VERSION` to `v1` if you're using cgroups v1):
Install `k8s-pod-cpu-booster`:

```sh
KO_DOCKER_REPO=kind.local ko resolve -f config/ | \
CGROUP_VERSION=v2 K8S_DISTRIBUTION=kind envsubst | \
kubectl apply -f -
KO_DOCKER_REPO=kind.local ko apply -f config/
```

Start two similar pods with low CPU limits and running `python -m http.server`, with a readiness probe configured to check when the http server is started. The only differences are the name (obviously), and the annotation `norbjd.github.io/k8s-pod-cpu-booster-enabled`:
Expand Down Expand Up @@ -90,11 +86,11 @@ done
Example result:

```
python-with-default-boost started at: 2023-09-03T15:55:45Z / ready at: 2023-09-03T15:55:46Z
python-no-boost started at: 2023-09-03T15:55:44Z / ready at: 2023-09-03T15:55:58Z
python-with-default-boost started at: 2023-10-28T14:00:46Z / ready at: 2023-10-28T14:00:49Z
python-no-boost started at: 2023-10-28T14:00:46Z / ready at: 2023-10-28T14:01:04Z
```

Here, the pod with the CPU boost (`python-with-default-boost`) took around 1 second to start, while the pod without CPU boost (`python-no-boost`) took around 14 seconds.
Here, the pod with the CPU boost (`python-with-default-boost`) took around 3 seconds to start, while the pod without CPU boost (`python-no-boost`) took around 18 seconds.

Cleanup:

Expand Down
28 changes: 1 addition & 27 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"flag"
"os"

"github.com/norbjd/k8s-pod-cpu-booster/pkg/cgroup"
"github.com/norbjd/k8s-pod-cpu-booster/pkg/informer"

"k8s.io/client-go/kubernetes"
Expand All @@ -26,29 +24,5 @@ func main() {
panic(err)
}

var cgroupHandler cgroup.Handler

// TODO: instead of using an env var, detect automatically the right cgroup handler to use
switch os.Getenv("CGROUP_VERSION") {
case "v1":
switch os.Getenv("K8S_DISTRIBUTION") {
case "kapsule":
klog.Info("Using V1KapsuleHandler")
cgroupHandler = cgroup.V1KapsuleHandler{}
default:
klog.Info("Using V1KindHandler")
cgroupHandler = cgroup.V1KindHandler{}
}
default:
switch os.Getenv("K8S_DISTRIBUTION") {
case "kapsule":
klog.Info("Using V2KapsuleHandler")
cgroupHandler = cgroup.V2KapsuleHandler{}
default:
klog.Info("Using V2KindHandler")
cgroupHandler = cgroup.V2KindHandler{}
}
}

informer.Run(clientset, cgroupHandler)
informer.Run(clientset)
}
1 change: 1 addition & 0 deletions config/200-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rules:
- list
- watch
- get
- update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
13 changes: 0 additions & 13 deletions config/300-pod-cpu-booster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,12 @@ spec:
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: CGROUP_VERSION
value: "$CGROUP_VERSION"
- name: K8S_DISTRIBUTION
value: "$K8S_DISTRIBUTION"
resources:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 100m
memory: 100Mi
securityContext:
runAsUser: 0 # required otherwise we won't be able to edit files under /sys/fs/cgroup
volumeMounts:
- name: sysfscgroup
mountPath: /sys/fs/cgroup
serviceAccountName: pod-cpu-booster
terminationGracePeriodSeconds: 0 # TODO: change for production environments
volumes:
- name: sysfscgroup
hostPath:
path: /sys/fs/cgroup
2 changes: 2 additions & 0 deletions examples/kind-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
featureGates:
InPlacePodVerticalScaling: true
nodes:
- role: control-plane
image: kindest/node:v1.28.0@sha256:b7a4cad12c197af3ba43202d3efe03246b3f0793f162afb40a33c923952d5b31
Expand Down
3 changes: 3 additions & 0 deletions examples/python-with-default-boost.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ spec:
periodSeconds: 1
successThreshold: 1
timeoutSeconds: 1
resizePolicy:
- resourceName: cpu
restartPolicy: NotRequired # this is very important to be able to update the CPU resources in place (no need to restart the pod)
resources:
requests:
cpu: 50m
Expand Down
48 changes: 0 additions & 48 deletions pkg/cgroup/cgroup.go

This file was deleted.

76 changes: 0 additions & 76 deletions pkg/cgroup/v1.go

This file was deleted.

28 changes: 0 additions & 28 deletions pkg/cgroup/v1_kapsule.go

This file was deleted.

31 changes: 0 additions & 31 deletions pkg/cgroup/v1_kind.go

This file was deleted.

44 changes: 0 additions & 44 deletions pkg/cgroup/v2.go

This file was deleted.

28 changes: 0 additions & 28 deletions pkg/cgroup/v2_kapsule.go

This file was deleted.

Loading

0 comments on commit ba76c38

Please sign in to comment.