Kubernetes : Create Virtual Machine2024/10/29 |
|
Create a virtual machine with KubeVirt. This example is based on the environment like follows.
-----------+---------------------------+--------------------------+------------
| | |
eth0|10.0.0.25 eth0|10.0.0.71 eth0|10.0.0.72
+----------+-----------+ +-----------+-----------+ +-----------+-----------+
| [ ctrl.srv.world ] | | [snode01.srv.world] | | [snode02.srv.world] |
| Control Plane | | Worker Node | | Worker Node |
+----------------------+ +-----------------------+ +-----------------------+
|
| [1] |
A Persistent storage is needed to store OS images.
On this example, install NFS Server on Control Plane Node and configure [/home/nfsshare] directory as NFS share as external persistent storage, and also configure dynamic volume provisioning with NFS plugin like the example of [1], [2], [3]. |
| [2] | Install Containerized Data Importer to store OS images. |
|
root@ctrl:~#
root@ctrl:~# export TAG=$(curl -s -w %{redirect_url} https://github.com/kubevirt/containerized-data-importer/releases/latest) root@ctrl:~# export VERSION=$(echo ${TAG##*/}) root@ctrl:~# wget https://github.com/kubevirt/containerized-data-importer/releases/download/${VERSION}/cdi-operator.yaml root@ctrl:~# wget https://github.com/kubevirt/containerized-data-importer/releases/download/${VERSION}/cdi-cr.yaml
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
root@ctrl:~#
kubectl apply -f cdi-cr.yaml cdi.cdi.kubevirt.io/cdi created # after a few minutes, the pods will start up as follows root@ctrl:~# kubectl get pods -n cdi NAME READY STATUS RESTARTS AGE cdi-apiserver-555ccd5f7b-jz5pp 1/1 Running 0 17s cdi-deployment-8bf6546cc-6fq9r 1/1 Running 0 17s cdi-operator-659fd5d79-thdtw 1/1 Running 0 38s cdi-uploadproxy-6dcd6d454b-9vl6x 1/1 Running 0 17s |
| [3] | Create a virtual machine. On this example, create it with debian 12. |
|
root@ctrl:~# kubectl get sc NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE nfs-client cluster.local/nfs-client-nfs-subdir-external-provisioner Delete Immediate true 15m
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: "debian12-pvc"
labels:
app: containerized-data-importer
annotations:
cdi.kubevirt.io/storage.import.endpoint: "https://cdimage.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.raw"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
root@ctrl:~#
root@ctrl:~# kubectl apply -f debian12-pvc.yml persistentvolumeclaim/debian12 created kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE debian12-pvc Bound pvc-35e24f79-c234-4fca-8edb-3add5b306084 10Gi RWO nfs-client <unset> 5sroot@ctrl:~# kubectl get pods NAME READY STATUS RESTARTS AGE importer-debian12-pvc 1/1 Running 0 35s # possible to see importing logs root@ctrl:~# kubectl logs -f importer-debian12-pvc
.....
.....
I1029 03:22:00.246565 1 data-processor.go:341] Expanding image size to: 10146021376
E1029 03:22:00.254057 1 prlimit.go:156] failed to kill the process; os: process already finished
I1029 03:22:00.254082 1 data-processor.go:253] Validating image
E1029 03:22:00.258163 1 prlimit.go:156] failed to kill the process; os: process already finished
I1029 03:22:00.261407 1 data-processor.go:247] New phase: Complete
I1029 03:22:00.261655 1 importer.go:231] {"scratchSpaceRequired":false,"preallocationApplied":false,"message":"Import Complete"}
# after finishing importing, importer pod will also finish root@ctrl:~# kubectl get pods No resources found in default namespace.
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: debian12
labels:
kubevirt.io/os: linux
spec:
running: true
template:
spec:
domain:
cpu:
cores: 2
devices:
disks:
- disk:
bus: virtio
name: disk0
- cdrom:
bus: sata
readonly: true
name: cloudinitdisk
machine:
type: q35
resources:
requests:
memory: 4096M
volumes:
- name: disk0
persistentVolumeClaim:
claimName: debian12-pvc
- cloudInitNoCloud:
userData: |
#cloud-config
hostname: debian12
ssh_pwauth: true
disable_root: false
chpasswd:
list: |
root:myrootpassword
debian:userpassword
expire: False
name: cloudinitdisk
root@ctrl:~#
root@ctrl:~# kubectl apply -f debian12-vm.yml virtualmachine.kubevirt.io/debian12 created kubectl get vms NAME AGE STATUS READY debian12 15s Running Trueroot@ctrl:~# kubectl get vmi AME AGE PHASE IP NODENAME READY debian12 25s Running 192.168.211.157 snode02.srv.world True root@ctrl:~# virtctl console debian12 Successfully connected to debian12 console. The escape sequence is ^] debian12 login: root Password: Linux debian12 6.1.0-26-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.112-1 (2024-09-30) 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@debian12:~# # to go back to the Host's console, push Ctrl + ] key # * same operation as virsh command root@ctrl:~# ssh debian@192.168.211.157 The authenticity of host '192.168.211.157 (192.168.211.157)' can't be established. ED25519 key fingerprint is SHA256:LbB8PnkFGCxvmB1puODuh/t5NHp+ojfJAZp2Gq97TJQ. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.211.157' (ED25519) to the list of known hosts. debian@192.168.211.157's password: Linux debian12 6.1.0-26-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.112-1 (2024-09-30) 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@debian12:~$ |
| Sponsored Link |
|
|