容器运行时

目前k8s支持的几个常用的运行时

v1.24 之前的 Kubernetes 版本直接集成了 Docker Engine 的一个组件,名为 dockershim。 这种特殊的直接整合不再是 Kubernetes 的一部分

Docker运行时

早起k8s 版本使用 容器技术中的docker 作为运行时,后将docker的运行时改名为dockershim。(shim 译为垫片)将dockershim作为运行时的临时解决方案。

在 Kubernetes 1.22 版之前,它不会被彻底移除,dockershim 被移除的最早版本会是 2021 年底发布的 1.23 版。 更新:dockershim 计划在 Kubernetes 1.24 版被移除。

参考阅读:移除 Dockershim 这一 Kubernetes 增强方案

取而代之的是符合容器运行时接口标准(CRI -Container Runtime Interface )的运行时技术。

可以查看 containerd 和 CRI-O 的使用者列表, 这两个容器运行时是云原生基金会(CNCF)下的项目。

CRI 的API定义主要包含了两个RPC服务,分别是RuntimeService和ImageService。

RuntimeService:主要用来管理Pod和容器的生命周期,以及容器交互的调用等等

ImageService:主要用于拉取镜像,查看和删除镜像等等

切换容器运行时CRI 的时候需要注意哪些?

  • 日志配置
  • 运行时的资源限制
  • 直接访问 docker 命令或通过控制套接字调用 Docker 的节点供应脚本
  • 需要访问 docker 命令或控制套接字的 kubectl 插件
  • 需要直接访问 Docker 的 Kubernetes 工具(例如:kube-imagepuller)
  • 配置像 registry-mirrors 和不安全的镜像仓库等功能
  • 需要 Docker 保持可用、且运行在 Kubernetes 之外的,其他支持脚本或守护进程(例如:监视或安全代理)
  • GPU 或特殊硬件,以及它们如何与你的运行时和 Kubernetes 集成

同时,如果你用了自定义了 dockerd 配置,则可能需要为新容器运行时做一些适配工作。

镜像的操作命令可以使用 crictl 工具,临时替代docker 工具指令

crictl 的输出格式类似于 docker 命令行,只是对于某些命令而言会有部分列缺失。

docker 命令行的 v1.40 版本和 crictl 的 v1.19.0 版本映射详情参阅:从 Docker 命令行映射到 crictl

仅被 crictl 支持的命令

crictl 描述
imagefsinfo 返回镜像文件系统信息
inspectp 显示一个或多个 Pod 的状态
port-forward 将本地端口转发到 Pod
pods 列举 Pod
runp 运行一个新的 Pod
rmp 删除一个或多个 Pod
stopp 停止一个或多个运行中的 Pod

Containerd运行时

转发 IPv4 并让 iptables 看到桥接流量

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# 设置所需的 sysctl 参数,参数在重新启动后保持不变
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# 应用 sysctl 参数而不重新启动
sudo sysctl --system

以上配置如果没有设置,在k8s 初始化执行kubeadm init指令时会报错

可通过指令方式,临时生效

echo 1 > /proc/sys/net/ipv4/ip_forward	
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables

如果报错:/proc/sys/net/bridge/bridge-nf-call-iptables: No such file or directory,可能需要执行如下命令,生成相应的文件

modprobe br_netfilter

image-20230716194056426

永久生效

[root@master ~]# touch /etc/sysconfig/modules/iptables.modules
[root@master ~]# vim /etc/sysconfig/modules/iptables.modules
modprobe br_netfilter
[root@master ~]# chmod 755 /etc/sysconfig/modules/iptables.modules
[root@master ~]# sh /etc/sysconfig/modules/iptables.modules

通过运行以下指令确认 br_netfilteroverlay 模块被加载:

lsmod | grep br_netfilter
lsmod | grep overlay

image-20230716194142256

通过运行以下指令确认 net.bridge.bridge-nf-call-iptablesnet.bridge.bridge-nf-call-ip6tablesnet.ipv4.ip_forward 系统变量在你的 sysctl 配置中被设置为 1:

sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward

image-20230716194849504

CRI 版本支持

你的容器运行时必须至少支持 v1alpha2 版本的容器运行时接口。

Kubernetes 从 1.26 版本开始仅适用于 v1 版本的容器运行时(CRI)API。早期版本默认为 v1 版本, 但是如果容器运行时不支持 v1 版本的 API, 则 kubelet 会回退到使用(已弃用的)v1alpha2 版本的 API。

Containerd安装

参阅:https://github.com/containerd/containerd/blob/main/docs/getting-started.md

如果想使用systemd管理containerd,需要下载 containerd.service 文件到/usr/lib/systemd/system/containerd.service

一般安装完containerd后,自动生成此文件,无需单独下载。如果启动报错,可参考如下配置进行排查问题!

[root@master ~]# vim /usr/lib/systemd/system/containerd.service
# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target

[Service]
#uncomment to enable the experimental sbservice (sandboxed) version of containerd/cri integration
#Environment="ENABLE_CRI_SANDBOXES=sandboxed"
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target

image-20230716195935025

之后执行下面指令

systemctl daemon-reload
systemctl enable --now containerd

CRI套接字路径设置

在 Linux 上,containerd 的默认 CRI 套接字是 /run/containerd/containerd.sock

在 Windows 上,默认 CRI 端点是 npipe://./pipe/containerd-containerd

配置 systemd cgroup 驱动

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

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

如果你使用 cgroup v2,则推荐 systemd cgroup 驱动。

说明:

如果从软件包(例如,RPM 或者 .deb)中安装 containerd,你可能会发现其中默认禁止了 CRI 集成插件。

需要启用 CRI 支持才能在 Kubernetes 集群中使用 containerd。 要确保 cri 没有出现在 /etc/containerd/config.toml 文件中 disabled_plugins 列表内。如果你更改了这个文件,也请记得要重启 containerd

如果你在初次安装集群后或安装 CNI 后遇到容器崩溃循环,则随软件包提供的 containerd 配置可能包含不兼容的配置参数。考虑按照 getting-started.md 中指定的 containerd config default > /etc/containerd/config.toml 重置 containerd 配置,然后相应地设置上述配置参数。

image-20230716200657140

更改yaml配置后,仍然需要重启containerd

sudo systemctl restart containerd

重载沙箱镜像

在containerd配置中,设置如阿里云镜像,修改为离本地近的快速镜像站

[plugins."io.containerd.grpc.v1.cri"]
  sandbox_image = "registry.k8s.io/pause:3.2"

image-20230716201103106

CRI-O运行时

CRI-O运行时的配置,参阅CRI-O

Mirantis 容器运行时

Mirantis Container Runtime (MCR) 是一种商用容器运行时,以前称为 Docker 企业版。 你可以使用 MCR 中包含的开源 cri-dockerd 组件将 Mirantis Container Runtime 与 Kubernetes 一起使用。

参阅:MCR

猜你喜欢

转载自blog.csdn.net/baidu_33864675/article/details/131754385