标签(空格分隔):containerd 系列
一:Containerd Network管理
默认Containerd管理的容器仅有lo网络,无法访问容器之外的网络,可以为其添加网络插件,
使用容器可以连接外网。CNI(Container Network Interface)
创建CNI网络插件:
使用wget下载cni工具源码包
# wget https://github.com/containernetworking/cni/archive/refs/tags/v1.0.1.tar.gz
# tar -zxvf v1.0.1.tar.gz
# mv cni-1.0.1 /usr/local/cni
下载CNI 网络插件:
# wget https://github.com/containernetworking/plugins/releases/download/v1.0.1/cni-plugins-linux-amd64-v1.0.1.tgz
创建cni插件工具解压目录
# mkdir /home/cni-plugins
解压cni插件工具至上述创建的目录中
# tar xf cni-plugins-linux-amd64-v1.0.1.tgz -C /home/cni-plugins
# cd /home/cni-plugins/
# ls
二:准备CNI 的网络配置文件
创建名为mynet的网络,其中包含名为cni0的网桥
# vim /etc/cni/net.d/10-mynet.conf
# cat /etc/cni/net.d/10-mynet.conf
{
"cniVersion": "1.0.0",
"name": "mynet",
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"ipMasq": true,
"ipam": {
"type": "host-local",
"subnet": "10.88.0.0/16",
"routes": [
{ "dst": "0.0.0.0/0" }
]
}
}
vim /etc/cni/net.d/99-loopback.conf
----
{
"cniVerion": "1.0.0",
"name": "lo",
"type": "loopback"
}
-----
### 生成CNI网络
获取epel源
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安装jq
# yum -y install jq
# 执行脚本文件,基于/etc/cni/net.d/目录中的*.conf配置文件生成容器网络
# cd /usr/local/cni/script/
# CNI_PATH=/home/cni-plugins ./priv-net-run.sh echo "Hello World"
# ip addr
# 查看生成cni0 的网络
# ip route
# 查看cni的 路由表
三:为Containerd容器配置网络功能
3.1 创建一个容器
# ctr images pull docker.io/library/busybox:latest
# ctr run -d docker.io/library/busybox:latest busybox
# ctr container ls
CONTAINER IMAGE RUNTIME
busybox docker.io/library/busybox:latest io.containerd.runc.v2
# ctr tasks ls
TASK PID STATUS
busybox 15996 RUNNING
# ctr tasks exec --exec-id $RANDOM -t busybox sh
3.2获取容器进程ID及其网络命名空间
在宿主机中完成指定容器进程ID获取
# pid=$(ctr tasks ls | grep busybox | awk '{print $2}')
# echo $pid
15996
在宿主机中完成指定容器网络命名空间路径获取
# netnspath=/proc/$pid/ns/net
# echo $netnspath
/proc/15996/ns/net
为指定容器添加网络配置
cd /usr/local/cni/scripts/
CNI_PATH=/home/cni-plugins ./exec-plugins.sh add $pid $netnspath
进入容器确认是否添加网卡信息
# ctr tasks exec --exec-id $RANDOM -t busybox sh
在容器中开启httpd服务
/ # echo "containerd net web test" > /tmp/index.html
/ # httpd -h /tmp
/ # wget -O - -q 127.0.0.1
containerd net web test
/ # exit