From b11575a5ab38602d51cc83a01ea30fef5b0e6a3f Mon Sep 17 00:00:00 2001 From: wlp1153468871 Date: Thu, 12 May 2022 01:20:42 +0800 Subject: [PATCH] [zh]sync-setup-3 --- .../container-runtimes.md | 716 +++++++----------- 1 file changed, 260 insertions(+), 456 deletions(-) diff --git a/content/zh/docs/setup/production-environment/container-runtimes.md b/content/zh/docs/setup/production-environment/container-runtimes.md index 03380bae14..431232cd64 100644 --- a/content/zh/docs/setup/production-environment/container-runtimes.md +++ b/content/zh/docs/setup/production-environment/container-runtimes.md @@ -7,7 +7,7 @@ weight: 20 reviewers: - vincepri - bart0sh -title: Container runtimes +title: Container Runtimes content_type: concept weight: 20 --> @@ -15,6 +15,8 @@ weight: 20 -本文列出了在 Linux 上结合 Kubernetes 使用的几种通用容器运行时的详细信息: + +Kubernetes {{< skew currentVersion >}} 要求你使用符合{{}} (CRI)的运行时。 + +有关详细信息,请参阅 [CRI 版本支持](#cri-versions)。 +本页简要介绍在 Kubernetes 中几个常见的容器运行时的用法。 - [containerd](#containerd) - [CRI-O](#cri-o) -- [Docker](#docker) +- [Docker Engine](#docker) +- [Mirantis Container Runtime](#mcr) -提示:对于其他操作系统,请查阅特定于你所使用平台的相关文档。 + {{< note >}} +提示:v1.24 之前的 Kubernetes 版本包括与 Docker Engine 的直接集成,使用名为 _dockershim_ 的组件。 +这种特殊的直接整合不再是 Kubernetes 的一部分 +(这次删除被作为 v1.20 发行版本的一部分[宣布](/zh/blog/2020/12/08/kubernetes-1-20-release-announcement/#dockershim-deprecation))。 +你可以阅读[检查 Dockershim 弃用是否会影响你](/zh/docs/tasks/administer-cluster/migrating-from-dockershim/check-if-dockershim-deprecation-affects-you/) +以了解此删除可能会如何影响你。 +要了解如何使用 dockershim 进行迁移,请参阅[从 dockershim 迁移](/zh/docs/tasks/administer-cluster/migrating-from-dockershim/)。 + +如果你正在运行 v{{< skew currentVersion >}} 以外的 Kubernetes 版本,检查该版本的文档。 +{{< /note >}} + + + + +## 安装和配置先决条件 + +以下步骤将通用设置应用于 Linux 上的 Kubernetes 节点。 + +如果你确定不需要某个特定设置,则可以跳过它。 + +有关更多信息,请参阅[网络插件要求](/zh/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/#network-plugin-requirements) +或特定容器运行时的文档。 + + + +### 转发 IPv4 并让 iptables 看到桥接流量 + +通过运行 `lsmod | grep br_netfilter` 来验证 `br_netfilter` 模块是否已加载。 + +若要显式加载此模块,请运行 `sudo modprobe br_netfilter`。 + +为了让 Linux 节点的 iptables 能够正确查看桥接流量,请确认 `sysctl` 配置中的 +`net.bridge.bridge-nf-call-iptables` 设置为 1。 例如: + + + +```bash +cat <}} are used to constrain resources that are allocated to processes. When [systemd](https://www.freedesktop.org/wiki/Software/systemd/) is chosen as the init system for a Linux distribution, the init process generates and consumes a root control group @@ -58,10 +169,10 @@ Systemd has a tight integration with cgroups and allocates a cgroup per systemd to configure your container runtime and the kubelet to use `cgroupfs`. Using `cgroupfs` alongside systemd means that there will be two different cgroup managers. --> -控制组用来约束分配给进程的资源。 +在 Linux 上,{{}}用于限制分配给进程的资源。 当某个 Linux 系统发行版使用 [systemd](https://www.freedesktop.org/wiki/Software/systemd/) -作为其初始化系统时,初始化进程会生成并使用一个 root 控制组 (`cgroup`), 并充当 cgroup 管理器。 +作为其初始化系统时,初始化进程会生成并使用一个 root 控制组(`cgroup`),并充当 cgroup 管理器。 Systemd 与 cgroup 集成紧密,并将为每个 systemd 单元分配一个 cgroup。 你也可以配置容器运行时和 kubelet 使用 `cgroupfs`。 连同 systemd 一起使用 `cgroupfs` 意味着将有两个不同的 cgroup 管理器。 @@ -107,12 +218,13 @@ configuration, or reinstall it using automation. 或者使用自动化方案来重新安装。 -## Cgroup v2 +### Cgroup v2 {#cgroup-v2} + Cgroup v2 是 cgroup Linux API 的下一个版本。与 cgroup v1 不同的是, Cgroup v2 只有一个层次结构,而不是每个控制器有一个不同的层次结构。 @@ -143,22 +255,36 @@ Kubernetes 仅支持使用同一 cgroup 版本来管理所有控制器。 如果 systemd 默认不使用 cgroup v2,你可以通过在内核命令行中添加 `systemd.unified_cgroup_hierarchy=1` 来配置系统去使用它。 + + +```shell +# 此示例适用于使用 DNF 包管理器的 Linux 操作系统 +# 你的系统可能使用不同的方法来设置 Linux 内核使用的命令行。 +sudo dnf install -y grubby && \ sudo grubby \ --update-kernel=ALL \ --args="systemd.unified_cgroup_hierarchy=1" ``` -要应用配置,必须重新启动节点。 +如果更改内核的命令行,则必须重新启动节点才能使更改生效。 切换到 cgroup v2 时,用户体验不应有任何明显差异, 除非用户直接在节点上或在容器内访问 cgroup 文件系统。 @@ -170,11 +296,23 @@ In order to use it, cgroup v2 must be supported by the CRI runtime as well. ### 将 kubeadm 托管的集群迁移到 `systemd` 驱动 -如果你想迁移到现有 kubeadm 托管集群中的 `systemd` cgroup 驱动程序, -遵循此[迁移指南](/zh/docs/tasks/administer-cluster/kubeadm/configure-cgroup-driver)。 +如果你希望将现有的由 kubeadm 管理的集群迁移到 `systemd` cgroup 驱动程序, +请按照[配置 cgroup 驱动程序](/zh/docs/tasks/administer-cluster/kubeadm/configure-cgroup-driver/)操作。 + +## CRI 版本支持 {#cri-versions} + +你的容器运行时必须至少支持容器运行时接口的 v1alpha2。 + +Kubernetes {{< skew currentVersion >}} 默认使用 v1 的 CRI API。如果容器运行时不支持 v1 API, +则 kubelet 会回退到使用(已弃用的)v1alpha2 API。 -本节包含使用 containerd 作为 CRI 运行时的必要步骤。 +本节概述了使用 containerd 作为 CRI 运行时的必要步骤。 使用以下命令在系统上安装 Containerd: -安装和配置的先决条件: + -```shell -cat < -安装 containerd: - -{{< tabs name="tab-cri-containerd-installation" >}} +{{< tabs name="Finding your config.toml file" >}} {{% tab name="Linux" %}} - - -1. 从官方Docker仓库安装 `containerd.io` 软件包。可以在 - [安装 Docker 引擎](https://docs.docker.com/engine/install/#server) - 中找到有关为各自的 Linux 发行版设置 Docker 存储库和安装 `containerd.io` - 软件包的说明。 - - -2. 配置 containerd: - - ```shell - sudo mkdir -p /etc/containerd - containerd config default | sudo tee /etc/containerd/config.toml - ``` - - -3. 重新启动 containerd: - - ```shell - sudo systemctl restart containerd - ``` - -{{% /tab %}} -{{% tab name="Windows (PowerShell)" %}} - - -启动 Powershell 会话,将 `$Version` 设置为所需的版本(例如:`$Version=1.4.3`), -然后运行以下命令: - - -1. 下载 containerd: - - ```powershell - curl.exe -L https://github.com/containerd/containerd/releases/download/v$Version/containerd-$Version-windows-amd64.tar.gz -o containerd-windows-amd64.tar.gz - tar.exe xvf .\containerd-windows-amd64.tar.gz - ``` - -2. 提取并配置: - - ```powershell - Copy-Item -Path ".\bin\" -Destination "$Env:ProgramFiles\containerd" -Recurse -Force - cd $Env:ProgramFiles\containerd\ - .\containerd.exe config default | Out-File config.toml -Encoding ascii - - # 检查配置。根据你的配置,可能需要调整: - # - sandbox_image (Kubernetes pause 镜像) - # - cni bin_dir 和 conf_dir 位置 - Get-Content config.toml - - # (可选 - 不过强烈建议) 禁止 Windows Defender 扫描 containerd - Add-MpPreference -ExclusionProcess "$Env:ProgramFiles\containerd\containerd.exe" - ``` - -3. 启动 containerd: - - ```powershell - .\containerd.exe --register-service - Start-Service containerd - ``` - + +你可以在路径 `/etc/containerd/config.toml` 下找到此文件。 {{% /tab %}} +{{< tab name="Windows" >}} + +你可以在路径 `C:\Program Files\containerd\config.toml` 下找到此文件。 +{{< /tab >}} {{< /tabs >}} +On Linux the default CRI socket for containerd is `/run/containerd/containerd.sock`. +On Windows the default CRI endpoint is `npipe://./pipe/containerd-containerd`. -#### 使用 `systemd` cgroup 驱动程序 {#containerd-systemd} +#### Configuring the `systemd` cgroup driver {#containerd-systemd} +--> +在 Linux 上,containerd 的默认 CRI 套接字是 `/run/containerd/containerd.sock`。 +在 Windows 上,默认 CRI 端点是 `npipe://./pipe/containerd-containerd`。 + +#### 配置 `systemd` cgroup 驱动程序 {#containerd-systemd} -如果您应用此更改,请确保再次重新启动 containerd: +如果你应用此更改,请确保重新启动 containerd: ```shell sudo systemctl restart containerd @@ -345,274 +395,26 @@ When using kubeadm, manually configure the 本节包含安装 CRI-O 作为容器运行时的必要步骤。 -使用以下命令在系统中安装 CRI-O: - -{{< note >}} - -CRI-O 的主要以及次要版本必须与 Kubernetes 的主要和次要版本相匹配。 -更多信息请查阅 -[CRI-O 兼容性列表](https://github.com/cri-o/cri-o#compatibility-matrix-cri-o--kubernetes)。 -{{< /note >}} + +要安装 CRI-O,请按照 [CRI-O 安装说明](https://github.com/cri-o/cri-o/blob/main/install.md#readme)执行操作。 + + + +#### cgroup 驱动程序 -安装并配置前置环境: -```shell +CRI-O 默认使用 systemd cgroup 驱动程序,这对你来说可能工作得很好。要切换到 `cgroupfs` cgroup 驱动程序, +请编辑 `/etc/crio/crio.conf` 或在 `/etc/crio/crio.conf.d/02-cgroup-manager.conf` 中放置一个插入式配置 ,例如: -# 创建 .conf 文件以在启动时加载模块 -cat <}} -{{% tab name="Debian" %}} - - -在下列操作系统上安装 CRI-O, 使用下表中合适的值设置环境变量 `OS`: - -| 操作系统 | `$OS` | -| ---------------- | ----------------- | -| Debian Unstable | `Debian_Unstable` | -| Debian Testing | `Debian_Testing` | - -
-然后,将 `$VERSION` 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 -例如,如果你要安装 CRI-O 1.20, 请设置 `VERSION=1.20`. -你也可以安装一个特定的发行版本。 -例如要安装 1.20.0 版本,设置 `VERSION=1.20.0:1.20.0`. -
- -然后执行 - -```shell -cat < -Then, set `$VERSION` to the CRI-O version that matches your Kubernetes version. -For instance, if you want to install CRI-O 1.20, set `VERSION=1.20`. -You can pin your installation to a specific release. -To install version 1.20.0, set `VERSION=1.20:1.20.0`. -
- -Then run ---> -在下列操作系统上安装 CRI-O, 使用下表中合适的值设置环境变量 `OS`: - -| 操作系统 | `$OS` | -| ---------------- | ----------------- | -| Ubuntu 20.04 | `xUbuntu_20.04` | -| Ubuntu 19.10 | `xUbuntu_19.10` | -| Ubuntu 19.04 | `xUbuntu_19.04` | -| Ubuntu 18.04 | `xUbuntu_18.04` | - -
-然后,将 `$VERSION` 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 -例如,如果你要安装 CRI-O 1.20, 请设置 `VERSION=1.20`. -你也可以安装一个特定的发行版本。 -例如要安装 1.20.0 版本,设置 `VERSION=1.20:1.20.0`. -
- -然后执行 - -```shell -cat < -Then, set `$VERSION` to the CRI-O version that matches your Kubernetes version. -For instance, if you want to install CRI-O 1.20, set `VERSION=1.20`. -You can pin your installation to a specific release. -To install version 1.20.0, set `VERSION=1.20:1.20.0`. -
- -Then run ---> -在下列操作系统上安装 CRI-O, 使用下表中合适的值设置环境变量 `OS`: - -| 操作系统 | `$OS` | -| ---------------- | ----------------- | -| Centos 8 | `CentOS_8` | -| Centos 8 Stream | `CentOS_8_Stream` | -| Centos 7 | `CentOS_7` | - -
-然后,将 `$VERSION` 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 -例如,如果你要安装 CRI-O 1.20, 请设置 `VERSION=1.20`. -你也可以安装一个特定的发行版本。 -例如要安装 1.20.0 版本,设置 `VERSION=1.20:1.20.0`. -
- -然后执行 - -```shell -sudo curl -L -o /etc/yum.repos.d/devel:kubic:libcontainers:stable.repo https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/devel:kubic:libcontainers:stable.repo -sudo curl -L -o /etc/yum.repos.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.repo https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$VERSION/$OS/devel:kubic:libcontainers:stable:cri-o:$VERSION.repo -sudo yum install cri-o -``` - -{{% /tab %}} - -{{% tab name="openSUSE Tumbleweed" %}} - -```shell -sudo zypper install cri-o -``` -{{% /tab %}} -{{% tab name="Fedora" %}} - - -将 `$VERSION` 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 -例如,如果要安装 CRI-O 1.20,请设置 `VERSION=1.20`。 -你可以用下列命令查找可用的版本: - -```shell -sudo dnf module list cri-o -``` - -CRI-O 不支持在 Fedora 上固定到特定的版本。 - -然后执行 - -```shell -sudo dnf module enable cri-o:$VERSION -sudo dnf install cri-o --now -``` - -{{% /tab %}} -{{< /tabs >}} - - -启动 CRI-O: - -```shell -sudo systemctl daemon-reload -sudo systemctl enable crio --now -``` - - -参阅[CRI-O 安装指南](https://github.com/cri-o/cri-o/blob/master/install.md) -了解进一步的详细信息。 - - -#### cgroup 驱动 - -默认情况下,CRI-O 使用 systemd cgroup 驱动程序。要切换到 `cgroupfs` -驱动程序,或者编辑 `/ etc / crio / crio.conf` 或放置一个插件 -在 `/etc/crio/crio.conf.d/02-cgroup-manager.conf` 中的配置,例如: ```toml [crio.runtime] @@ -620,77 +422,79 @@ conmon_cgroup = "pod" cgroup_manager = "cgroupfs" ``` - -另请注意更改后的 `conmon_cgroup`,将 CRI-O 与 `cgroupfs` 一起使用时, -必须将其设置为 `pod`。通常有必要保持 kubelet 的 cgroup 驱动程序配置 -(通常透过 kubeadm 完成)和 CRI-O 一致。 - -### Docker - -1. 在每个节点上,根据[安装 Docker 引擎](https://docs.docker.com/engine/install/#server) - 为你的 Linux 发行版安装 Docker。 - 你可以在此文件中找到最新的经过验证的 Docker 版本 - [依赖关系](https://git.k8s.io/kubernetes/build/dependencies.yaml)。 - - -2. 配置 Docker 守护程序,尤其是使用 systemd 来管理容器的 cgroup。 - - ```shell - sudo mkdir /etc/docker - cat <}} - - 对于运行 Linux 内核版本 4.0 或更高版本,或使用 3.10.0-51 及更高版本的 RHEL - 或 CentOS 的系统,`overlay2`是首选的存储驱动程序。 - {{< /note >}} - - -3. 重新启动 Docker 并在启动时启用: - ```shell - sudo systemctl enable docker - sudo systemctl daemon-reload - sudo systemctl restart docker - ``` +你还应该注意到 `conmon_cgroup` 被更改,当使用 CRI-O 和 `cgroupfs` 时,必须将其设置为值 `pod`。 +通常需要保持 kubelet 的 cgroup 驱动配置(通常通过 kubeadm 完成)和 CRI-O 同步。 + + +对于 CRI-O,CRI 套接字默认为 `/var/run/crio/crio.sock`。 + +### Docker Engine {#docker} + + {{< note >}} +以下操作假设你使用 [`cri-dockerd`](https://github.com/Mirantis/cri-dockerd) 适配器来将 +Docker Engine 与 Kubernetes 集成。 +{{< /note >}} + + +1. 在你的每个节点上,遵循[安装 Docker 引擎](https://docs.docker.com/engine/install/#server)指南为你的 + Linux 发行版安装 Docker。 + + -有关更多信息,请参阅 +2. 按照源代码仓库中的说明安装 [`cri-dockerd`](https://github.com/Mirantis/cri-dockerd)。 + + + +对于 `cri-dockerd`,默认情况下,CRI 套接字是 `/run/cri-dockerd.sock`。 + +### Mirantis 容器运行时 {#mcr} + + +[Mirantis Container Runtime](https://docs.mirantis.com/mcr/20.10/overview.html) (MCR) 是一种商用容器运行时,以前称为 Docker 企业版。 +你可以使用 MCR 中包含的开源 [`cri-dockerd`](https://github.com/Mirantis/cri-dockerd) 组件将 Mirantis Container Runtime 与 Kubernetes 一起使用。 + + +要了解有关如何安装 Mirantis Container Runtime 的更多信息,请访问 [MCR 部署指南](https://docs.mirantis.com/mcr/20.10/install.html)。 + +检查名为 `cri-docker.socket` 的 systemd 单元以找出 CRI 套接字的路径。 + +## {{% heading "whatsnext" %}} + + + +除了容器运行时,你的集群还需要有效的[网络插件](/zh/docs/concepts/cluster-administration/networking/#how-to-implement-the-kubernetes-networking-model)。 -- [配置 Docker 守护程序](https://docs.docker.com/config/daemon/) -- [使用 systemd 控制 Docker](https://docs.docker.com/config/daemon/systemd/) -{{< /note >}}