KubeVirt的价值及使用

本文将从Kubevirt的价值,潜力,安装,架构,特性及如何使用两方面介绍。

KubeVirt价值

如果说kubevirt的价值是什么?那么就是要问为什么需要Kubevirt。

  1. kubevirt 技术试图解决在开发团队已经接受或者试图接受K8S思想时但存在有些基于虚拟化的工作负载难以实现容器化的情景。

  2. 更确切地说,KubeVirt提供了一个统一的开发平台,开发者在同一个共享环境中既能够对容器化的应用程序构建,修改,部署,也能对虚拟化的应用程序进行同样的操作。

  3. 这样做好处是广泛而显著的。依赖基于虚拟机工作负载的团队有能力迅速容器化应用。随着虚拟化工作负载直接放置在开发工作流程,随着时间的推移,团队仍可以对工作负载进行分解,同时可以无缝地利用其他虚拟化组件。

KubeVirt潜力

利用KubeVirt能做哪些事情?

  1. 利用KubeVirt和Kubernetes 管理不便于容器化且适合虚拟机的应用。
  2. 将已有的虚拟化工作负载与新容器化的工作负载相结合共存于同一个平台。
  3. 支持在容器中与已有的虚拟化应用有交互的新微服务应用的开发。

KubeVirt架构

在这里插入图片描述
重要组件

  1. virt-api
    (1) HTTP API Server作为所有涉及虚拟化相关的处理流程的入口(Entry Point),负责更新、验证VMI CRDs;
    (2) 提供RESTful API来管理集群中虚拟机,Kubevirt采用CRD的工作方式,virt-api提供自定义的API请求处理流程,如VNC、 Console、 Start/Stop虚拟机;
  2. virt-controller
    (1) 该控制器负责监控虚拟机实例VMI对象和管理集群中每个虚拟机实例VMI的状态以及与其相关的Pod;
    (2) VMI对象将在其生命周期内始终与容器关联,但是,由于VMI的迁移,容器实例可能会随时间变化。
  3. virt-handler
    (1) 在K8S的计算节点上,virt-handler运行于Pod中,作为DaemonSet;
    (2) 类似于virt-controller都是响应式的,virt-handler负责监控每个虚拟机实例的状态变化,一旦检测到状态变化就响应并确保相应操作能达到所需(理想)状态;
    (3) virt-handler负责以下几方面:保持集群级VMI Spec与相应libvirt域之间的同步;报告Libvirt域状态和集群Spec的变化;调用以节点为中心的插件以满足VMI Spec定义的网络和存储要求。
  4. virt-launcher
    (1) 每个虚拟机实例(VMI)对象都会对应一个Pod,该Pod中的基础容器中运行着Kubevirt核心组件virt-launcher;
    (2) K8S或者Kubelet是不负责运行VMI的运行的,取而代之的是,群集中每个节点上的守护进程会负责为每个Pod启动一个与VMI对象关联的VMI进程,无论何时在主机上对其进行调度。
    (3) virt-launcher Pod的主要功能是提供cgroups和名称空间并用于托管VMI进程。
    (4) virt-handler通过将VMI的CRD对象传递给virt-launcher来通知virt-launcher启动VMI。然后virt-launcher在其容器中使用本地libvirtd实例来启动VMI。从此开始,virt-launcher将监控VMI进程,并在VMI实例退出后终止;
    (5) 如果K8S的Runtime在VMI退出之前试图关闭virt-launcher Pod时,virt-launcher会将信号从K8S转发给VMI进程,并尝试推迟pod的终止,直到VMI成功关闭。
  5. Libvirtd
    (1) 每个VMI实例对应的Pod都会有一个libvirtd实例;
    (2) virt-launcher借助于libvirtd来管理VMI实例的生命周期;

KubeVirt安装与使用

Checking if nested virtualization is supported

For Intel Processors

# cat /sys/module/kvm_intel/parameters/nested
Y[1]

For AMD Processors

# $ cat /sys/module/kvm_amd/parameters/nested
Y[1]

If Value is not Y[1], do actions as below:
Intel Processor

# modprobe -r kvm_intel
# modprobe kvm_intel nested=1

AMD Processor

# modprobe -r kvm_amd
# modprobe kvm_amd nested=1

If you want to enable it permanently, as below:
Intel Processor

# echo 'options kvm_intel nested=1' >/etc/modprobe.d/kvm-nested.conf

AMD Processor

# echo 'options kvm_amd nested=1' >/etc/modprobe.d/kvm-nested.conf

Check for the Virtualization Extensions

If CPU supports virtualization extensions, Some messages will be output after executing the following command:

# egrep 'svm|vmx' /proc/cpuinfo

If not, create the ConfigMap so that KubeVirt uses emulation mode:

# kubectl create configmap kubevirt-config -n kubevirt --from-literal debug.useEmulation=true
# KubeVirt's Version
$ export KUBEVIRT_VERSION=v0.23.0
# creates KubeVirt operator
$ kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${KUBEVIRT_VERSION}/kubevirt-operator.yaml
# creates KubeVirt KV custom resource
$ kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${KUBEVIRT_VERSION}/kubevirt-cr.yaml

Download virtctl and Yamls related to KubeVirt:

(1) Create Kubevirt Directory and Set Kubevirt version ENV

# mkdir Kubevirt
# pushd Kubevirt
# export KUBEVIRT_VERSION="v0.23.0"

(2) Download KubeVirt VirtCtl and Install

