|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: Modifying kernel parameters in a Kubernetes cluster using a DaemonSet |
| 4 | + description: This guide explains how to modify kernel parameters in a Kubernetes cluster using a DaemonSet |
| 5 | +content: |
| 6 | + h1: Modifying kernel parameters in a Kubernetes cluster using a DaemonSet |
| 7 | + paragraph: This guide explains how to modify kernel parameters in a Kubernetes cluster using a DaemonSet |
| 8 | +tags: kubernetes kernel |
| 9 | +dates: |
| 10 | + validation: 2024-10-24 |
| 11 | + posted: 2024-10-24 |
| 12 | +categories: |
| 13 | + - kubernetes |
| 14 | +--- |
| 15 | + |
| 16 | +Kernel parameters control the behavior of the operating system at runtime. They allow you to configure and fine-tune various aspects of the Linux kernel, such as networking, memory management, process handling, and security. These parameters are located in the `/proc/sys` directory on each node and can be dynamically modified at runtime using the `sysctl` command. |
| 17 | + |
| 18 | +This guide outlines how to modify kernel parameters across all nodes in a Kubernetes cluster using a DaemonSet. |
| 19 | + |
| 20 | +## Identifying the kernel parameters to modify |
| 21 | + |
| 22 | +Kernel parameters, managed via the `sysctl` command, are grouped into different categories depending on which part of the kernel they influence: |
| 23 | + |
| 24 | +- **Networking (`net.*`)**: Controls network-related settings such as buffer sizes, TCP/IP settings, and routing. |
| 25 | + *Example*: `net.ipv4.ip_forward` enables or disables IP packet forwarding, often used in routing scenarios. |
| 26 | + |
| 27 | +- **Memory Management (`vm.*`)**: Manages memory and swap behaviors. |
| 28 | + *Example*: `vm.swappiness` controls how aggressively the system swaps memory pages to disk. |
| 29 | + |
| 30 | +- **File System (`fs.*`)**: Configures file system-related limits and behaviors. |
| 31 | + *Example*: `fs.file-max` sets the maximum number of file descriptors the system can allocate. |
| 32 | + |
| 33 | +- **General Kernel Settings (`kernel.*`)**: Configures overall kernel behaviors. |
| 34 | + *Example*: `kernel.hostname` defines the system’s hostname. |
| 35 | + |
| 36 | +- **Security (`kernel.random.*`, `net.ipv4.conf.*`, etc.)**: Manages security settings such as IP forwarding, source address validation, and firewall rules. |
| 37 | + *Example*: `net.ipv4.conf.all.rp_filter` enables reverse path filtering for added network security. |
| 38 | + |
| 39 | +- **Process Limits (`kernel.*`)**: Controls limits for processes, such as the maximum number of processes or threads. |
| 40 | + *Example*: `kernel.pid_max` sets the maximum number of process IDs (PIDs) the system can allocate. |
| 41 | + |
| 42 | +## Creating a DaemonSet to modify kernel parameters |
| 43 | + |
| 44 | +To apply kernel parameter changes across all nodes in the cluster, you can create a Kubernetes DaemonSet that runs privileged pods. This will ensure the changes are applied to every node. |
| 45 | + |
| 46 | +Create a YAML file (e.g., `sysctl-daemonset.yaml`), copy/paste the following content into the file, save it and exit the text editor: |
| 47 | + |
| 48 | +```yaml |
| 49 | +apiVersion: apps/v1 |
| 50 | +kind: DaemonSet |
| 51 | +metadata: |
| 52 | + name: sysctl-tuning |
| 53 | + namespace: kube-system |
| 54 | + labels: |
| 55 | + app: sysctl-tuning |
| 56 | +spec: |
| 57 | + selector: |
| 58 | + matchLabels: |
| 59 | + app: sysctl-tuning |
| 60 | + template: |
| 61 | + metadata: |
| 62 | + labels: |
| 63 | + app: sysctl-tuning |
| 64 | + spec: |
| 65 | + hostNetwork: true # Share the host's network namespace for network-related sysctl changes |
| 66 | + hostPID: true # Access the host's PID namespace for sysctl commands |
| 67 | + initContainers: |
| 68 | + - name: sysctl-init # Init container to set sysctl parameters |
| 69 | + image: busybox:latest |
| 70 | + command: |
| 71 | + - /bin/sh |
| 72 | + - -c |
| 73 | + - | |
| 74 | + sysctl -w net.core.rmem_max=7500000 # Set the maximum receive buffer size |
| 75 | + sysctl -w net.core.wmem_max=7500000 # Set the maximum send buffer size |
| 76 | + securityContext: |
| 77 | + privileged: true # Privileged access to modify sysctl settings on the host |
| 78 | + containers: |
| 79 | + - name: sleep-container # Main container to keep the pod running |
| 80 | + image: busybox:latest |
| 81 | + command: |
| 82 | + - /bin/sh |
| 83 | + - -c |
| 84 | + - sleep infinity # Keep the pod alive indefinitely |
| 85 | +``` |
| 86 | +
|
| 87 | +## Applying the DaemonSet |
| 88 | +
|
| 89 | +To apply the configuration, use the following command: |
| 90 | +
|
| 91 | +```bash |
| 92 | +kubectl apply -f sysctl-daemonset.yaml |
| 93 | +``` |
| 94 | + |
| 95 | +This command deploys the DaemonSet, which ensures that the kernel parameters are modified on all nodes. |
| 96 | + |
| 97 | +## Verifying changes |
| 98 | + |
| 99 | +To verify that the DaemonSet is running on all nodes, use the following command: |
| 100 | + |
| 101 | +```bash |
| 102 | +kubectl get daemonset -n kube-system |
| 103 | +``` |
| 104 | + |
| 105 | +To check if the kernel parameters were successfully updated on a node, SSH into the node and run: |
| 106 | + |
| 107 | +```bash |
| 108 | +ssh <node-name> |
| 109 | +sysctl net.core.rmem_max |
| 110 | +sysctl net.core.wmem_max |
| 111 | +``` |
| 112 | + |
| 113 | +<Message type="note"> |
| 114 | + On Scaleway Kapsule SSH access is blocked by default. You need to enable SSH in your security group before connecting to the node. Refer to [How to enable or disable SSH ports on Kubernetes Kapsule cluster nodes](/containers/kubernetes/how-to/enable-disable-ssh/) for further information. |
| 115 | +</Message> |
| 116 | + |
| 117 | +## Cleaning up (Optional) |
| 118 | + |
| 119 | +If the DaemonSet is no longer needed after the kernel parameters have been modified, you can delete it with the following command: |
| 120 | + |
| 121 | +```bash |
| 122 | +kubectl delete -f sysctl-daemonset.yaml |
| 123 | +``` |
0 commit comments