容器运行时

你需要在集群内每个节点上安装一个 容器运行时 以使 Pod 可以运行在上面。本文概述了所涉及的内容并描述了与节点设置相关的任务。

本文列出了在 Linux 上结合 Kubernetes 使用的几种通用容器运行时的详细信息:

提示:对于其他操作系统,请查阅特定于你所使用平台的相关文档。

Cgroup 驱动程序

控制组用来约束分配给进程的资源。

当某个 Linux 系统发行版使用 systemd 作为其初始化系统时,初始化进程会生成并使用一个 root 控制组 (cgroup), 并充当 cgroup 管理器。 Systemd 与 cgroup 集成紧密,并将为每个 systemd 单元分配一个 cgroup。 你也可以配置容器运行时和 kubelet 使用 cgroupfs。 连同 systemd 一起使用 cgroupfs 意味着将有两个不同的 cgroup 管理器。

单个 cgroup 管理器将简化分配资源的视图,并且默认情况下将对可用资源和使用 中的资源具有更一致的视图。 当有两个管理器共存于一个系统中时,最终将对这些资源产生两种视图。 在此领域人们已经报告过一些案例,某些节点配置让 kubelet 和 docker 使用 cgroupfs,而节点上运行的其余进程则使用 systemd; 这类节点在资源压力下 会变得不稳定。

更改设置,令容器运行时和 kubelet 使用 systemd 作为 cgroup 驱动,以此使系统更为稳定。 对于 Docker, 设置 native.cgroupdriver=systemd 选项。

注意:更改已加入集群的节点的 cgroup 驱动是一项敏感的操作。 如果 kubelet 已经使用某 cgroup 驱动的语义创建了 pod,更改运行时以使用 别的 cgroup 驱动,当为现有 Pods 重新创建 PodSandbox 时会产生错误。 重启 kubelet 也可能无法解决此类问题。 如果你有切实可行的自动化方案,使用其他已更新配置的节点来替换该节点, 或者使用自动化方案来重新安装。

将 kubeadm 托管的集群迁移到 systemd 驱动

如果你想迁移到现有 kubeadm 托管集群中的 systemd cgroup 驱动程序, 遵循此迁移指南

容器运行时

注意: 本部分链接到提供 Kubernetes 所需功能的第三方项目。Kubernetes 项目作者不负责这些项目。此页面遵循CNCF 网站指南,按字母顺序列出项目。要将项目添加到此列表中,请在提交更改之前阅读内容指南

containerd

本节包含使用 containerd 作为 CRI 运行时的必要步骤。

使用以下命令在系统上安装 Containerd:

安装和配置的先决条件:

  1. cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
  2. overlay
  3. br_netfilter
  4. EOF
  5. sudo modprobe overlay
  6. sudo modprobe br_netfilter
  7. # 设置必需的 sysctl 参数,这些参数在重新启动后仍然存在。
  8. cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
  9. net.bridge.bridge-nf-call-iptables = 1
  10. net.ipv4.ip_forward = 1
  11. net.bridge.bridge-nf-call-ip6tables = 1
  12. EOF
  13. # 应用 sysctl 参数而无需重新启动
  14. sudo sysctl --system

安装 containerd:

  1. 从官方Docker仓库安装 containerd.io 软件包。可以在 安装 Docker 引擎 中找到有关为各自的 Linux 发行版设置 Docker 存储库和安装 containerd.io 软件包的说明。

  2. 配置 containerd:

    1. sudo mkdir -p /etc/containerd
    2. containerd config default | sudo tee /etc/containerd/config.toml
  3. 重新启动 containerd:

    1. sudo systemctl restart containerd

启动 Powershell 会话,将 $Version 设置为所需的版本(例如:$ Version=1.4.3), 然后运行以下命令:

  1. 下载 containerd:

    1. curl.exe -L https://github.com/containerd/containerd/releases/download/v$Version/containerd-$Version-windows-amd64.tar.gz -o containerd-windows-amd64.tar.gz
    2. tar.exe xvf .\containerd-windows-amd64.tar.gz
  2. 提取并配置:

    1. Copy-Item -Path ".\bin\" -Destination "$Env:ProgramFiles\containerd" -Recurse -Force
    2. cd $Env:ProgramFiles\containerd\
    3. .\containerd.exe config default | Out-File config.toml -Encoding ascii
    4. # 检查配置。根据你的配置,可能需要调整:
    5. # - sandbox_image (Kubernetes pause 镜像)
    6. # - cni bin_dir 和 conf_dir 位置
    7. Get-Content config.toml
    8. # (可选 - 不过强烈建议) 禁止 Windows Defender 扫描 containerd
    9. Add-MpPreference -ExclusionProcess "$Env:ProgramFiles\containerd\containerd.exe"
  3. 启动 containerd:

    1. .\containerd.exe --register-service
    2. Start-Service containerd

使用 systemd cgroup 驱动程序