# curl -L -o /usr/local/bin/virtctl https://github.com/kubevirt/kubevirt/releases/download/${KUBEVIRT_VERSION}/virtctl-${KUBEVIRT_VERSION}-linux-amd64
# chmod +x /usr/local/bin/virtctl
# virtctl --help 
virtctl controls virtual machine related operations on your kubernetes cluster.

Available Commands:
  console      Connect to a console of a virtual machine instance.
  expose       Expose a virtual machine instance, virtual machine, or virtual machine instance replica set as a new service.
  help         Help about any command
  image-upload Upload a VM image to a PersistentVolumeClaim.
  restart      Restart a virtual machine.
  start        Start a virtual machine.
  stop         Stop a virtual machine.
  version      Print the client and server version information.
  vnc          Open a vnc connection to a virtual machine instance.

Use "virtctl <command> --help" for more information about a given command.
Use "virtctl options" for a list of global command-line options (applies to all commands).

(3) Download Kubevirt Operator and Custom Resources yaml files

# wget https://github.com/kubevirt/kubevirt/releases/download/${KUBEVIRT_VERSION}/kubevirt-operator.yaml
# wget https://github.com/kubevirt/kubevirt/releases/download/${KUBEVIRT_VERSION}/kubevirt-cr.yaml

(4) Download testVM yaml

# wget https://raw.githubusercontent.com/kubevirt/kubevirt.github.io/master/labs/manifests/vm.yaml
# popd Kubevirt

(5) CDI Deploy

VERSION=v1.10.9
kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-operator.yaml
kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-operator-cr.yaml

(6) Deploy hostpath

mkdir -p /var/run/kubevirt/hostpath
kubectl create -f https://github.com/kubevirt/kubevirt/blob/master/docs/devel/hostpath-provisioner.yaml

Deploy KubeVirt Operator

# pushd Kubevirt
# kubectl create -f kubevirt-operator.yaml
# kubectl get pods -n kubevirt
NAME                             READY     STATUS              RESTARTS   AGE
virt-operator-7f589cd8cb-2fsm8   0/1       ContainerCreating   0          12s
...
virt-operator-7f589cd8cb-n9knm   1/1       Running   0         28s

Deploy KubeVirt

# kubectl create -f kubevirt-cr.yaml
# kubectl get pods -n kubevirt
NAME                               READY   STATUS    RESTARTS   AGE
virt-api-7dc455b79c-b7l8j          1/1     Running   1          31h
virt-api-7dc455b79c-p8sl5          1/1     Running   1          31h
virt-controller-76cccd9979-kr7sv   1/1     Running   3          31h
virt-controller-76cccd9979-q9cxk   1/1     Running   3          31h
virt-handler-k8df6                 1/1     Running   1          31h
virt-operator-7f589cd8cb-2fsm8     1/1     Running   2          2d23h
virt-operator-7f589cd8cb-n9knm     1/1     Running   5          2d23h

then the pod, svc status as below:

# kubectl get po -n kubevirt
NAME                               READY   STATUS    RESTARTS   AGE
virt-api-7dc455b79c-b7l8j          1/1     Running   0          60m
virt-api-7dc455b79c-p8sl5          1/1     Running   0          60m
virt-controller-76cccd9979-kr7sv   1/1     Running   0          57m
virt-controller-76cccd9979-q9cxk   1/1     Running   0          57m
virt-handler-k8df6                 1/1     Running   0          60m
virt-operator-7f589cd8cb-2fsm8     1/1     Running   1          40h
virt-operator-7f589cd8cb-n9knm     1/1     Running   1          40h
# kubectl get svc -n kubevirt
NAME                          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubevirt-prometheus-metrics   ClusterIP   10.96.32.24     <none>        443/TCP   62m
virt-api                      ClusterIP   10.108.69.243   <none>        443/TCP   62m

Deploy a VirtualMachine

# kubectl apply -f vm.yaml
# kubectl get vms
NAME     AGE   RUNNING   VOLUME
testvm   54m   false
# kubectl get vms -o yaml testvm

Note:
Field RUNNING is set “false”, that means we’ve only defined the object but it now needs to be instantiated but not be running,
We should start VM with virtctl:

# virtctl start testvm
VM testvm was scheduled to start
# kubectl get vms
NAME     AGE   RUNNING   VOLUME
testvm   59m   true
# kubectl get vmis
NAME     AGE   PHASE     IP               NODENAME
testvm   25s   Running   10.244.166.133   node1
# kubectl get vmis -o yaml testvm

Note:
(1) vmis stands for VirtualMachineInstance.
(2) Field PHASE indicates VMI’s progress transitioning from one state to the next till to RUNNING.

Using virtctl to connect to the VMI consoles interfaces, and EXIT with “Ctrl+]”

# virtctl console testvm
Successfully connected to testvm console. The escape sequence is ^]

login as 'cirros' user. default password: 'gocubsgo'. use 'sudo' for root.
testvm login:cirros
Password:gocubsgo
$ uname -msr
Linux 4.4.0-28-generic x86_64
$ 

VNC to Connet to VM, VNC requires remote-viewer from the virt-viewer package installed on the host.

# virtctl vnc testvm

Clean Up VM Instance

(1) Stop VM

# virtctl stop testvm
VM testvm was scheduled to stop

(2) Delete VM

# kubectl delete vm testvm
virtualmachine.kubevirt.io "testvm" deleted

Reference

台球猫头鹰的脚印
kubevirt官网
kubevirt以容器方式运行虚拟机

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