k3s 卸载_手把手实操教程!使用k3s运行轻量级VM

前 言k3s作为轻量级的Kubernetes发行版,运行容器是基本功能。VM的管理原本是IaaS平台的基本能力,随着Kubernetes的不断发展,VM也可以纳入其管理体系。结合Container和VM的各自优势,又发展出轻量级VM的理念,兼具容器的轻量特性,又有VM的隔离安全性,这其中kata-container是时下比较受关注的开源项目。那么轻量级的k3s和轻量级VM碰撞,又有怎样的效果,结合两者进行实践,谨以此文进行说明。

请注意,本文部署时使用的主要软件版本是:

k8s v1.17.2+k3s.1,kata-containers v1.9.3。

环境准备kata-containers既可以在纯Docker上运行,也可以运行在Kubernetes中,但本文只关注Kubernetes场景。kubelet本身可以支持多种满足CRI接口的runtime,包括CRI-O和CRI-Containerd,Kubernetes的RuntimeClaas可以配置Pod使用那种运行时,创建容器时对应的runtime调用runc创建容器,创建VM时调用kata-runtime创建VM。kata-runtime支持多种形式的轻量VM,kata-qemu是默认支持选项,另外也可以使用firecracker(另一种MicroVM)。整体调用关系如下图所示:

无论哪种VM,KVM的支持是需要的,现在主流的MicroVM技术都是基于KVM,所以我们的环境需要准备支持KVM的Host。你可以用Bare Metal,也可以用支持KVM的PC server,还可以用嵌套虚拟化方式。Host开启KVM支持后,可以用以下命令检查:

检测虚拟化情况(需要>0)grep -cw vmx /proc/cpuinfo

安装k3s和kata-containersk3s的安装部署非常简单,直接参考官方文档即可:

https://rancher.com/docs/k3s/l ... tion/

笔者使用GCP的Host,并开启嵌套虚拟化,所以采用在线脚本安装方式:

curl -sfL https://get.k3s.io | sh -

备份 containerd原始配置,后面会用到cd /var/lib/rancher/k3s/agent/etc/containerd/

cp config.toml config.toml.base

kata-containers部署有些需要注意的地方,kata-deploy(https://github.com/kata-contai ... eploy)是官方维护的部署脚本,可以帮助部署kata-containers。但是,kata-deploy的k3s支持有些问题,尽管已经有PR提交声明支持了k3s,笔者整理了这一部署过程遇到的问题,仅作参考。

首先设置RBAC,先安装文档中默认的RBAC yaml,再做一些修改:

kubectl apply -f https://raw.githubusercontent.com/kata-containers/packaging/master/kata-deploy/kata-rbac/base/kata-rbac.yaml

编辑 clusterrole node-labeler,添加新的api授权

新增coordination.k8s.io的leases授权kubectl edit clusterrole node-labeler

...

...

rules:

- apiGroups:

- ""

resources:

- nodes

verbs:

- get

- patch

- apiGroups:

- coordination.k8s.io

resources:

- leases

verbs:

- get

- list

部署kata,依然是先安装文档中默认的yaml:

kubectl apply -k github.com/kata-containers/packaging/kata-deploy/kata-deploy/overlays/k3s

查看kata-deploy Pod的运行情况,基本上你肯定会看到出错信息:

crictl ps -a | grep kube-kata

crictl logs 

...

...

Failed to restart containerd.service: Unit containerd.service not found.

这时候发现containerd虽然配置完成,但是重启失败。k3s目前的containerd是内置在k3s管理的,并不是通过systemd,而kata-deploy无法识别,这需要我们手动来完成这个过程:

创建/var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl

使用之前保留的原始配置cat config.toml.base > config.toml.tmpl

在config.toml.tmpl末尾追加

你可以筛选加入你使用的runtimes,也可以全部添加[plugins.cri.containerd.runtimes.kata]

runtime_type = "io.containerd.kata.v2"

[plugins.cri.containerd.runtimes.kata.options]

ConfigPath = "/opt/kata/share/defaults/kata-containers/configuration.toml"

[plugins.cri.containerd.runtimes.kata-fc]

runtime_type = "io.containerd.kata-fc.v2"

[plugins.cri.containerd.runtimes.kata-fc.options]

ConfigPath = "/opt/kata/share/defaults/kata-containers/configuration-fc.toml"

[plugins.cri.containerd.runtimes.kata-qemu]

runtime_type = "io.containerd.kata-qemu.v2"

[plugins.cri.containerd.runtimes.kata-qemu.options]

ConfigPath = "/opt/kata/share/defaults/kata-containers/configuration-qemu.toml"

[plugins.cri.containerd.runtimes.kata-qemu-virtiofs]

runtime_type = "io.containerd.kata-qemu-virtiofs.v2"

[plugins.cri.containerd.runtimes.kata-qemu-virtiofs.options]

ConfigPath = "/opt/kata/share/defaults/kata-containers/configuration-qemu-virtiofs.toml"

[plugins.cri.containerd.runtimes.kata-nemu]

runtime_type = "io.containerd.kata-nemu.v2"

[plugins.cri.containerd.runtimes.kata-nemu.options]

ConfigPath = "/opt/kata/share/defaults/kata-containers/configuration-nemu.toml"

由于kata-deploy每次启动都会重写k3s containerd配置,为了避免干扰,在一切就绪后,我们可以删除kata-deploy,并重启k3s:

由于kata-deploy stop时会清理安装的kata相关程序,所以删除kata-deploy前,我们先去掉这个机制

编辑kata-deploy,删除lifecycle preStoplifecycle:

preStop:

exec:

command:

- bash

- -c

- /opt/kata-artifacts/scripts/kata-deploy.sh cleanup

kubectl delete -k github.com/kata-containers/packaging/kata-deploy/kata-deploy/overlays/k3s

systemctl restart k3s.service

运行demo添加RuntimeClass,目前经过笔者测试,k3s只支持kata-qemu,所以我们只安装kata-qemu-runtimeClass:

kubectl apply -f https://raw.githubusercontent.com/kata-containers/packaging/master/kata-deploy/k8s-1.14/kata-qemu-runtimeClass.yaml

添加workloadkubectl apply -f https://raw.githubusercontent.com/kata-containers/packaging/master/kata-deploy/examples/test-deploy-kata-qemu.yaml

kubectl get deploy php-apache-kata-qemu

NAME                   READY   UP-TO-DATE   AVAILABLE   AGE

php-apache-kata-qemu   1/1     1            1           1m

确认kata-qemu正常创建VM:

ps aux| grep qemu

root      3589  0.9  0.9 2490176 151368 ?      Sl   06:49   0:15 /opt/kata/bin/qemu-system-x86_64

进入VMcrictl ps | grep php-apache

crictl exec -it  bash

uname -r4.19.75

exit

查看Host kerneluname -r

5.0.0-1029-gcp

我们可以看到通过k3s创建的每个kata容器都具有独立的内核,没有共享主机内核。

后 记随着k3s的进一步发展,越来越多的软件不仅仅支持完整的Kubernetes,也会支持在k3s中安装部署。k3s除了会在devops领域和边缘计算领域外不断发展外,作为软件运行载体也在不断被各个开源产品接受,k3s辅助其他软件给用户提供轻量级的交付体验,开箱即用的使用感受。


版权声明:本文为weixin_42613773原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。