--- reviewers: - vincepri - bart0sh title: 容器运行时 content_type: concept weight: 10 --- {{< feature-state for_k8s_version="v1.6" state="stable" >}} Kubernetes 使用容器运行时来实现在 pod 中运行容器。 这是各种运行时的安装说明。 {{< caution >}} 我们发现 runc 在运行容器,处理系统文件描述符时存在一个漏洞。 恶意容器可以利用此漏洞覆盖 runc 二进制文件的内容,并以此在主机系统的容器上运行任意的命令。 请参考此链接以获取有关此问题的更多信息 [cve-2019-5736: runc vulnerability](https://access.redhat.com/security/cve/cve-2019-5736) {{< /caution >}} ### 适用性 {{< note >}} 本文档是为在 Linux 上安装 CRI 的用户编写的。 对于其他操作系统,请查找特定于您平台的文档。 {{< /note >}} 您应该以 `root` 身份执行本指南中的所有命令。 例如,使用 `sudo` 前缀命令,或者成为 `root` 并以该用户身份运行命令。 ### Cgroup 驱动程序 当某个 Linux 系统发行版使用 systemd 作为其初始化系统时,初始化进程会生成并使用一个 root 控制组 (`cgroup`),并充当 cgroup 管理器。 systemd 与 cgroup 集成紧密,并将为每个进程分配 cgroup。 您也可以配置容器运行时和 kubelet 使用 `cgroupfs`。 连同 systemd 一起使用 `cgroupfs` 意味着将有两个不同的 cgroup 管理器。 控制组用来约束分配给进程的资源。 单个 cgroup 管理器将简化分配资源的视图,并且默认情况下将对可用资源和使用中的资源具有更一致的视图。 当有两个管理器时,最终将对这些资源产生两种视图。 在此领域我们已经看到案例,某些节点配置让 kubelet 和 docker 使用 `cgroupfs`,而节点上运行的其余进程则使用 systemd;这类节点在资源压力下会变得不稳定。 更改设置,令容器运行时和 kubelet 使用 `systemd` 作为 cgroup 驱动,以此使系统更为稳定。 请注意在 docker 下设置 `native.cgroupdriver=systemd` 选项。 {{< caution >}} 强烈建议不要更改已加入集群的节点的 cgroup 驱动。 如果 kubelet 已经使用某 cgroup 驱动的语义创建了 pod,尝试更改运行时以使用别的 cgroup 驱动,为现有 Pods 重新创建 PodSandbox 时会产生错误。 重启 kubelet 也可能无法解决此类问题。 推荐将工作负载逐出节点,之后将节点从集群中删除并重新加入。 {{< /caution >}} ## Docker 在您的每台机器上安装 Docker。 推荐安装 19.03.11 版本,但是 1.13.1、17.03、17.06、17.09、18.06 和 18.09 版本也是可以的。 请跟踪 Kubernetes 发行说明中经过验证的 Docker 最新版本变化。 使用以下命令在您的系统上安装 Docker: {{< tabs name="tab-cri-docker-installation" >}} {{% tab name="Ubuntu 16.04+" %}} ```shell # 安装 Docker CE ## 设置仓库 ### 安装软件包以允许 apt 通过 HTTPS 使用存储库 apt-get update && apt-get install \ apt-transport-https ca-certificates curl software-properties-common ``` ```shell ### 新增 Docker 的 官方 GPG 秘钥 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - ``` ```shell ### 添加 Docker apt 仓库 add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" ``` ```shell ## 安装 Docker CE apt-get update && apt-get install -y\ containerd.io=1.2.13-2 \ docker-ce=5:19.03.11~3-0~ubuntu-$(lsb_release -cs) \ docker-ce-cli=5:19.03.11~3-0~ubuntu-$(lsb_release -cs) ``` ```shell # 设置 daemon cat > /etc/docker/daemon.json < ```shell # 重启 docker. systemctl daemon-reload systemctl restart docker ``` {{% /tab %}} {{% tab name="CentOS/RHEL 7.4+" %}} ```shell # 安装 Docker CE ## 设置仓库 ### 安装所需包 yum install yum-utils device-mapper-persistent-data lvm2 ``` ```shell ### 新增 Docker 仓库。 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo ``` ```shell ## 安装 Docker CE. yum update -y && yum install -y \ containerd.io-1.2.13 \ docker-ce-19.03.11 \ docker-ce-cli-19.03.11 ``` ```shell ## 创建 /etc/docker 目录。 mkdir /etc/docker ``` ```shell # 设置 daemon。 cat > /etc/docker/daemon.json < ```shell # 重启 Docker systemctl daemon-reload systemctl restart docker ``` {{% /tab %}} {{% /tabs %}} 如果你想开机即启动 docker 服务,执行以下命令: ```shell sudo systemctl enable docker ``` 请参阅[官方 Docker 安装指南](https://docs.docker.com/engine/installation/) 来获取更多的信息。 ## CRI-O 本节包含安装 `CRI-O` 作为 CRI 运行时的必要步骤。 使用以下命令在系统中安装 CRI-O: ### 准备环境 ```shell modprobe overlay modprobe br_netfilter # 设置必需的sysctl参数,这些参数在重新启动后仍然存在。 cat > /etc/sysctl.d/99-kubernetes-cri.conf <}} {{% tab name="Debian" %}} ```shell # Debian Unstable/Sid echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/Debian_Unstable/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/Debian_Unstable/Release.key -O- | sudo apt-key add - ``` ```shell # Debian Testing echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/Debian_Testing/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/Debian_Testing/Release.key -O- | sudo apt-key add - ``` ```shell # Debian 10 echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/Debian_10/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/Debian_10/Release.key -O- | sudo apt-key add - ``` ```shell # Raspbian 10 echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/Raspbian_10/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/Raspbian_10/Release.key -O- | sudo apt-key add - ``` 随后安装 CRI-O: ```shell sudo apt-get install cri-o-1.17 ``` {{% /tab %}} {{% tab name="Ubuntu 18.04, 19.04 and 19.10" %}} ```shell # 配置仓库 . /etc/os-release sudo sh -c "echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/x${NAME}_${VERSION_ID}/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list" wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/x${NAME}_${VERSION_ID}/Release.key -O- | sudo apt-key add - sudo apt-get update ``` ```shell # 安装 CRI-O sudo apt-get install cri-o-1.17 ``` {{% /tab %}} {{% tab name="Ubuntu 16.04" %}} ```shell # 安装必备软件 apt-get update apt-get install software-properties-common add-apt-repository ppa:projectatomic/ppa apt-get update # 安装 CRI-O apt-get install cri-o-1.15 ``` {{% /tab %}} {{% tab name="CentOS" %}} 要在以下操作系统上安装,请将环境变量 `$OS` 设置为下表中的相应字段: | 操作系统 | $OS | | ---------------- | ----------------- | | Centos 8 | `CentOS_8` | | Centos 8 Stream | `CentOS_8_Stream` | | Centos 7 | `CentOS_7` |
然后将 `$VERSION` 设置为与你的 Kubernetes 相匹配的 CRI-O 版本。 例如,如果要安装 CRI-O 1.18,请设置 `VERSION=1.18`。 你也可以安装特定版本,例如 1.18.3,请设置 `VERSION=1.18:1.18.3`。
确保声明变量后,使用下面命令安装 ```shell 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 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 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.18,请设置 `VERSION=1.18`。 你可以用下列命令查找可用的版本: ```shell dnf module list cri-o ``` CRI-O 不支持在 Fedora 上固定到特定的版本。 运行下列命令安装 ```shell dnf module enable cri-o:$VERSION dnf install cri-o ``` {{% /tab %}} {{< /tabs >}} ### 启动 CRI-O ``` systemctl start crio ``` 请参阅 [CRI-O 安装指南](https://github.com/kubernetes-sigs/cri-o#getting-started) 来获取更多的信息。 ## containerd 本节包含使用 `containerd` 作为 CRI 运行时的必要步骤。 使用以下命令在系统上安装容器: ### 准备环境 ```shell cat > /etc/modules-load.d/containerd.conf < /etc/sysctl.d/99-kubernetes-cri.conf < ### 安装 containerd {{< tabs name="tab-cri-containerd-installation" >}} {{% tab name="Ubuntu 16.04" %}} ```shell # 安装 containerd ## 设置仓库 ### 安装软件包以允许 apt 通过 HTTPS 使用存储库 apt-get update && apt-get install -y apt-transport-https ca-certificates curl software-properties-common ``` ```shell ### 安装 Docker 的官方 GPG 密钥 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - ``` ```shell ### 新增 Docker apt 仓库。 add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" ``` ```shell ## 安装 containerd apt-get update && apt-get install -y containerd.io ``` ```shell # 配置 containerd mkdir -p /etc/containerd containerd config default > /etc/containerd/config.toml ``` ```shell # 重启 containerd systemctl restart containerd ``` {{< /tab >}} {{% tab name="CentOS/RHEL 7.4+" %}} ```shell # 安装 containerd ## 设置仓库 ### 安装所需包 yum install yum-utils device-mapper-persistent-data lvm2 ``` ```shell ### 新增 Docker 仓库 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo ``` ```shell ## 安装 containerd yum update && yum install containerd.io ``` ```shell # 配置 containerd mkdir -p /etc/containerd containerd config default > /etc/containerd/config.toml ``` ```shell # 重启 containerd systemctl restart containerd ``` {{% /tab %}} {{< /tabs >}} ### systemd 使用 `systemd` cgroup 驱动,在 `/etc/containerd/config.toml` 中设置 ``` [plugins.cri] systemd_cgroup = true ``` 当使用 kubeadm 时,请手动配置 [kubelet 的 cgroup 驱动](/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#configure-cgroup-driver-used-by-kubelet-on-master-node) ## 其他的 CRI 运行时:frakti 请参阅 [Frakti 快速开始指南](https://github.com/kubernetes/frakti#quickstart) 来获取更多的信息。