Kubernetes 最小化微服务漏洞 gVisor与Containerd集成

已经测试过的应用和工具: https://gvisor.dev/docs/user_guide/compatibility/

这里可以看到现有的哪些应用可以使用gvisor,因为官方已经给你测试了。除此之外不能够百分之百去使用。除此之外还有哪些工具是可以使用的。

安全沙箱运行容器:gVisor与Containerd集成


 由docker切换到containerd容器引擎(安装conatinerd------>kubelet安装containerd----->最后验证)

在切换之前先将docker给停止了

[root@k8s-node1 ~]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket
[root@k8s-node1 ~]# pkill docker
[root@k8s-node1 ~]# ps -ef | grep docker
root     129602 126435  0 03:13 pts/0    00:00:00 grep --color=auto docker
1、准备配置
[root@k8s-node1 ~]# cat > /etc/sysctl.d/99-kubernetes-cri.conf << EOF
> net.bridge.bridge-nf-call-iptables = 1
> net.ipv4.ip_forward = 1
> net.bridge.bridge-nf-call-ip6tables = 1
> EOF


[root@k8s-node1 ~]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
* Applying /usr/lib/sysctl.d/50-default.conf ...
2、安装
[root@k8s-node1 ~]# cd /etc/yum.repos.d
[root@k8s-node1 yum.repos.d]# yum install -y containerd.io

[root@k8s-node1 yum.repos.d]# wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
--2021-07-15 03:19:27--  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 115.223.27.223, 115.238.192.241, 115.238.192.242, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|115.223.27.223|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2081 (2.0K) [application/octet-stream]
Saving to: ?.ocker-ce.repo.1?

100%[===========================================================================================>] 2,081       --.-K/s   in 0s      

2021-07-15 03:19:28 (185 MB/s) - ?.ocker-ce.repo.1?.saved [2081/2081]

#生成配置文件
[root@k8s-node1 yum.repos.d]# cd /etc/containerd/
[root@k8s-node1 containerd]# ls
config.toml
[root@k8s-node1 containerd]# containerd config default > config.toml 
3、修改配置文件
• pause镜像地址(负责pod网络的镜像,需要修改地址,要不然下载不了)
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.2"

• Cgroup驱动改为systemd

          [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
             SystemdCgroup = true
• 增加runsc容器运行时
      [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runsc]
          runtime_type = "io.containerd.runsc.v1"
      [plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
        [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
          runtime_type = "io.containerd.runc.v2"
          runtime_engine = ""
          runtime_root = ""
• 配置docker镜像加速器
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://b9pmyelo.mirror.aliyuncs.com"]
[root@k8s-node1 containerd]# systemctl restart containerd
[root@k8s-node1 containerd]# ps -ef | grep containerd
root       2887      1  1 03:41 ?        00:00:00 /usr/bin/containerd

 4、配置kubelet使用containerd

这个文件是kubelet为用户提供扩展参数的一个地方,在这里可以加入扩展参数,kubelet就可以去使用了。

=--container-runtime=remote  指定容器运行时为远程,默认支持的是docker,在后续版本对docker支持启用 
--container-runtime-endpoint=unix:///run/containerd/containerd.sock 
--cgroup-driver=systemd

docker有docker的管理命令,containerd有它自己的管理命令,

containerd也有 ctr 管理工具,但功能比较简单,一般使用crictl工具检查和调试容器。
项目地址:https://github.com/kubernetes-sigs/cri-tools/
准备crictl连接containerd配置文件:
cat > /etc/crictl.yaml << EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
EOF

下面是docker与crictl命令对照表:

RuntimeClass 是一个用于选择容器运行时配置的特性,容器运行时配置用于运行 Pod 中的容器。
创建RuntimeClass:
apiVersion: node.k8s.io/v1 # RuntimeClass 定义于 node.k8s.io API 组
kind: RuntimeClass
metadata:
  name: gvisor # 用来引用 RuntimeClass 的名字
handler: runsc # 对应的 CRI 配置的名称


root@k8s-master:~# kubectl apply -f cks_runtimeclass.yaml 
runtimeclass.node.k8s.io/gvisor created
root@k8s-master:~# kubectl get runtimeclass
NAME     HANDLER   AGE
gvisor   runsc     14s

 

 现在已经完成了安全沙箱运行一个容器,在linux内核和容器之间做了隔离,但是这里有个问题,除了gvisor官网提供的哪些工具,可能有些工具是不能使用的。还有如果容器需要特权模式该怎么办呢?

gvisor是防止linux容器去直接调用内核的能力,特权模式是放开了对Linux内核的访问。所以gvisor是不允许特权模式的,使用了强隔离。如果有些容器使用了特权模式呢?

所以多容器运行时应用而生。RuntimeClass 是一个用于选择容器运行时配置的特性,容器运行时配置用于运行 Pod 中的容器。 通过runtimeclass可以配置不同容器引擎的容器运行时。让你pod在工作的时候,根据指定的容器运行时,让你的容器引擎选择去启动,来去满足不太同的需求。

如果有些容器需要使用特权模式可以使用runc,如果要使用沙箱机制那么就可以使用runsc。那么就可以指定rumtimeclass。所以可以根据你的需求使用不同的容器运行时。

所以企业使用的话是runc+runsc的组合。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值