Based on
Routim's IPv4 and IPv6 in KVM using the example of Hetzner, he decided to describe his howto about creating and organizing virtual machines.
Task: on a dedicated server, isolate applications as much as possible and separate the databases from them. To do this, you need two virtual machines (app1, db1). In the future, other applications can be placed in similar app1 containers.
')
On the DS3000 packet, one IP is issued (1.1.1.1) and a subnet (2.2.2.0/29). Total network we have a mask of 255.255.255.248 and 6 addresses.
The hetzner documentation suggested the option of creating a bridge for each address, which would allow all 6 addresses to be issued to the virtual machines. However, I am lazy and decided to sacrifice one of the addresses for the sake of ease of implementation.
In Ubuntu, KVM is used for virtualization, and I used the recommended libvirt to manage virtual machines.
Installation will require an installation image. You should also think in advance how the virtual machine disks will be located - either in files, or they will be real partitions of the disk, or they will be LVM logical volumes. I preferred LVM, because with its help you can easily take a snapshot of the disk, increase or decrease it.
To begin, I corrected and added the virtual networks provided by libvirt.
Initially, only the default network (/etc/libvirt/qemu/networks/default.xml) is available. After all the edits, she took the following form from me:
< network > <br> < name > default </ name > <br> < bridge name ="virbr0" /> <br> < forward mode ="nat" /> <br> < ip address ="192.168.122.1" netmask ="255.255.255.0" > <br> < dhcp > <br> < range start ="192.168.122.2" end ="192.168.122.254" /> <br> </ dhcp > <br> </ ip > <br> </ network > <br><br> * This source code was highlighted with Source Code Highlighter .
I also added two other networks - one private, in which the database server will be located, and one for the subnet 2.2.2.0/29 issued by hetzner:
/etc/libvirt/qemu/networks/private.xml
< network > <br> < name > private </ name > <br> < forward mode ='nat' /> <br> < bridge name ='virbr1' stp ='on' forwardDelay ='0' /> <br> < ip address ='192.168.123.1' netmask ='255.255.255.0' > <br> < dhcp > <br> < range start ='192.168.123.2' end ='192.168.123.254' /> <br> </ dhcp > <br> </ ip > <br> </ network > <br><br> * This source code was highlighted with Source Code Highlighter .
/etc/libvirt/qemu/networks/hetzner.xml
< network > <br> < name > hetzner </ name > <br> < bridge name ="virbr2" /> <br> < forward mode ="route" dev ="eth0" /> <br> < ip address ="2.2.2.1" netmask ="255.255.255.248" > <br> < dhcp > <br> < range start ="2.2.2.2" end ="2.2.2.7" /> <br> </ dhcp > <br> </ ip > <br> </ network > <br><br> * This source code was highlighted with Source Code Highlighter .
Next, we attach the installation image:
cd /srv && wget swtsrv.informatik.uni-mannheim.de/pub/linux/distributions/ubuntu-release/10.04.1/ubuntu-10.04.1-server-amd64.iso
We make two LVs for a virtual machine - one for the system, the other for the swap:
lvcreate -L 10G -n vm01-sys sysvg
lvcreate -L 2G -n vm01-swap sysvg
To create a disk for a swap and for a system is my preference, which is not at all mandatory for repetition. Nothing prevents to do one partition on 12GB.
Fine. Now you need to create the car itself:
virt-install --name = vm01 \
--ram = 256 \
--vcpus = 2 \
--disk path = / dev / sysvg / vm01-sys \
--disk path = / dev / sysvg / vm01-swap \
--accelerate \
--noautoconsole \
--connect = qemu: /// system \
--vnc \
--hvm \
--os-type = linux \
--cdrom = / srv / ubuntu-10.04.1-server-amd64.iso \
--network network: hetzner
After that, go to / etc / libvirt / qemu and see / edit vm01.xml:
< domain type ='kvm' > <br> < name > vm01 </ name > <br> < uuid > ... </ uuid > <br> < memory > 262144 </ memory > <br> < currentMemory > 262144 </ currentMemory > <br> < vcpu > 2 </ vcpu > <br> < os > <br> < type arch ='x86_64' machine ='pc' > hvm </ type > <br> < boot dev ='hd' /> <br> </ os > <br> < features > <br> < acpi /> <br> < apic /> <br> < pae /> <br> </ features > <br> < clock offset ='utc' /> <br> < on_poweroff > destroy </ on_poweroff > <br> < on_reboot > restart </ on_reboot > <br> < on_crash > restart </ on_crash > <br> < devices > <br> < emulator > /usr/bin/kvm </ emulator > <br> < disk type ='block' device ='disk' > <br> < source dev ='/dev/sysvg/vm01-sys' /> <br> < target dev ='sda' bus ='scsi' /> <br> </ disk > <br> < disk type ='block' device ='disk' > <br> < source dev ='/dev/sysvg/vm01-swap' /> <br> < target dev ='sdb' bus ='scsi' /> <br> </ disk > <br> < disk type ='file' device ='cdrom' > <br> < source file ='/srv/ubuntu-10.04.1-server-amd64.iso' /> <br> < target dev ='hda' bus ='ide' /> <br> < readonly /> <br> </ disk > <br> < interface type ="network" > <br> < source network ="hetzner" /> <br> < mac address ='00:11:22:33:44:55' /> <br> </ interface > <br> < serial type ='pty' > <br> < target port ='0' /> <br> </ serial > <br> < console type ='pty' > <br> < target port ='0' /> <br> </ console > <br> < input type ='mouse' bus ='ps2' /> <br> < graphics type ='vnc' port ='-1' autoport ='yes' keymap ='en-us' /> <br> </ devices > <br> </ domain > <br><br> * This source code was highlighted with Source Code Highlighter .
To boot from a disk or in vnc, you will need to press f12 to select a device, or fix boot dev = 'hd' to boot dev = 'cdrom'.
We also see the MAC address. You can register a static address in the machine itself, or you can assign a binding for DHCP in the network settings. To do this, in /etc/libvirt/qemu/networks/hetzner.xml you need to add the following line to the dhcp section:
< host mac ="00:11:22:33:44:55" name ="vm01" ip ="2.2.2.2" /> <br><br> * This source code was highlighted with Source Code Highlighter .
Then it would be nice to restart libvirt:
service libvirt-bin restart
and hook on a shell (virsh) to libvirt in order to launch networks and machines:
# virsh -c qemu:///system
virsh# net-start hetzner
virsh# net-start private
virsh# start vm01
This will start the standard installer. You can hang on to the console of the machine by VNC. However, VNC initially listens at 127.0.0.1:59**. Therefore, by uncomplicated tunneling through ssh, we forward the port to our computer:
ssh user@host -L5901:localhost:5900
and connect to VNC on 127.0.0.1:5901 or display: 1.
It is not hard to guess that for the next virtual machine, vnc will listen on port 5901, etc.
To install the second machine, repeat all actions for vm02, with the only difference that it will be in the network: private.
To avoid repeating actions, you can use the virt-clone utility:
virt-clone -o vm01 -n vm02 -f /dev/sysvg/vm02-sys -f /dev/sysvg/vm02-swap
However, after that I had to manually copy the contents of vm01-sys into vm02-sys using dd.
You should also remember to change the name of the second machine in the hostname, and in /etc/udev/rules.d/70-persistent-net.rules delete the network entry record so that the second network address with a different MAC address becomes eth0.
To add machines / networks to autorun in the virsh console, run net-autostart network_name / autostart vmname.
Note : you can control cars from the same virsh. To stop them correctly, do not forget to install the acpi package in the guest machines so that they catch the shutdown.
Note : a dns server may already be running, which listens on udp 0.0.0.0:67. So, the net-start network will not start, because dnsmasq will not be able to occupy the specified port. Therefore, we correct the system DNS settings before attempting to raise the virtual network.
Note : if you have made the partition table on the virtual machine in the installer, then on the host system, you will need to zamappit the partitions with kpartx:
kpartx -a /dev/sysvg/vm02-sys
You can find the partitioned devices in / dev / mapper / sysvg - vm02-sys *.