Tutorials
In this article:
Tutorials#
Virtual machine migration to K2 Cloud#
K2 Cloud uses QEMU KVM hypervisor. So, before migration, please make sure that OS supports VirtIO devices (network interfaces and disk controllers) and VirtIO drivers are installed. Even if the drivers are missing, a VM can still be started in K2 Cloud, but its disk and network performance will be much worse than when the drivers are installed.
You can upload both a VM image (in OVA format) and a set of snapshots of its virtual volumes (in VMDK, VHD or QCOW2 format) to the cloud and create your own instance image from it. This image can then be used to start a VM in K2 Cloud.
Attention
OS images that use UEFI mode cannot be imported.
Create your own image from a VM image#
Stop the VM and create its image in OVA format.
Note
An OVA file usually contains volumes in VMDK format and a VM description in OVF format. The cloud can recognize the files in OVA format and create VM images according to the OVF file.
Upload the image into the object storage in K2 Cloud.
Select the uploaded image in the bucket and click Create image.
In the dialog window:
Enter the name for the image being created.
Add its description (optional).
Add a volume snapshot description (optional). This description will be used for all volume snapshots created together with the image.
If the instance did not have VirtIO drivers, tick the Enable legacy OS support checkbox.
if necessary, configure to send notification to your email when the image is ready.
Click Create image. The image readiness status can be viewed in the section Virtual machines Storage Import/Export. The completed image will be available in the section Virtual machines Storage Images.
You can now create an instance from this image.
Create your own image from volume snapshots#
An image can be created from one or more volume snapshots.
Stop the VM and create snapshots of its volumes in one of the formats supported by K2 Cloud (VMDK, VHD, or QCOW2).
Upload the volume snapshots into the object storage in the K2 Cloud.
In the bucket, select all volumes’ snapshots to be included in the image to be created and click Create image.
In the dialog window:
Enter the name for the image being created.
Add its description (optional).
Add a volume snapshot description (optional). This description will be used for all volume snapshots created together with the image.
If the instance did not have VirtIO drivers, tick the Enable legacy OS support checkbox.
if necessary, configure to send notification to your email when the image is ready.
Note
The boot volume should be at the top of the list. Hold down the icon to drag the boot volume to the top of the list. If necessary, change the sequence of the other volumes.
Click Create image. The image readiness status can be viewed in the section Virtual machines Storage Import/Export. The completed image will be available in the section Virtual machines Storage Images.
You can now create an instance from this image.
How to create and connect to a Linux instance#
Before you start using your Linux instance, make sure that your project has at least one subnet since if it is not specified, an instance cannot be created. If necessary, you can create a subnet on your own.
K2 Cloud supports SSH key authentication.
To add an SSH key, go to the Virtual machines Storage SSH keys and click Add key.
You can use cloud-generated or custom key. Enter its name and click Add.
Store the private key locally, since it will be used in the future to remotely access the Linux instance.
Creating an instance#
To create a new Linux instance, use instance wizard.
Provisioning a Linux instance#
Once the instance switches to the Running state, you can connect to it.
VMs created from K2 Cloud images are accessed under the ec2-user account, for which access by password is disabled. For this account, only access by SSH key is possible.
To connect via SSH, allow access from an external network to an appropriate port. Port 22
is used by default:
Open the instance page, go to the Network and Security tab, and click on the security group ID you plan to use to control outside access.
On the security group page, go to the Inbound rules tab.
Add an enabling rule for the tcp protocol and port
22
.Associate Elastic IP with a network interface of this instance.
You can use any SSH client to access the instance.
Connecting to a Linux instance from Windows using Putty#
Here we consider connecting to an instance from a local Windows machine using Putty.
To use Putty with an SSH key pre-saved on a local computer in .pem format, it should be converted to .ppk format. For this purpose:
Run PuTTY Key Generator (PUTTYGEN.EXE).
Click Load to load the key in .pem format to it.
Once the key is loaded, click Save private key and save the key in .ppk format.
Now, you can connect using the SSH key:
Open Putty (PUTTY.EXE).
In the Session tab, enter Elastic IP of the instance network interface and port
22
, then choose the SSH connection type.Then, go to the SSH section and choose the Auth category.
Click Browse to specify the path to the saved private part of the key (in the .ppk format).
Click Open.
In the command line, specify the ec2-user login
Connection is established.
Connecting to a Linux instance from MacOS#
Here we consider connecting to a Linux instance from a MacOS computer:
Open a terminal
Grant the necessary rights to the private key you downloaded at the VM creation step. Use the command:
chmod 0600 ~/Downloads/my_ssh_key.pem
Using the private key located (in our case) at ~/Downloads/my_ssh_key.pem, connect to the server c2-217-73-60-2.elastic.k2.cloud under the ec2-user account:
ssh ec2-user@c2-217-73-60-2.elastic.k2.cloud -i ~/Downloads/my_ssh_key.pem
Connection is established.
How to create and connect to a Windows instance#
Before you start using your Windows instance, make sure that your project has at least one subnet. If there is no subnet than an instance cannot be created. If necessary, you can create a subnet on your own.
Creating an instance#
To create a new Windows instance, use instance wizard.
Note
For Windows, we recommend choosing instance types with at least 4,096 MB RAM.
Provisioning a Windows instance#
Once the instance is in the Running state, connect to it via Remote console:
Go to the page of the running instance.
In the Information tab, click Open console.
Enter the required parameters and the administrator password to log on to the system.
To log into OS, press Ctrl+Alt+Delete. To transfer this key combination to the OS, you can click the rightmost button in the top right corner of the K2 Cloud remote console.
Enter the administrator password and press Enter.
Connecting to a Windows instance via RDP#
To connect to a Windows instance via RDP (Remote Desktop Protocol), you should enable access from an external network to an appropriate port:
Open the Network and Security tab on the instance page and click ID of the network interface that will be used for access.
On the network interface page, open the Security Groups tab and click ID of the security group where the instance is placed.
On the security group page, open the Inbound Rules tab and click Add.
Add a permitting rule for
TCP``protocol and ``3389
port. To do so, selectTCP
in the Protocol field, specifyto specific
for the Grant access to ports option and enter3389
for Ports. If necessary, add a rule description.To prevent unauthorized users to access the console, we strongly recommend that you explicitly specify IP addresses from which access is permitted. For the Grant access option, select
from network
and in the Network field specify, in CIDR notation, the subnet or IP address from which access will be granted.Click Add. If necessary, add permitting rules for other addresses by repeating steps 3-5.
Associate Elastic IP with a network interface of this instance.
You can use any RDP client to connect to the instance from Windows. Specify Elastic IP of the Windows VM network interface as the connection address. Use the standard Administrator login and the password you specified when you first logged in the system via remote console.
How to install an OS from your template#
You can use your image for an operating system, for example, if the K2 Cloud does not have such an image. To do this, you should first install and configure the OS image locally on any virtualization platform, for example, VirtualBox or Libvirt.
Important
Please install the VirtIO drivers to make your Windows images work correctly in the cloud.
Once the OS template is set up, follow instructions on how to migrate virtual machines to the K2 Cloud.
How to change a boot volume#
This example describes how to change a boot volume using API. You can also change the boot volume in the web interface.
The c2-ec2 utility from the c2-client package is used to call API. First, install it on the machine used for management. It can be either a virtual machine in the cloud or any other machine with Internet access.
To change the boot volume:
Stop the instance (in this example
i-01234567
), for example, using the method StopInstances. To do so, run the following command on the machine with the c2-ec2 utility installed:c2-ec2 StopInstances InstanceId.1 i-01234567
Attach the second volume to the instance, using the method AttachVolume. This is the volume that you plan to use as the boot volume (in this example,
vol-01234567
).c2-ec2 AttachVolume VolumeId vol-01234567 InstanceId i-01234567 Device disk2
To change the boot volume, use the method ModifyInstanceAttribute:
c2-ec2 ModifyInstanceAttribute InstanceId i-01234567 RootDeviceName.Value vol-01234567
If successful, vol-01234567
will become a boot volume and the previous boot volume will become one of the attached ones. To verify that this volume is really bootable, run, for example, this command:
c2-ec2 DescribeInstanceAttribute InstanceId i-01234567 Attribute blockDeviceMapping
Note
The boot volume name is disk1
.
System recovery from a recovery image#
If you cannot restore your OS using its own tools, use a volume image as an external recovery tool.
You can use recovery images provided by the OS vendor or your own ones in VMDK, VHD, or QCOW2 format.
Attention
If you convert an ISO image to the required format, then it will be impossible to boot from it, since this image does not have a boot partition.
To recover the system, follow these steps:
Download your OS vendor’s recovery image in a supported format.
Note
You can also use ready-made K2 Cloud’s volume snapshots: snap-B5F44E1E for Linux and snap-3978A0A7 for Windows. In this case, skip steps 1-3.
Upload it into the object storage in the K2 Cloud.
Create volume snapshot from the uploaded image.
From the resulting volume snapshot create a new volume.
Stop the instance you want to recover.
Attach the resulting volume to the instance.
Boot the OS from the attached volume. To do this:
Change the parameter Root device name to
menu
on the instance page.Start the instance.
Open a VNC console and wait for the boot device selection menu to appear.
Select the attached volume.
Perform recovery.
Stop the instance.
Detach the volume you used for the OS recovery.
Revert the Boot device parameter to disk1.
Start and stop an instance on schedule#
You can use this instruction to start and stop instances:
Install the c2-ec2 utility on a Windows or Linux machine that you plan to use for management tasks. This can be a cloud-based VM or any other computer with Internet access. Installation process is described in K2 Cloud API Client.
Use the following commands to enable/disable an instance on a machine with the c2-ec2 utility:
c2-ec2 StartInstances InstanceId.1 <instance_id> InstanceId.2 <instance_id> c2-ec2 StopInstances InstanceId.1 <instance_id> InstanceId.2 <instance_id>
The schedule is set using a task scheduler (such as crond in Linux).
Here is an example of how to schedule a VM reboot every night at 2 A.M.:
crontab -e #command to access crond task scheduler (be sure to run it as a superuser) 0 2 * * * /usr/local/bin/restart
/usr/local/bin/restart in this example, this is the path to the script with the instance enabling/disabling commands
See more details of using c2-ec2 utility here: K2 Cloud API Client.
How to hibernate an instance#
To hibernate an instance, you should preconfigure the instance OS first.
Important
We do not guarantee that an instance without a proper preconfiguration will enter the hibernation mode after clicking the Hibernate button.
Attention
We do not recommend resizing the volume or changing the type of hibernated instance. Otherwise, the OS will boot normally, and the saved VM memory state will be ignored.
Configuring Windows to support hibernation#
To enable hibernation support for a recently launched Windows instance, follow these steps:
Download the image with the qemu-guest-agent package and drivers.
Mount the downloaded ISO image within a VM.
Install the Virtioserial driver.
Install QEMU Guest Agent.
To enable hibernation on a running Windows instance, we recommend disabling the commands, which QEMU Guest Agent does not use for hibernation:
Note
If you plan to back up this instance using K2 Cloud backup service, then do not disable guest-fsfreeze-* commands.
sc config QEMU-GA binPath= "C:\Program Files\Qemu-ga\qemu-ga.exe -d --retry-path --blacklist=guest-get-osinfo,guest-get-timezone,guest-get-users,guest-get-host-name,guest-exec,guest-exec-status,guest-get-memory-block-info,guest-set-memory-blocks,guest-get-memory-blocks,guest-set-user-password,guest-get-fsinfo,guest-set-vcpus,guest-get-vcpus,guest-network-get-interfaces,guest-suspend-hybrid,guest-suspend-ram,guest-fstrim,guest-fsfreeze-thaw,guest-fsfreeze-freeze-list,guest-fsfreeze-freeze,guest-fsfreeze-status,guest-file-flush,guest-file-seek,guest-file-write,guest-file-read,guest-file-close,guest-file-open,guest-shutdown,guest-info,guest-set-time,guest-get-time,guest-sync,guest-sync-delimited"
Restart the QEMU Guest Agent service.
Reboot the operating system.
To enable hibernation support, use the command:
powercfg /h on powercfg /h size 100
Configuring Linux to support hibernation#
Linux requires a swap to be able to enter the hibernation mode.
As a swap, you can use:
a separate volume,
a partition on an existing volume,
a file in the file system.
Important
Regardless of the swap type (file or volume), it must be larger than the RAM size of the instance.
Configuring a partition/volume as swap to support hibernation#
Create an additional partition (for example, /dev/vda3) on an existing volume or attach a new volume. This partition/volume will be used to move/store data from the memory dump. How fast the instance enters/exits the hibernation mode depends on the performance of a volume used as swap.
Format the created partition or attached volume as swap and activate it with the command:
mkswap /dev/vda3 swapon /dev/vda3
Add the following record about the swap partition to /etc/fstab:
/dev/vda3 swap swap defaults 0 0
Instead of /dev/vda3, you may specify the partition ID
UUID=b05509b2-82f1-4edb-a80e-ef8d0c100ac0
from the blkid output:blkid … /dev/vda3: UUID="2c031b77-92bd-45b4-876e-18f1de7d4674" TYPE="swap" …
Set kernel parameters. In /etc/default/grub, add the
resume=/dev/vda3
parameter to theGRUB_CMDLINE_LINUX
variable (you can also use UUID):GRUB_CMDLINE_LINUX="console=tty0 crashkernel=auto console=ttyS0,115200 resume=/dev/vda3"
Generate a new configuration file for
grub
loader:grub2-mkconfig --output=/boot/grub2/grub.cfg
Set the following value for the
resume
parameter:lsblk /dev/vda3 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vdb 253:16 0 32G 0 disk [SWAP]
Use major:minor numbers of the swap partition:
echo "253:16" > /sys/power/resume
Configure a swap file to support hibernation#
Important
To setup a swap file for hibernation, OS reboot is required.
Create a file (for example, of 10 GiB):
dd if=/dev/zero of=/swapfile count=10 bs=1GiB
Format the resultant file as swap and activate it using the commands:
mkswap /swapfile chmod 600 /swapfile swapon /swapfile
Add the following record about the swap file to /etc/fstab:
/swapfile swap swap defaults 0 0
Find out the swap file offset on the volume. For the ext3/ext4 and xfs file systems, use the following command:
filefrag -v /swapfile | awk '{ if($1=="0:"){print substr($4, 1, length($4)-2)} }'
Set kernel parameters. In /etc/default/grub, add the
resume=<partition name> resume_offset=<value>
parameter to theGRUB_CMDLINE_LINUX
variable:GRUB_CMDLINE_LINUX="console=tty0 crashkernel=auto console=ttyS0,115200 resume=/dev/vda1 resume_offset=181367"
You can find out the
<partition name>
where the swapfile is located, as follows:findmnt -no SOURCE -T /swapfile /dev/vda1
Generate a new configuration file for
grub
loader:grub2-mkconfig --output=/boot/grub2/grub.cfg
Reboot the operating system.
Configuring QEMU Guest Agent to hibernate a CentOS instance#
Make sure that the package qemu-guest-agent is installed or install it using the command:
yum install qemu-guest-agent
To enable hibernation on a recently started CentOS instance, we recommend disabling the commands, which QEMU Guest Agent does not use for hibernation. To do this, change the
BLACKLIST_RPC
parameter in the /etc/sysconfig/qemu-ga configuration file:BLACKLIST_RPC=blacklist=guest-get-osinfo,guest-get-timezone,guest-get-users,guest-get-host-name,guest-exec,guest-exec-status,guest-get-memory-block-info,guest-set-memory-blocks,guest-get-memory-blocks,guest-set-user-password,guest-get-fsinfo,guest-set-vcpus,guest-get-vcpus,guest-network-get-interfaces,guest-suspend-hybrid,guest-suspend-ram,guest-fstrim,guest-fsfreeze-thaw,guest-fsfreeze-freeze-list,guest-fsfreeze-freeze,guest-fsfreeze-status,guest-file-flush,guest-file-seek,guest-file-write,guest-file-read,guest-file-close,guest-file-open,guest-shutdown,guest-info,guest-set-time,guest-get-time,guest-sync,guest-sync-delimited
Restart the QEMU Guest Agent service:
systemctl restart qemu-guest-agent
Configuring QEMU Guest Agent to hibernate a Ubuntu instance#
Install the package qemu-guest-agent, using the following commands:
sudo apt update sudo apt install qemu-guest-agent
To enable hibernation on a recently started Ubuntu instance, we recommend disabling the commands, which QEMU Guest Agent does not use for hibernation:
cat <<EOT > /etc/qemu/qemu-ga.conf [general] blacklist=blacklist=guest-get-osinfo,guest-get-timezone,guest-get-users,guest-get-host-name,guest-exec,guest-exec-status,guest-get-memory-block-info,guest-set-memory-blocks,guest-get-memory-blocks,guest-set-user-password,guest-get-fsinfo,guest-set-vcpus,guest-get-vcpus,guest-network-get-interfaces,guest-suspend-hybrid,guest-suspend-ram,guest-fstrim,guest-fsfreeze-thaw,guest-fsfreeze-freeze-list,guest-fsfreeze-freeze,guest-fsfreeze-status,guest-file-flush,guest-file-seek,guest-file-write,guest-file-read,guest-file-close,guest-file-open,guest-shutdown,guest-info,guest-set-time,guest-get-time,guest-sync,guest-sync-delimited EOT
Restart the QEMU Guest Agent service:
systemctl restart qemu-guest-agent
Preparing an instance for RAM monitoring#
RAM monitoring is supported only for Linux instances with CloudWatch agent installed.
Note
To run CloudWatch agent, the instance should have access to Metadata API. To this end, the instance should be attached to one subnet at least.
You can deploy and run CloudWatch agent, using one of the following methods:
Passing commands in user data#
To install and run CloudWatch agent, pass the following cloud-init
commands in user data when creating an instance or launch template:
#cloud-config
runcmd:
- wget https://github.com/C2Devel/cloudwatch-metadata-agent/releases/download/0.1.0/cloudwatch-agent-0.1.0-ROCKIT.deb
- sudo dpkg -i ./cloudwatch-agent-0.1.0-ROCKIT.deb
#cloud-config
runcmd:
- sudo yum -y install wget
- wget https://github.com/C2Devel/cloudwatch-metadata-agent/releases/download/0.1.0/cloudwatch-agent-0.1.0-ROCKIT1.el8.noarch.rpm
- sudo dnf -y install ./cloudwatch-agent-0.1.0-ROCKIT1.el8.noarch.rpm
Installing an agent manually#
To install and run CloudWatch agent on the guest OS, connect to the instance and execute the following commands:
wget https://github.com/C2Devel/cloudwatch-metadata-agent/releases/download/0.1.0/cloudwatch-agent-0.1.0-ROCKIT.deb
sudo dpkg -i ./cloudwatch-agent-0.1.0-ROCKIT.deb
sudo yum install wget
wget https://github.com/C2Devel/cloudwatch-metadata-agent/releases/download/0.1.0/cloudwatch-agent-0.1.0-ROCKIT1.el8.noarch.rpm
sudo dnf install ./cloudwatch-agent-0.1.0-ROCKIT1.el8.noarch.rpm
Preparing an image with a pre-installed agent#
To avoid installing CloudWatch agent every time from scratch, prepare your own image with the pre-installed agent and use it to create instances and templates. To do it:
create the instance with the required properties;
How to correct the resetting time error in Windows#
To resolve the time problem, you have to edit Windows registry so that it accepts the BIOS time as UTC: