Kubernetes : Create Virtual Machine2024/11/18 |
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. |
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
root@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: linuxroot@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
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-nkf96 1/1 Running 0 30s cdi-deployment-8bf6546cc-xhhrg 1/1 Running 0 30s cdi-operator-659fd5d79-g49j6 1/1 Running 0 42s cdi-uploadproxy-6dcd6d454b-t985r 1/1 Running 0 30s |
[3] | Create a virtual machine. On this example, create it with Ubuntu 24.04. |
root@ctrl:~# kubectl get sc NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE nfs-client cluster.local/nfs-client-nfs-subdir-external-provisioner Delete Immediate true 2m42s apiVersion: v1 kind: PersistentVolumeClaim metadata: name: "ubuntu-pvc" labels: app: containerized-data-importer annotations: cdi.kubevirt.io/storage.import.endpoint: "http://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img" spec: accessModes: - ReadWriteOnce resources: requests: storage: 15Gi storageClassName: nfs-client
root@ctrl:~#
root@ctrl:~# kubectl apply -f ubuntu-pvc.yml persistentvolumeclaim/ubuntu-pvc created kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE ubuntu-pvc Bound pvc-7eb289d4-d9c9-4f94-8043-5b6e29c421a4 15Gi RWO nfs-client <unset> 5sroot@ctrl:~# kubectl get pods NAME READY STATUS RESTARTS AGE importer-ubuntu-pvc 1/1 Running 0 18s # possible to see importing logs root@ctrl:~# kubectl logs -f importer-ubuntu-pvc ..... ..... I1118 02:34:00.808524 1 data-processor.go:341] Expanding image size to: 15220080640 E1118 02:34:00.840043 1 prlimit.go:156] failed to kill the process; os: process already finished I1118 02:34:00.840066 1 data-processor.go:253] Validating image E1118 02:34:00.844044 1 prlimit.go:156] failed to kill the process; os: process already finished I1118 02:34:00.853015 1 data-processor.go:247] New phase: Complete I1118 02:34:00.853219 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: ubuntu2404 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: ubuntu-pvc - cloudInitNoCloud: userData: | #cloud-config hostname: ubuntu2404 ssh_pwauth: true disable_root: false chpasswd: list: | root:myrootpassword ubuntu:userpassword expire: False name: cloudinitdisk
root@ctrl:~#
root@ctrl:~# kubectl apply -f ubuntu-vm.yml virtualmachine.kubevirt.io/ubuntu2404 created kubectl get vms
NAME AGE STATUS READY
ubuntu2404 19s Stopped False
root@ctrl:~# virtctl start ubuntu2404
VM ubuntu2404 was scheduled to start
root@ctrl:~# kubectl get vmi NAME AGE PHASE IP NODENAME READY ubuntu2404 22s Running 192.168.40.232 node01.srv.world True root@ctrl:~# virtctl console ubuntu2404 Successfully connected to ubuntu2404 console. The escape sequence is ^] ubuntu2404 login: root Password: Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-48-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro System information as of Mon Nov 18 02:44:16 UTC 2024 ..... ..... root@ubuntu2404:~# # to go back to the Host's console, push Ctrl + ] key # * same operation as virsh command # connect to VM via ssh root@ctrl:~# kubectl get pods NAME READY STATUS RESTARTS AGE virt-launcher-ubuntu2404-mg4gs 2/2 Running 0 101s root@ctrl:~# kubectl port-forward pod/virt-launcher-ubuntu2404-mg4gs 222:22 & root@ctrl:~# ssh ubuntu@127.0.0.1 -p 222 Handling connection for 222 The authenticity of host '[127.0.0.1]:222 ([127.0.0.1]:222)' can't be established. ED25519 key fingerprint is SHA256:W/jCnIP+PoghhP3FOrVDa9HBGjOzip+QVBqGEep5roM. 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]:222' (ED25519) to the list of known hosts. ubuntu@127.0.0.1's password: Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-48-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro System information as of Mon Nov 18 05:25:15 UTC 2024 ..... ..... ubuntu@ubuntu2404:~$ |
Sponsored Link |