结合 runc 使用 systemd cgroup 驱动,在 /etc/containerd/config.toml 中设置

  1. [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
  2. ...
  3. [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
  4. SystemdCgroup = true

如果您应用此更改,请确保再次重新启动 containerd:

  1. sudo systemctl restart containerd

当使用 kubeadm 时,请手动配置 kubelet 的 cgroup 驱动.

CRI-O

本节包含安装 CRI-O 作为容器运行时的必要步骤。

使用以下命令在系统中安装 CRI-O:

说明:

CRI-O 的主要以及次要版本必须与 Kubernetes 的主要和次要版本相匹配。 更多信息请查阅 CRI-O 兼容性列表

安装并配置前置环境:

  1. # 创建 .conf 文件以在启动时加载模块
  2. cat <<EOF | sudo tee /etc/modules-load.d/crio.conf
  3. overlay
  4. br_netfilter
  5. EOF
  6. sudo modprobe overlay
  7. sudo modprobe br_netfilter
  8. # 配置 sysctl 参数,这些配置在重启之后仍然起作用
  9. cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
  10. net.bridge.bridge-nf-call-iptables = 1
  11. net.ipv4.ip_forward = 1
  12. net.bridge.bridge-nf-call-ip6tables = 1
  13. EOF
  14. sudo sysctl --system

在下列操作系统上安装 CRI-O, 使用下表中合适的值设置环境变量 OS:

操作系统$OS
Debian UnstableDebian_Unstable
Debian TestingDebian_Testing

然后,将 $VERSION 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 例如,如果你要安装 CRI-O 1.20, 请设置 VERSION=1.20. 你也可以安装一个特定的发行版本。 例如要安装 1.20.0 版本,设置 VERSION=1.20.0:1.20.0.

然后执行

  1. cat <<EOF | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
  2. deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /
  3. EOF
  4. cat <<EOF | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.list
  5. deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/ /
  6. EOF
  7. curl -L https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$VERSION/$OS/Release.key | sudo apt-key --keyring /etc/apt/trusted.gpg.d/libcontainers.gpg add -
  8. curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | sudo apt-key --keyring /etc/apt/trusted.gpg.d/libcontainers.gpg add -
  9. sudo apt-get update
  10. sudo apt-get install cri-o cri-o-runc

在下列操作系统上安装 CRI-O, 使用下表中合适的值设置环境变量 OS:

操作系统$OS
Ubuntu 20.04xUbuntu_20.04
Ubuntu 19.10xUbuntu_19.10
Ubuntu 19.04xUbuntu_19.04
Ubuntu 18.04xUbuntu_18.04

然后,将 $VERSION 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 例如,如果你要安装 CRI-O 1.20, 请设置 VERSION=1.20. 你也可以安装一个特定的发行版本。 例如要安装 1.20.0 版本,设置 VERSION=1.20:1.20.0.

然后执行

  1. cat <<EOF | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
  2. deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /
  3. EOF
  4. cat <<EOF | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.list
  5. deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/ /
  6. EOF
  7. curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | sudo apt-key --keyring /etc/apt/trusted.gpg.d/libcontainers.gpg add -
  8. curl -L https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$VERSION/$OS/Release.key | sudo apt-key --keyring /etc/apt/trusted.gpg.d/libcontainers-cri-o.gpg add -
  9. sudo apt-get update
  10. sudo apt-get install cri-o cri-o-runc

在下列操作系统上安装 CRI-O, 使用下表中合适的值设置环境变量 OS:

操作系统$OS
Centos 8CentOS_8
Centos 8 StreamCentOS_8_Stream
Centos 7CentOS_7

然后,将 $VERSION 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 例如,如果你要安装 CRI-O 1.20, 请设置 VERSION=1.20. 你也可以安装一个特定的发行版本。 例如要安装 1.20.0 版本,设置 VERSION=1.20:1.20.0.

然后执行

  1. 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
  2. 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
  3. sudo yum install cri-o
  1. sudo zypper install cri-o

$VERSION 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 例如,如果要安装 CRI-O 1.20,请设置 VERSION=1.20。 你可以用下列命令查找可用的版本:

  1. sudo dnf module list cri-o

CRI-O 不支持在 Fedora 上固定到特定的版本。

然后执行

  1. sudo dnf module enable cri-o:$VERSION
  2. sudo dnf install cri-o --now

启动 CRI-O:

  1. sudo systemctl daemon-reload
  2. sudo systemctl enable crio --no

参阅CRI-O 安装指南 了解进一步的详细信息。

cgroup 驱动

默认情况下,CRI-O 使用 systemd cgroup 驱动程序。要切换到 cgroupfs 驱动程序,或者编辑 / etc / crio / crio.conf 或放置一个插件 在 /etc/crio/crio.conf.d/02-cgroup-manager.conf 中的配置,例如:

  1. [crio.runtime]
  2. conmon_cgroup = "pod"
  3. cgroup_manager = "cgroupfs"

另请注意更改后的 conmon_cgroup,将 CRI-O 与 cgroupfs 一起使用时, 必须将其设置为 pod。通常有必要保持 kubelet 的 cgroup 驱动程序配置 (通常透过 kubeadm 完成)和 CRI-O 一致。

Docker

  1. 在每个节点上,根据安装 Docker 引擎 为你的 Linux 发行版安装 Docker。 你可以在此文件中找到最新的经过验证的 Docker 版本 依赖关系

  2. 配置 Docker 守护程序,尤其是使用 systemd 来管理容器的 cgroup。

    1. sudo mkdir /etc/docker
    2. cat <<EOF | sudo tee /etc/docker/daemon.json
    3. {
    4. "exec-opts": ["native.cgroupdriver=systemd"],
    5. "log-driver": "json-file",
    6. "log-opts": {
    7. "max-size": "100m"
    8. },
    9. "storage-driver": "overlay2"
    10. }
    11. EOF

    说明:

    对于运行 Linux 内核版本 4.0 或更高版本,或使用 3.10.0-51 及更高版本的 RHEL 或 CentOS 的系统,overlay2是首选的存储驱动程序。

  3. 重新启动 Docker 并在启动时启用:

    1. sudo systemctl enable docker
    2. sudo systemctl daemon-reload
    3. sudo systemctl restart docker

说明:

有关更多信息,请参阅