1.安装kubevirt-operator.yaml (可以直接指定VERSION=v0.59.0-alpha.2;可以直接先在浏览器访问github下载yaml)
export VERSION=$(curl -s https://api.github.com/repos/kubevirt/kubevirt/releases | grep tag_name | grep -v -- '-rc' | sort -r | head -1 | awk -F': ' '{print $2}' | sed 's/,//' | xargs)echo $VERSION
kubectl create -f https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-operator.yaml
验证:kubectl -n kubevirt get pod
2.安装kubevirt-cr.yaml
kubectl create -f https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-cr.yaml
如果需要开启功能特性(如热迁移、热插拔、dataVolume),需要先编辑kbuevirt.cr.yaml
---
apiVersion: kubevirt.io/v1
kind: KubeVirt
metadata:
name: kubevirt
namespace: kubevirt
spec:
certificateRotateStrategy: {}
configuration:
developerConfiguration:
featureGates:
- LiveMigration # 热迁移特性
- DataVolumes # dataVolume特性
- HotplugVolumes # 热插拔特性
- HostDisk # 本地磁盘
customizeComponents: {}
imagePullPolicy: IfNotPresent
workloadUpdateStrategy: {}
infra:
replicas: 1 # api副本数调整为1,使用默认的2
验证:kubectl -n kubevirt get kubevirt
3.安装CDI,用于使用dataVolume
cdi-operator: v1.55.2
kubectl apply -f cdi-operator.yaml
验证:kubectl -n cdi get pod
kubectl apply -f cdi-cr.yaml
验证:kubectl -n cdi get cdi
4.创建dataVolume
如果k3s版本过低(本人当时遇到错误的版本是:k3s v1.18.5),会提示没有dataVolume类型的错误
kubectl apply -f dataVolume.yaml
apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
name: example-blank-dv
spec:
source:
blank: {}
pvc:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
验证:kubectl get pvc
5.创建vm
kubectl apply -f vm.yaml
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: testvm
spec:
running: false
template:
metadata:
labels:
kubevirt.io/size: small
kubevirt.io/domain: testvm
spec:
domain:
devices:
disks:
- name: containerdisk
disk:
bus: virtio
- name: cloudinitdisk
disk:
bus: virtio
interfaces:
- name: default
masquerade: {}
resources:
requests:
memory: 64M
networks:
- name: default
pod: {}
volumes:
- name: containerdisk
containerDisk:
image: quay.io/kubevirt/cirros-container-disk-demo
- name: cloudinitdisk
cloudInitNoCloud:
userDataBase64: SGkuXG4=
验证:kubectl get vm
6.启动虚机testvm
virtctl start testvm
说明:virtctl安装参见《virtctl安装》
验证:kubectl get vmi
7.连接虚机
virtctl console testvm
输入账号密码即可登录
8.退出vmi连接
ctrl+]
参考:
更加全面,版本更新: