-
Making a KubeVirt image
- 1. Preparation of disk files
- 2. Writing the Dockerfile
- 3. Mirroring
- 4. Upload mirrors to the repository (optional)
- 5. Export mirrors
- 6. Virtual machine yaml file
- 7. Starting the virtual machine
- 8. Error booting virtual machine
Making a KubeVirt image
We now have Kubevirt installed and the first VM running, but this VM is not what we want, we now want to customize the image
Kubernetes version 1.28.2
1. Preparation of disk files
OpenEuler's qcow2 file download address
I'm building the openeuler VM here, and I can download the qcow2 file directly from the major mirror sites, and it's a zip file after downloading, so we need to decompress it.
[root@master ~]# wget /openeuler/openEuler-22.03-LTS-SP4/virtual_machine_img/x86_64/openEuler-22.03-LTS-SP4-x86_64.
[root@master ~]# xz -d openEuler-22.03-LTS-SP4-x86_64.
This way a disk file is ready for us, but of course there are other ways to get it, such as creating a virtual machine through KVM and taking the disk file out, or getting the qcow2 file through openstack
2. Writing the Dockerfile
We need to pass this disk file inside the container image
[root@master ~]# mkdir -p kubevirt/images
[root@master ~]# cd kubevirt/images
[root@master images]# cp ~/openEuler-22.03-LTS-SP4-x86_64.qcow2 .
[root@master images]# ls
openEuler-22.03-LTS-SP4-x86_64.qcow2
file in this place, and then we start writing the dockerfile
FROM openeuler/openeuler:22.03
ADD openEuler-22.03-LTS-SP4-x86_64.qcow2 /disk/openEuler-22.03-LTS-SP4-x86_64.qcow2
Just write these 2 lines and start building the image
3. Mirroring
[root@master images]# docker build -t openeuler2203-sp4:v1 .
Next wait for the build to complete
4. Upload mirrors to the repository (optional)
If you don't have a private mirror repository, this step can be left out and you can just export the mirror and import it using ctr (more on that in a minute)
We now need to upload the image to the private repository, so we have to change the image tag first
[root@master images]# docker tag openeuler2203-sp4:v1 /openeuler2203-sp4:v1
[root@master images]# docker push /openeuler2203-sp4:v1
You have to log in before you can push the image, just log in on your own, and the image will be uploaded here.
5. Export mirrors
If you don't have a private repository, do this step
[root@master images]# docker save -o openeuler2203-sp4:v1
[root@master images]# ctr -n image import
Once the import is complete we can use this image to boot the VM, next we prepare a boot template for the VM
6. Virtual machine yaml file
[root@master images]# vim
apiVersion: /v1
kind: VirtualMachine
metadata:
name: openeuler
spec:
running: false
template:
metadata:
labels:
/domain: openeuler
spec:
domain:
devices:
disks:
- name: rootfs
disk:
bus: virtio
- name: cloudinit
disk:
bus: virtio
interfaces:
- name: default
bridge: {}
resources:
requests:
memory: 1Gi
limits:
memory: 1Gi
networks:
- name: default
pod: {}
volumes:
- name: rootfs
containerDisk:
image: openeuler2203-sp4:v1
# This is where you fill in the name of the disk file you just sent in.
path: /disk/openEuler-22.03-LTS-SP4-x86_64.qcow2
#- name: cloudinit
# cloudInitNoCloud:
# userData: |-
# #cloud-config
# password: openeuler
# chpasswd: { expire: False }
# ssh_pwauth: True
# packages:
# - nginx
# runcmd:
# - systemctl enable nginx
# - systemctl start nginx
The last paragraph is cloud-init, but openeuler does not have this service installed by default, so it is useless to write it, replace it with another system is completely OK, here's the cloud-init is to allow him to perform some personalized operations, I am here is to write the installation of nginx and change the password of the root, which does not take effect in my case, so I commented on the
7. Starting the virtual machine
We have the image ready as well as the yaml file, so we can now start booting the virtual machine
[root@master images]# kubectl apply -f
[root@master kubevirt]# kubectl get vms
NAME AGE STATUS READY
openeuler 41s Stopped False
Next we start this virtual machine
[root@master kubevirt]# virtctl start openeuler
VM openeuler was scheduled to start
[root@master kubevirt]# kubectl get vms
NAME AGE STATUS READY
openeuler 1m17s Running True
Now that he's running, we can connect in.
[root@master kubevirt]# virtctl console openeuler
Successfully connected to openeuler console. The escape sequence is ^]
openeuler login: root
Password:
Authorized users only. All activities may be monitored and reported.
Welcome to 5.10.0-216.0.0.115.oe2203sp4.x86_64
System information as of time: Tue Jul 23 09:32:59 AM UTC 2024
System load: 0.38
Memory used: 3.7%
Swap used: 0.0%
Usage On: 4%
IP address: 10.244.219.127
Users online: 1
[root@openeuler ~]#
The default password for this image isopenEuler12#$
Or connect directly via ssh, his IP address is 10.244.219.127
[root@master kubevirt]# ssh [email protected]
Authorized users only. All activities may be monitored and reported.
[email protected]'s password:
It's possible to connect.
8. Error booting virtual machine
[root@master kubevirt]# kubectl apply -f
Warning: /v1alpha3 is now deprecated and will be removed in a future release.
The request is invalid: [2]: HostDisk feature gate is not enabled
If you're getting this error it's because the hostDisk feature is not turned on, so let's turn it on.
[root@master kubevirt]# kubectl edit -n kubevirt kv kubevirt
spec:
configuration:
developerConfiguration:
featureGates:
- HostDisk
Find this paragraph, change the content to this, then save and exit to resolve the error