Skip to content

Commit e1e7bc6

Browse files
committed
[zh] Add provision-swap-memory.md
1 parent a5bdf2c commit e1e7bc6

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
---
2+
title: 在 Kubernetes 节点上配置交换内存
3+
content_type: tutorial
4+
weight: 35
5+
min-kubernetes-server-version: "1.33"
6+
---
7+
<!--
8+
reviewers:
9+
- lmktfy
10+
title: Configuring swap memory on Kubernetes nodes
11+
content_type: tutorial
12+
weight: 35
13+
min-kubernetes-server-version: "1.33"
14+
-->
15+
16+
<!-- overview -->
17+
18+
<!--
19+
This page provides an example of how to provision and configure swap memory on a Kubernetes node using kubeadm.
20+
-->
21+
本文演示了如何使用 kubeadm 在 Kubernetes 节点上制备和启用交换内存。
22+
23+
<!-- lessoncontent -->
24+
25+
## {{% heading "objectives" %}}
26+
27+
<!--
28+
* Provision swap memory on a Kubernetes node using kubeadm.
29+
* Learn to configure both encrypted and unencrypted swap.
30+
* Learn to enable swap on boot.
31+
-->
32+
* 使用 kubeadm 在 Kubernetes 节点上制备交换内存。
33+
* 学习配置加密和未加密的交换内存。
34+
* 学习如何在系统启动时启用交换内存。
35+
36+
## {{% heading "prerequisites" %}}
37+
38+
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
39+
40+
<!--
41+
You need at least one worker node in your cluster which needs to run a Linux operating system.
42+
It is required for this demo that the kubeadm tool be installed, following the steps outlined in the
43+
[kubeadm installation guide](/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm).
44+
-->
45+
你需要在集群中至少有一个运行 Linux 操作系统的工作节点。
46+
本次演示需要先安装 kubeadm 工具,安装步骤请参考
47+
[kubeadm 安装指南](/zh-cn/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm)
48+
49+
<!--
50+
On each worker node where you will configure swap use, you need:
51+
* `fallocate`
52+
* `mkswap`
53+
* `swapon`
54+
55+
* For encrypted swap space (recommended), you also need:
56+
* `cryptsetup`
57+
-->
58+
在每个需要配置交换内存的工作节点上,你需要以下工具:
59+
60+
* `fallocate`
61+
* `mkswap`
62+
* `swapon`
63+
64+
对于加密的交换空间(推荐),你还需要:
65+
66+
* `cryptsetup`
67+
68+
<!-- lessoncontent -->
69+
70+
<!--
71+
## Install a swap-enabled cluster with kubeadm
72+
73+
### Create a swap file and turn swap on
74+
75+
If swap is not enabled, there's a need to provision swap on the node.
76+
The following sections demonstrate creating 4GiB of swap, both in the encrypted and unencrypted case.
77+
-->
78+
## 使用 kubeadm 安装支持交换内存的集群
79+
80+
### 创建交换文件并启用交换内存
81+
82+
如果当前节点未启用交换内存,则需要先**制备**交换空间。
83+
本节将展示如何以加密和未加密的方式创建 4GiB 的交换文件。
84+
85+
<!--
86+
"Create a swap file and turn swap on"
87+
"Setting up encrypted swap"
88+
-->
89+
{{< tabs name="创建交换文件并启用交换内存" >}}
90+
91+
{{% tab name="设置加密的交换内存" %}}
92+
93+
<!--
94+
An encrypted swap file can be set up as follows.
95+
Bear in mind that this example uses the `cryptsetup` binary (which is available
96+
on most Linux distributions).
97+
-->
98+
你可以使用如下命令设置加密的交换文件。
99+
请注意,此示例使用的是 `cryptsetup` 工具(在大多数 Linux 发行版中都可用):
100+
101+
<!--
102+
```bash
103+
# Allocate storage and restrict access
104+
fallocate --length 4GiB /swapfile
105+
chmod 600 /swapfile
106+
107+
# Create an encrypted device backed by the allocated storage
108+
cryptsetup --type plain --cipher aes-xts-plain64 --key-size 256 -d /dev/urandom open /swapfile cryptswap
109+
110+
# Format the swap space
111+
mkswap /dev/mapper/cryptswap
112+
113+
# Activate the swap space for paging
114+
swapon /dev/mapper/cryptswap
115+
```
116+
-->
117+
```bash
118+
# 分配存储空间并限制访问权限
119+
fallocate --length 4GiB /swapfile
120+
chmod 600 /swapfile
121+
122+
# 基于已分配的存储空间创建加密设备
123+
cryptsetup --type plain --cipher aes-xts-plain64 --key-size 256 -d /dev/urandom open /swapfile cryptswap
124+
125+
# 格式化此交换空间
126+
mkswap /dev/mapper/cryptswap
127+
128+
# 为换页激活交换空间
129+
swapon /dev/mapper/cryptswap
130+
```
131+
132+
{{% /tab %}}
133+
134+
<!--
135+
"Setting up unencrypted swap"
136+
-->
137+
{{% tab name="设置未加密的交换内存" %}}
138+
<!--
139+
An unencrypted swap file can be set up as follows.
140+
141+
```bash
142+
# Allocate storage and restrict access
143+
fallocate --length 4GiB /swapfile
144+
chmod 600 /swapfile
145+
146+
# Format the swap space
147+
mkswap /swapfile
148+
149+
# Activate the swap space for paging
150+
swapon /swapfile
151+
```
152+
-->
153+
未加密的交换文件可以按以下方式配置:
154+
155+
```bash
156+
# 分配存储空间并限制访问权限
157+
fallocate --length 4GiB /swapfile
158+
chmod 600 /swapfile
159+
160+
# 格式化此交换空间
161+
mkswap /swapfile
162+
163+
# 为换页激活交换空间
164+
swapon /swapfile
165+
```
166+
167+
{{% /tab %}}
168+
169+
{{< /tabs >}}
170+
171+
<!--
172+
#### Verify that swap is enabled
173+
174+
Swap can be verified to be enabled with both `swapon -s` command or the `free` command.
175+
176+
Using `swapon -s`:
177+
-->
178+
#### 验证交换内存是否启用
179+
180+
你可以使用 `swapon -s` 命令或 `free` 命令来验证交换内存是否启用。
181+
182+
使用 `swapon -s`
183+
184+
```
185+
Filename Type Size Used Priority
186+
/dev/dm-0 partition 4194300 0 -2
187+
```
188+
189+
使用 `free -h`
190+
<!--
191+
Using `free -h`:
192+
-->
193+
194+
```
195+
total used free shared buff/cache available
196+
Mem: 3.8Gi 1.3Gi 249Mi 25Mi 2.5Gi 2.5Gi
197+
Swap: 4.0Gi 0B 4.0Gi
198+
```
199+
200+
<!--
201+
#### Enable swap on boot
202+
203+
After setting up swap, to start the swap file at boot time,
204+
you typically either set up a systemd unit to activate (encrypted) swap, or you
205+
add a line similar to `/swapfile swap swap defaults 0 0` into `/etc/fstab`.
206+
207+
Using systemd for swap activation allows the system to delay kubelet start until swap is available,
208+
if that is something you want to ensure.
209+
In a similar way, using systemd allows your server to leave swap active until kubelet
210+
(and, typically, your container runtime) have shut down.
211+
-->
212+
#### 引导时启用交换内存
213+
214+
在设置好交换内存后,若要在系统引导时启动交换文件,通常有两种做法:
215+
你可以设置一个 systemd 单元来激活(加密的)交换内存,或者在
216+
`/etc/fstab` 文件中添加类似于 `/swapfile swap swap defaults 0 0` 的行。
217+
218+
使用 systemd 激活交换内存,可以确保在交换内存可用之前延迟启动 kubelet(如果你有这个需求)。
219+
同样,使用 systemd 还可以让服务器在 kubelet(以及通常的容器运行时)关闭之前保持交换内存处于启用状态。
220+
221+
<!--
222+
### Set up kubelet configuration
223+
224+
After enabling swap on the node, kubelet needs to be configured in the following way:
225+
226+
```yaml
227+
# this fragment goes into the kubelet's configuration file
228+
failSwapOn: false
229+
memorySwap:
230+
swapBehavior: LimitedSwap
231+
```
232+
233+
In order for these configurations to take effect, kubelet needs to be restarted.
234+
-->
235+
### 配置 kubelet {#set-up-kubelet-configuration}
236+
237+
在节点上启用交换内存后,需要按如下方式配置 kubelet:
238+
239+
```yaml
240+
# 此代码片段应添加到 kubelet 的配置文件中
241+
failSwapOn: false
242+
memorySwap:
243+
swapBehavior: LimitedSwap
244+
```
245+
246+
为了使这些配置生效,需重启 kubelet。

0 commit comments

Comments
 (0)