KubeVirt 运维:创建 VM
使用提供的镜像在 kubevirt 命名空间下创建一台 VM,名称为 exam,指定 VM 的内存、CPU、网卡和磁盘等配置。
[root@k8s-master-node1 ~]# kubectl explain kubevirt.spec. --recursive |grep use
useEmulation <boolean>
[root@k8s-master-node1 ~]# kubectl -n kubevirt edit kubevirt
spec:
certificateRotateStrategy: {}
configuration:
developerConfiguration: #{}
useEmulation: true
[root@k8s-master-node1 ~]# vim vm.yaml
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: exam
spec:
running: true
template:
spec:
domain:
devices:
disks:
- name: vm
disk: {}
resources:
requests:
memory: 1Gi
volumes:
- name: vm
containerDisk:
image: fedora-virt:v1.0
imagePullPolicy: IfNotPresent
[root@k8s-master-node1 ~]# kubectl apply -f vm.yaml
virtualmachine.kubevirt.io/exam created
[root@k8s-master-node1 ~]# kubectl get virtualmachine
NAME AGE STATUS READY
exam 31s Running True
[root@k8s-master-node1 ~]# kubectl delete -f vm.yaml
virtualmachine.kubevirt.io "exam" deleted
KubeVirt 运维:VMI 管理
将提供的镜像在default命名空间下创建一台VMI,名称为exam,使用Service 对外暴露 VMI。
[root@k8s-master-node1 ~]# vim Dockerfile
FROM scratch
ADD exam.qcow2 /disk/
[root@k8s-master-node1 ~]# docker build -t exam:v1.0 -f Dockerfile .
[root@k8s-master-node1 ~]# vim exam.yaml
apiVersion: kubevirt.io/v1
kind: VirtualMachineInstance
metadata:
name: exam
labels:
app: exam
spec:
domain:
devices:
disks:
- name: containerdisk
disk:
bus: virtio
- name: cloudinitnodisk
disk:
bus: virtio
resources:
requests:
memory: 512Mi
volumes:
- name: containerdisk
containerDisk:
image: exam:v1.0
imagePullPolicy: IfNotPresent
- name: cloudinitnodisk
cloudInitNoCloud:
userData: |-
hostname: exam
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
name: exam
labels:
app: exam
spec:
ports:
- name: 80-80
port: 80
nodePort: 30082 # 节点端口
protocol: TCP
targetPort: 80 # 目标端口
selector:
app: exam
type: NodePort
[root@k8s-master-node1 ~]# kubectl apply -f exam.yaml
virtualmachineinstance.kubevirt.io/exam created
service/exam created
[root@k8s-master-node1 ~]# kubectl get vmi
NAME AGE PHASE IP NODENAME READY
exam 60s Running 10.244.0.50 k8s-master-node1 True