Skip to content

Commit acb6f24

Browse files
alegrey91cristicalin
andauthoredAug 30, 2022
feat: add kubelet systemd service hardening option (#9194)
* feat: add kubelet systemd service hardening option * refactor: move variable name to kubelet_secure_addresses Co-authored-by: Cristian Calin <6627509+cristicalin@users.noreply.github.com> * docs: add diagram about kubelet_secure_addresses variable Co-authored-by: Cristian Calin <6627509+cristicalin@users.noreply.github.com>
1 parent 220f149 commit acb6f24

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed
 

‎docs/hardening.md

+9
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ kubelet_streaming_connection_idle_timeout: "5m"
8585
kubelet_make_iptables_util_chains: true
8686
kubelet_feature_gates: ["RotateKubeletServerCertificate=true","SeccompDefault=true"]
8787
kubelet_seccomp_default: true
88+
kubelet_systemd_hardening: true
89+
# In case you have multiple interfaces in your
90+
# control plane nodes and you want to specify the right
91+
# IP addresses, kubelet_secure_addresses allows you
92+
# to specify the IP from which the kubelet
93+
# will receive the packets.
94+
kubelet_secure_addresses: "192.168.10.110 192.168.10.111 192.168.10.112"
8895

8996
# additional configurations
9097
kube_owner: root
@@ -103,6 +110,8 @@ Let's take a deep look to the resultant **kubernetes** configuration:
103110
* The `encryption-provider-config` provide encryption at rest. This means that the `kube-apiserver` encrypt data that is going to be stored before they reach `etcd`. So the data is completely unreadable from `etcd` (in case an attacker is able to exploit this).
104111
* The `rotateCertificates` in `KubeletConfiguration` is set to `true` along with `serverTLSBootstrap`. This could be used in alternative to `tlsCertFile` and `tlsPrivateKeyFile` parameters. Additionally it automatically generates certificates by itself, but you need to manually approve them or at least using an operator to do this (for more details, please take a look here: <https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-tls-bootstrapping/>).
105112
* If you are installing **kubernetes** in an AppArmor-based OS (eg. Debian/Ubuntu) you can enable the `AppArmor` feature gate uncommenting the lines with the comment `# AppArmor-based OS` on top.
113+
* The `kubelet_systemd_hardening`, both with `kubelet_secure_addresses` setup a minimal firewall on the system. To better understand how these variables work, here's an explanatory image:
114+
![kubelet hardening](img/kubelet-hardening.png)
106115

107116
Once you have the file properly filled, you can run the **Ansible** command to start the installation:
108117

‎docs/img/kubelet-hardening.png

1.48 MB
Loading

‎docs/vars.md

+21
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,46 @@ Stack](https://github.com/kubernetes-sigs/kubespray/blob/master/docs/dns-stack.m
175175

176176
* *docker_options* - Commonly used to set
177177
``--insecure-registry=myregistry.mydomain:5000``
178+
178179
* *docker_plugins* - This list can be used to define [Docker plugins](https://docs.docker.com/engine/extend/) to install.
180+
179181
* *containerd_default_runtime* - If defined, changes the default Containerd runtime used by the Kubernetes CRI plugin.
182+
180183
* *containerd_additional_runtimes* - Sets the additional Containerd runtimes used by the Kubernetes CRI plugin.
181184
[Default config](https://github.com/kubernetes-sigs/kubespray/blob/master/roles/container-engine/containerd/defaults/main.yml) can be overriden in inventory vars.
185+
182186
* *http_proxy/https_proxy/no_proxy/no_proxy_exclude_workers/additional_no_proxy* - Proxy variables for deploying behind a
183187
proxy. Note that no_proxy defaults to all internal cluster IPs and hostnames
184188
that correspond to each node.
189+
185190
* *kubelet_cgroup_driver* - Allows manual override of the cgroup-driver option for Kubelet.
186191
By default autodetection is used to match container manager configuration.
187192
`systemd` is the preferred driver for `containerd` though it can have issues with `cgroups v1` and `kata-containers` in which case you may want to change to `cgroupfs`.
193+
188194
* *kubelet_rotate_certificates* - Auto rotate the kubelet client certificates by requesting new certificates
189195
from the kube-apiserver when the certificate expiration approaches.
196+
190197
* *kubelet_rotate_server_certificates* - Auto rotate the kubelet server certificates by requesting new certificates
191198
from the kube-apiserver when the certificate expiration approaches.
192199
**Note** that server certificates are **not** approved automatically. Approve them manually
193200
(`kubectl get csr`, `kubectl certificate approve`) or implement custom approving controller like
194201
[kubelet-rubber-stamp](https://github.com/kontena/kubelet-rubber-stamp).
202+
195203
* *kubelet_streaming_connection_idle_timeout* - Set the maximum time a streaming connection can be idle before the connection is automatically closed.
204+
196205
* *kubelet_make_iptables_util_chains* - If `true`, causes the kubelet ensures a set of `iptables` rules are present on host.
206+
207+
* *kubelet_systemd_hardening* - If `true`, provides kubelet systemd service with security features for isolation.
208+
209+
**N.B.** To enable this feature, ensure you are using the **`cgroup v2`** on your system. Check it out with command: `sudo ls -l /sys/fs/cgroup/*.slice`. If directory does not exists, enable this with the following guide: [enable cgroup v2](https://rootlesscontaine.rs/getting-started/common/cgroup2/#enabling-cgroup-v2).
210+
211+
* *kubelet_secure_addresses* - By default *kubelet_systemd_hardening* set the **control plane** `ansible_host` IPs as the `kubelet_secure_addresses`. In case you have multiple interfaces in your control plane nodes and the `kube-apiserver` is not bound to the default interface, you can override them with this variable.
212+
Example:
213+
214+
The **control plane** node may have 2 interfaces with the following IP addresses: `eth0:10.0.0.110`, `eth1:192.168.1.110`.
215+
216+
By default the `kubelet_secure_addresses` is set with the `10.0.0.110` the ansible control host uses `eth0` to connect to the machine. In case you want to use `eth1` as the outgoing interface on which `kube-apiserver` connects to the `kubelet`s, you should override the variable in this way: `kubelet_secure_addresses: "192.168.1.110"`.
217+
197218
* *node_labels* - Labels applied to nodes via kubelet --node-labels parameter.
198219
For example, labels can be set in the inventory as variables or more widely in group_vars.
199220
*node_labels* can only be defined as a dict:

‎roles/kubernetes/node/defaults/main.yml

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ kubelet_kubelet_cgroups_cgroupfs: "/system.slice/kubelet.service"
2222
### fail with swap on (default true)
2323
kubelet_fail_swap_on: true
2424

25+
# Set systemd service hardening features
26+
kubelet_systemd_hardening: false
27+
28+
# List of secure IPs for kubelet
29+
kubelet_secure_addresses: "{{ groups['kube_control_plane'] | map('extract', hostvars, ['ansible_host']) | join(' ') }}"
30+
2531
# Reserve this space for kube resources
2632
kube_memory_reserved: 256Mi
2733
kube_cpu_reserved: 100m

‎roles/kubernetes/node/templates/kubelet.service.j2

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ ExecStart={{ bin_dir }}/kubelet \
2424
$KUBELET_CLOUDPROVIDER
2525
Restart=always
2626
RestartSec=10s
27+
{% if kubelet_systemd_hardening %}
28+
# Hardening setup
29+
IPAddressDeny=any
30+
IPAddressAllow={{ kubelet_secure_addresses }}
31+
{% endif %}
2732

2833
[Install]
2934
WantedBy=multi-user.target

0 commit comments

Comments
 (0)
Please sign in to comment.