Kubernetes : Create Virtual Machine |
Create a virtual machine with KubeVirt. This example is based on the environment like follows. +----------------------+ +----------------------+
| [ ctrl.srv.world ] | | [ dlp.srv.world ] |
| Manager Node | | Control Plane |
+-----------+----------+ +-----------+----------+
eth0|10.0.0.25 eth0|10.0.0.30
| |
------------+--------------------------+-----------
| |
eth0|10.0.0.51 eth0|10.0.0.52
+-----------+----------+ +-----------+----------+
| [ node01.srv.world ] | | [ node02.srv.world ] |
| Worker Node#1 | | Worker Node#2 |
+----------------------+ +----------------------+
|
| [1] | A Persistent storage is needed to store OS images. |
| [2] | Install Containerized Data Importer to store OS images. |
debian@ctrl:~$ export TAG=$(curl -s -w %{redirect_url} https://github.com/kubevirt/containerized-data-importer/releases/latest) debian@ctrl:~$ export VERSION=$(echo ${TAG##*/}) debian@ctrl:~$ wget https://github.com/kubevirt/containerized-data-importer/releases/download/${VERSION}/cdi-operator.yaml debian@ctrl:~$ wget https://github.com/kubevirt/containerized-data-importer/releases/download/${VERSION}/cdi-cr.yaml debian@ctrl:~$ vi cdi-cr.yaml apiVersion: cdi.kubevirt.io/v1beta1
kind: CDI
metadata:
name: cdi
spec:
config:
# add resource section to expand memory limits
podResourceRequirements:
limits:
cpu: '1'
memory: 4Gi
featureGates:
- HonorWaitForFirstConsumer
imagePullPolicy: IfNotPresent
infra:
nodeSelector:
kubernetes.io/os: linux
tolerations:
- key: CriticalAddonsOnly
operator: Exists
workload:
nodeSelector:
kubernetes.io/os: linux
debian@ctrl:~$ kubectl apply -f cdi-operator.yaml namespace/cdi created customresourcedefinition.apiextensions.k8s.io/cdis.cdi.kubevirt.io created clusterrole.rbac.authorization.k8s.io/cdi-operator-cluster created clusterrolebinding.rbac.authorization.k8s.io/cdi-operator created serviceaccount/cdi-operator created role.rbac.authorization.k8s.io/cdi-operator created rolebinding.rbac.authorization.k8s.io/cdi-operator created deployment.apps/cdi-operator created debian@ctrl:~$ kubectl apply -f cdi-cr.yaml cdi.cdi.kubevirt.io/cdi created # after a few minutes, the pods will start up as follows debian@ctrl:~$ kubectl get pods -n cdi NAME READY STATUS RESTARTS AGE cdi-apiserver-56f6589fc5-mmjqm 1/1 Running 1 (2m15s ago) 2m33s cdi-deployment-7b9c6dd88c-l7qhf 1/1 Running 0 2m33s cdi-operator-575d6ffd85-s749h 1/1 Running 0 2m52s cdi-uploadproxy-8c64dc68c-rtv9h 1/1 Running 0 2m33s |
| [3] | Create a virtual machine. On this example, create it with Debian. |
| debian@ctrl:~$ kubectl get sc NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE nfs-client cluster.local/nfs-client-nfs-subdir-external-provisioner Delete Immediate true 55m apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: "debian-pvc"
labels:
app: containerized-data-importer
annotations:
cdi.kubevirt.io/storage.import.endpoint: "https://cdimage.debian.org/images/cloud/trixie/latest/debian-13-generic-amd64.raw"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
debian@ctrl:~$ debian@ctrl:~$ kubectl apply -f debian-pvc.yml persistentvolumeclaim/debian-pvc created kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE debian-pvc Bound pvc-f043ad24-c5a4-4555-b49d-232b77875057 10Gi RWO nfs-client <unset> 8sdebian@ctrl:~$ kubectl get pods NAME READY STATUS RESTARTS AGE importer-debian-pvc 1/1 Running 0 3s # possible to see importing logs debian@ctrl:~$ kubectl logs -f importer-debian-pvc .....
.....
I0825 01:59:14.846077 1 data-processor.go:354] Expanding image size to: 10129244160
E0825 01:59:14.851996 1 prlimit.go:156] failed to kill the process; os: process already finished
I0825 01:59:14.852010 1 data-processor.go:266] Validating image
E0825 01:59:14.855600 1 prlimit.go:156] failed to kill the process; os: process already finished
I0825 01:59:14.857726 1 data-processor.go:260] New phase: Complete
I0825 01:59:14.857896 1 importer.go:231] {"scratchSpaceRequired":false,"preallocationApplied":false,"message":"Import Complete"}
# after finishing importing, importer pod will also finish debian@ctrl:~$ kubectl get pods No resources found in default namespace. apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: debian13
labels:
kubevirt.io/os: linux
spec:
runStrategy: Halted
template:
spec:
domain:
cpu:
cores: 2
devices:
disks:
- disk:
bus: virtio
name: disk0
- cdrom:
bus: sata
readonly: true
name: cloudinitdisk
interfaces:
- name: default
masquerade: {}
machine:
type: q35
resources:
requests:
memory: 4096M
networks:
- name: default
pod: {}
volumes:
- name: disk0
persistentVolumeClaim:
claimName: debian-pvc
- cloudInitNoCloud:
userData: |
#cloud-config
hostname: debian13
ssh_pwauth: true
disable_root: false
chpasswd:
list: |
root:myrootpassword
debian:userpassword
expire: False
name: cloudinitdisk
debian@ctrl:~$ debian@ctrl:~$ kubectl apply -f debian-vm.yml virtualmachine.kubevirt.io/debian13 created kubectl get vms NAME AGE STATUS READY
debian13 6s Stopped False
debian@ctrl:~$ virtctl start debian13
VM debian13 was scheduled to start
debian@ctrl:~$ kubectl get vmi NAME AGE PHASE IP NODENAME READY debian13 13s Running 192.168.241.153 node02.srv.world True debian@ctrl:~$ virtctl console debian13 Successfully connected to debian13 console. Press Ctrl+] or Ctrl+5 to exit console. debian13 login: root Password: Linux debian13 6.12.41+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.41-1 (2025-08-12) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. root@debian13:~# # to go back to the Host's console, push Ctrl + ] key # * same operation as virsh command # connect to VM via ssh debian@ctrl:~$ kubectl get pods NAME READY STATUS RESTARTS AGE virt-launcher-debian13-4lfmm 2/2 Running 0 32s debian@ctrl:~$ kubectl port-forward pod/virt-launcher-debian13-4lfmm 2221:22 & debian@ctrl:~$ ssh debian@127.0.0.1 -p 2221 Handling connection for 2221 The authenticity of host '[127.0.0.1]:2221 ([127.0.0.1]:2221)' can't be established. ED25519 key fingerprint is SHA256:bc9jh0VgOxVNFIgmSVol7UKendQSROJDCl0n0UWSd5A. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '[127.0.0.1]:2221' (ED25519) to the list of known hosts. debian@127.0.0.1's password: Linux debian13 6.12.41+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.41-1 (2025-08-12) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. debian@debian13:~$ |
No comments:
Post a Comment