摘要:[plugins.cri.containerd.runtimes.kata-qemu.options]。[plugins.cri.containerd.runtimes.kata-qemu]。

前 言

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-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輔助其他軟件給用戶提供輕量級的交付體驗,開箱即用的使用感受。

相關文章