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

栏目: IT技术 · 发布时间: 6年前

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

前 言

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)。整体调用关系如下图所示:

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

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

检测虚拟化情况(需要>0)

grep -cw vmx /proc/cpuinfo

安装k3s和kata-containers

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

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 <kube-kata-container-id>

...

...

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 preStop

lifecycle:

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

添加workload

kubectl 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

进入VM

crictl ps | grep php-apache

crictl exec -it <php-apache-container-id> bash

uname -r

4.19.75

exit

查看Host kernel

uname -r

5.0.0-1029-gcp

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

后 记

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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Rationality for Mortals

Rationality for Mortals

Gerd Gigerenzer / Oxford University Press, USA / 2008-05-02 / USD 65.00

Gerd Gigerenzer's influential work examines the rationality of individuals not from the perspective of logic or probability, but from the point of view of adaptation to the real world of human behavio......一起来看看 《Rationality for Mortals》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器