Skip to content

Commit

Permalink
[containerd] Simplify limiting number of open files per container (ku…
Browse files Browse the repository at this point in the history
…bernetes-sigs#9319)

by setting a default runtime spec with a patch for RLIMIT_NOFILE.

- Introduces containerd_base_runtime_spec_rlimit_nofile.
- Generates base_runtime_spec on-the-fly, to use the containerd version
  of the node.

(cherry picked from commit 5a8cf82)
  • Loading branch information
fungusakafungus authored and notCalle committed Dec 18, 2023
1 parent 6f79ebc commit ac982cf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 224 deletions.
19 changes: 11 additions & 8 deletions docs/containerd.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ is a list of such dictionaries.

Default runtime can be changed by setting `containerd_default_runtime`.

#### base_runtime_spec

`base_runtime_spec` key in a runtime dictionary can be used to explicitly
specify a runtime spec json file. We ship the default one which is generated
with `ctr oci spec > /etc/containerd/cri-base.json`. It will be used if you set
`base_runtime_spec: cri-base.json`. The main advantage of doing so is the presence of
`rlimits` section in this configuration, which will restrict the maximum number
of file descriptors(open files) per container to 1024.
#### Base runtime specs and limiting number of open files

`base_runtime_spec` key in a runtime dictionary is used to explicitly
specify a runtime spec json file. `runc` runtime has it set to `cri-base.json`,
which is generated with `ctr oci spec > /etc/containerd/cri-base.json` and
updated to include a custom setting for maximum number of file descriptors per
container.

You can change maximum number of file descriptors per container for the default
`runc` runtime by setting the `containerd_base_runtime_spec_rlimit_nofile`
variable.

You can tune many more [settings][runtime-spec] by supplying your own file name and content with `containerd_base_runtime_specs`:

Expand Down
13 changes: 11 additions & 2 deletions roles/container-engine/containerd/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ containerd_runc_runtime:
type: "io.containerd.runc.v2"
engine: ""
root: ""
# base_runtime_spec: cri-base.json # use this to limit number of file descriptors per container
base_runtime_spec: cri-base.json
options:
systemdCgroup: "{{ containerd_use_systemd_cgroup | ternary('true', 'false') }}"

Expand All @@ -23,8 +23,17 @@ containerd_additional_runtimes: []
# engine: ""
# root: ""

containerd_base_runtime_spec_rlimit_nofile: 16384

containerd_default_base_runtime_spec_patch:
process:
rlimits:
- type: RLIMIT_NOFILE
hard: "{{ containerd_base_runtime_spec_rlimit_nofile }}"
soft: "{{ containerd_base_runtime_spec_rlimit_nofile }}"

containerd_base_runtime_specs:
cri-base.json: "{{ lookup('file', 'cri-base.json') }}"
cri-base.json: "{{ containerd_default_base_runtime_spec | combine(containerd_default_base_runtime_spec_patch,recursive=1) }}"

containerd_grpc_max_recv_message_size: 16777216
containerd_grpc_max_send_message_size: 16777216
Expand Down
214 changes: 0 additions & 214 deletions roles/container-engine/containerd/files/cri-base.json

This file was deleted.

10 changes: 10 additions & 0 deletions roles/container-engine/containerd/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@
notify: restart containerd
when: http_proxy is defined or https_proxy is defined

- name: containerd | Generate default base_runtime_spec
register: ctr_oci_spec
command: "{{ containerd_bin_dir }}/ctr oci spec"
check_mode: false
changed_when: false

- name: containerd | Store generated default base_runtime_spec
set_fact:
containerd_default_base_runtime_spec: "{{ ctr_oci_spec.stdout | from_json }}"

- name: containerd | Write base_runtime_specs
copy:
content: "{{ item.value }}"
Expand Down

0 comments on commit ac982cf

Please sign in to comment.