KubeVirt部署和使用
# KubeVirt部署和使用
# 安装 Kubevirt
# 🔍准备K8S集群
查看当前的K8S集群环境,确保两个节点有足够的性能支撑。当前这里使用的是1.21.1版本的集群。
[root@k8s-master-node1 ~]# kubectl cluster-info
Kubernetes control plane is running at https://apiserver.cluster.local:6443
CoreDNS is running at https://apiserver.cluster.local:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@k8s-master-node1 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master-node1 Ready control-plane,master,worker 2d1h v1.21.1
k8s-worker-node1 Ready worker 2d1h v1.21.1
# 🔍开启虚拟化
- 在安装 Kubevirt 之前,需要做一些准备工作。先安装 libvrt 和 qemu 软件包
- 查看节点是否支持 kvm 硬件辅助虚拟化
- 如果不支持,则先生成让 Kubevirt 使用软件虚拟化的配置
# Ubuntu
$ apt install -y qemu-kvm libvirt-bin bridge-utils virt-manager
# CentOS
$ yum install -y qemu-kvm libvirt virt-install bridge-utils
$ virt-host-validate qemu
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking if device /dev/kvm exists : PASS
QEMU: Checking if device /dev/kvm is accessible : PASS
QEMU: Checking if device /dev/vhost-net exists : PASS
QEMU: Checking if device /dev/net/tun exists : PASS
QEMU: Checking for cgroup 'memory' controller support : PASS
QEMU: Checking for cgroup 'memory' controller mount-point : PASS
QEMU: Checking for cgroup 'cpu' controller support : PASS
QEMU: Checking for cgroup 'cpu' controller mount-point : PASS
QEMU: Checking for cgroup 'cpuacct' controller support : PASS
QEMU: Checking for cgroup 'cpuacct' controller mount-point : PASS
QEMU: Checking for cgroup 'cpuset' controller support : PASS
QEMU: Checking for cgroup 'cpuset' controller mount-point : PASS
QEMU: Checking for cgroup 'devices' controller support : PASS
QEMU: Checking for cgroup 'devices' controller mount-point : PASS
QEMU: Checking for cgroup 'blkio' controller support : PASS
QEMU: Checking for cgroup 'blkio' controller mount-point : PASS
QEMU: Checking for device assignment IOMMU support : PASS
QEMU: Checking if IOMMU is enabled by kernel : PASS
不支持需要开启Kubevirt的虚拟化配置:
$ kubectl create namespace kubevirt
$ kubectl create configmap -n kubevirt kubevirt-config \
--from-literal debug.useEmulation=true
# 🔍部署kubevirt
部署最新版本的 Kubevirt,官方有提供对应的版本,如果你的k8s集群版本较低,可以切换相对应的版本。
$ export VERSION=$(curl -s https://api.github.com/repos/kubevirt/kubevirt/releases | grep tag_name | grep -v -- '-rc' | head -1 | awk -F': ' '{print $2}' | sed 's/,//' | xargs)
$ kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-operator.yaml
$ kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-cr.yaml
查看部署结果:
$ kubectl -n kubevirt get pod
NAME READY STATUS RESTARTS AGE
virt-api-64999f7bf5-n9kcl 1/1 Running 0 6d
virt-api-64999f7bf5-st5qv 1/1 Running 0 6d8h
virt-controller-8696ccdf44-v5wnq 1/1 Running 0 6d
virt-controller-8696ccdf44-vjvsw 1/1 Running 0 6d8h
virt-handler-85rdn 1/1 Running 3 7d19h
virt-handler-bpgzp 1/1 Running 21 7d19h
virt-handler-d55c7 1/1 Running 1 7d19h
virt-operator-78fbcdfdf4-sf5dv 1/1 Running 0 6d8h
virt-operator-78fbcdfdf4-zf9qr 1/1 Running 0 6d
# 🔍部署 CDI
Containerized Data Importer(CDI)
项目提供了用于使 PVC 作为 KubeVirt VM 磁盘的功能。建议同时部署 CDI:
$ export VERSION=$(curl -s https://github.com/kubevirt/containerized-data-importer/releases/latest | grep -o "v[0-9]\.[0-9]*\.[0-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-cr.yaml
$ kubectl get pods -n cdi
NAME READY STATUS RESTARTS AGE
cdi-apiserver-6dfc78cdfb-qj2jm 1/1 Running 0 32h
cdi-deployment-6bf458c4bb-9c2hn 1/1 Running 0 32h
cdi-operator-74d4968557-f5q49 1/1 Running 0 35h
cdi-uploadproxy-5767c4bf64-b879k 1/1 Running 0 32h
# 🔍客户端准备
Kubevirt 提供了一个命令行工具 virtctl
,可以直接下载:
$ export VERSION=$(curl -s https://api.github.com/repos/kubevirt/kubevirt/releases | grep tag_name | grep -v -- '-rc' | head -1 | awk -F': ' '{print $2}' | sed 's/,//' | xargs)
$ curl -L -o /usr/local/bin/virtctl https://github.com/kubevirt/kubevirt/releases/download/$VERSION/virtctl-$VERSION-linux-amd64
$ chmod +x /usr/local/bin/virtctl
# 使用KubeVirt创建虚拟机
# 🔍准备镜像
这里使用的是containerDisk的模式创建虚拟机,用qcow2的镜像构建。新建一个工作目录叫kubevirt
目前KubeVirt支持的镜像格式有以下:
.img
.qcow2
.iso
压缩为 .tar,.gz,.xz 格式的上述镜像
[root@k8s-master-node1 ~]# mkdir kubevirt
[root@k8s-master-node1 ~]# cd kubevirt/
[root@k8s-master-node1 kubevirt]# ll
total 567012
-rw-r--r--. 1 root root 510459904 Sep 17 2021 CentOS_7.5_x86_64_XD.qcow2
-rw-r--r--. 1 root root 88 Mar 29 23:24 Dockerfile
[root@k8s-master-node1 kubevirt]# cat Dockerfile
FROM kubevirt/container-disk-v1alpha
COPY CentOS_7.5_x86_64_XD.qcow2 /disk/centos.img
[root@k8s-master-node1 kubevirt]# docker build -t centos-disk:v1 .
Sending build context to Docker daemon 556MB
Step 1/2 : FROM kubevirt/container-disk-v1alpha
---> e4fa80ad2d49
Step 2/2 : COPY CentOS_7.5_x86_64_XD.qcow2 /disk/centos.img
---> aa0a0ad6d5f6
Successfully built aa0a0ad6d5f6
Successfully tagged centos-disk:v1
# 🔍编写VirtualMachine资源
通过vm资源创建一个虚拟机,分别使用的参数在后部分详解:
这里一共分为三个部分组成,分别是资源类型、资源模板、磁盘类型。
所以需要通过这三个部分来对kubevirt创建虚拟机所要求的资源根据需求修改。
注意:
创建虚拟机的时候,如果是本地存储创建虚拟机的时候,需要划分一部分空间,对于节点的调度,可以打上标签。
[root@k8s-master-node1 kubevirt]# cat vm2.yaml
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: centos
spec:
running: false
template:
metadata:
labels:
kubevirt.io/size: small
kubevirt.io/domain: centos
spec:
domain:
devices:
disks:
- name: containerdisk
disk:
bus: virtio
interfaces:
- name: default
masquerade: {}
resources:
requests:
memory: 1024M
cpu: 1
networks:
- name: default
pod: {}
volumes:
- name: containerdisk
containerDisk:
image: centos-disk:v1
# 🔍VirtualMachine资源详解
virtualmachine的资源类型:
- Kind是:
VirtualMachine
- api的版本是:
kubevirt.io/v1
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: centos
规范:
VirtualMachineInstance规范包含VirtualMachineInstance规范。
- 这里的running是表示:
是否创建关联VM和VMI的资源
- template表示:
是VirtualMachine Instance的直接规范
- labels:
打上两个标签
running: false
template:
metadata:
labels:
kubevirt.io/size: small
kubevirt.io/domain: centos
模板里的规范配置主机参数:
在模板规范中,配置当前主机的一些主要相关联的设备以及资源,配置规范详解如下。
- cpu:
主要是设置cpu的核心数为1个
- devices:
设备允许添加磁盘、网络接口等
- disks:
在devices的规范下添加vmi连接到的磁盘、CD-ROM、软盘和LUN
总线指示要模拟的磁盘设备的类型。支持的值:virtio,sata、scsi
- interfaces:
接口描述添加到vmi的网络接口
- machine:
机器类型
- resources:
设置资源和请求资源
- hostname:
设置主机名称
spec:
domain:
cpu:
cores: 1
devices:
disks:
- bootOrder: 1
disk:
bus: virtio
- name: cloudinitdisk
disk:
bus: virtio
- name: containerdisk
disk:
bus: virtio
interfaces:
- name: default
masquerade: {}
machine:
type: q35
resources:
limits:
cpu: "4"
memory: 4Gi
requests:
cpu: 1333m
memory: 4Gi
hostname: wtest
模板里的规范配置主机外部参数网络部分:
在配置完模板的主机规范之后,配置外部的网络参数。
- networks:
配置外部的网络参数
- pod:
配置网络接口CIDR
networks:
- name: default
pod: {}
模板里的规范配置主机外部参数磁盘:
这里主要配置的是虚拟机的磁盘来源,这里采用的是containerDisk的方式
- cloudInitNoCloud:
配置cloudinit的一些参数
- containerDisk:
ContainerDisk引用docker映像,嵌入qcow或原始磁盘
volumes:
- cloudInitNoCloud:
networkDataBase64:
userDataBase64:
name: cloudinitdisk
- name: containerdisk
containerDisk:
image: centos-disk:v1
# 🔍创建虚拟机
使用apply执行yaml文件,并且查看vm和vmi的资源,通过virtctl的命令开启虚拟机,需要等待一会,当镜像完全加载完成之后再使用console连接。
注意:
使用vm资源创建之后的虚拟机,默认状态是属于stop,需要开启之后再次查看vmi和pod的资源是否同步。
[root@k8s-master-node1 kubevirt]# kubectl apply -f vm1.yaml
[root@k8s-master-node1 kubevirt]# virtctl start centos
[root@k8s-master-node1 kubevirt]# kubectl get vm,vmi,pod
NAME AGE STATUS READY
virtualmachine.kubevirt.io/centos 7h52m Running True
NAME AGE PHASE IP NODENAME READY
virtualmachineinstance.kubevirt.io/centos 7h51m Running 10.244.0.12 k8s-master-node1 True
NAME READY STATUS RESTARTS AGE
pod/virt-launcher-centos-tjfs4 2/2 Running 0 7h51m
# 🔍连接虚拟机
virtctl 命令可以管理虚拟机的生命周期: 开启、重启、删除等。
[root@k8s-master-node1 kubevirt]# virtctl console centos
Successfully connected to centos console. The escape sequence is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-862.2.3.el7.x86_64 on an x86_64
centos login: root
Password:
Last login: Tue Mar 29 08:35:38 on ttyS0
#########################
# Welcome to XianDian #
#########################
[root@centos ~]#