Creating a VM from a public Linux image
This section provides guidelines for creating a VM with a Linux OS. To create a Windows-based VM, follow the instructions Creating a VM from a Windows public image.
To create a VM:
-
In the management console, select a folder to create a VM in.
-
In the list of services, select Compute Cloud.
-
Click Create VM.
-
Under Basic parameters:
-
Enter a name and description for the VM. Naming requirements:
- The name must be unique within the folder.
- The name may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter. The last character can't be a hyphen.
- The maximum length of the name is 63 characters.
Note
The VM name is used to generate the FQDN only once, when creating the VM. If the FQDN is important to you, choose an appropriate name for the VM at the creation stage.
-
(optional) Select or create a service account. By using a service account, you can flexibly configure access rights for your resources.
-
Select the availability zone to host the VM in.
-
-
Select an image and a Linux-based OS version under Images from Cloud Marketplace.
-
(optional) Configure the boot disk under Disks:
- Specify the necessary disk size.
- Select a disk type.
If you want to create an instance from an existing disk, go to Disks to add a disk.
-
Under Computing resources:
- Choose the platform.
- Specify the guaranteed share and number of vCPUs and RAM you need.
- If necessary, make your VM preemptible.
-
Under Network settings:
- Specify the subnet ID or select a cloud network from the list. If you don't have a network, click Create a new network to create one:
- In the window that opens, enter a name for the new network and choose a subnet to connect the virtual machine to. Each network must have at least one subnet (if there's no subnet, create one). Then click Create.
- In the Public IP field, choose a method for assigning an IP address:
- Auto: Assign a random IP address from the Yandex.Cloud IP pool.
- List: Select a public IP address from the list of previously reserved static addresses. For more information, see Making a dynamic public IP address static.
- No address: Don't assign a public IP address.
- (optional) Enable DDoS protection.
- Specify the subnet ID or select a cloud network from the list. If you don't have a network, click Create a new network to create one:
-
Under Access, specify the data required to access the VM:
-
Enter the username in the Login field.
Alert
Don't use the username
root
or other names reserved by the operating system. To perform operations that require superuser permissions, use the commandsudo
. -
In the SSH key field, paste the contents of the public key file.
-
-
Click Create VM.
The virtual machine appears in the list. When a VM is being created, it is assigned an IP address and hostname (FQDN).
If you don't have the Yandex.Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
View the description of the CLI command for creating a VM:
$ yc compute instance create --help
-
Prepare the key pair (public and private keys) for SSH access to the VM.
-
Select a public image based on a Linux OS (for example, CentOS 7).
To get a list of available images, run the following command:
yc compute image list --folder-id standard-images
Command execution result:
+----------------------+-------------------------------------+--------------------------+----------------------+--------+ | ID | NAME | FAMILY | PRODUCT IDS | STATUS | +----------------------+-------------------------------------+--------------------------+----------------------+--------+ ... | fdvk34al8k5nltb58shr | centos-7-1549279494 | centos-7 | dqni65lfhvv2den5gtv9 | READY | | fdv7ooobjfl3ts9gqp0q | windows-2016-gvlk-1548913814 | windows-2016-gvlk | dqnnc72gj2ist3ktjj1p | READY | | fdv4f5kv5cvf3ohu4flt | ubuntu-1604-lts-1549457823 | ubuntu-1604-lts | dqnnb6dc7640c5i968ro | READY | ... +----------------------+-------------------------------------+--------------------------+----------------------+--------+
-
Select a subnet:
$ yc vpc subnet list +----------------------+-----------------------+----------------------+----------------+---------------+-----------------+ | ID | NAME | NETWORK ID | ROUTE TABLE ID | ZONE | RANGE | +----------------------+-----------------------+----------------------+----------------+---------------+-----------------+ | b0c6n43f9lgh3695v2k2 | default-ru-central1-c | enpe3m3fa00udao8g5lg | | ru-central1-c | [10.130.0.0/24] | | e2l2da8a20b33g7o73bv | default-ru-central1-b | enpe3m3fa00udao8g5lg | | ru-central1-b | [10.129.0.0/24] | | e9bnlm18l70ao30pvfaa | default-ru-central1-a | enpe3m3fa00udao8g5lg | | ru-central1-a | [10.128.0.0/24] | +----------------------+-----------------------+----------------------+----------------+---------------+-----------------+
-
Create a VM in the default folder. Specify the following parameters:
-
name
: VM name.Note
The VM name is used to generate the FQDN only once, when creating the VM. If the FQDN is important to you, choose an appropriate name for the VM at the creation stage.
-
zone
: Availability zone that corresponds to the selected subnet. -
subnet-name
: Name of the selected subnet. -
image-family
: Family of images, such ascentos-7
. This option lets you install the latest version of the operating system from the specified family. -
Public IP. To create a VM without a public IP address, disable the
nat-ip-version=ipv4
option. -
ssh-key
: Path to the public SSH key. The useryc-user
will be automatically created on the VM for this key.
$ yc compute instance create \ --name first-instance \ --zone ru-central1-a \ --network-interface subnet-name=default-ru-central1-a,nat-ip-version=ipv4 \ --create-boot-disk image-folder-id=standard-images,image-family=centos-7 \ --ssh-key ~/.ssh/id_rsa.pub
This creates a VM named
first-instance
. -
Create a VM using the method Create for the resource Instance
:
-
Prepare the key pair (public and private keys) for SSH access to the VM.
-
Get an IAM token for authenticating in the examples:
- Instructions for users with a Yandex account.
- Instructions for a service account.
-
Get the ID of the folder.
-
Get information about the image to create your virtual machine from (image ID and minimum disk size):
- If you know the image family, get information about the latest image in this family:
$ export IAM_TOKEN=CggaATEVAgA... $ export FAMILY=ubuntu-1804 $ curl -H "Authorization: Bearer ${IAM_TOKEN}" \ "https://compute.api.cloud.yandex.net/compute/v1/images:latestByFamily?folderId=standard-images&family=${FAMILY}"
- You can get information about the image from the list of public images.
- If you know the image family, get information about the latest image in this family:
-
Get the subnet ID and availability zone ID. Specify the ID of the folder where the subnet was created in your request:
$ export IAM_TOKEN=CggaATEVAgA... $ export FOLDER_ID=b1gvmob95yysaplct532 $ curl -H "Authorization: Bearer ${IAM_TOKEN}" \ "https://vpc.api.cloud.yandex.net/vpc/v1/subnets?folderId=${FOLDER_ID}" { "subnets": [ { "v4CidrBlocks": [ "10.130.0.0/24" ], "id": "b0c6n43ftldh30l0vfg2", "folderId": "b1gvmob95yysaplct532", "createdAt": "2018-09-23T12:15:00Z", "name": "default-ru-central1-c", "description": "Auto-created default subnet for zone ru-central1-c", "networkId": "enpe3m3fagludao8aslg", "zoneId": "ru-central1-c" }, ... ] }
-
Create a file where the body contains your request for VM creation (for example,
body.json
). Specify the following parameters:folderId
: ID of the folder.name
: Name to be assigned to the VM when it's created.zoneId
: Availability zone that corresponds to the selected subnet.platformId
: The platform.resourceSpec
: Resources available to the VM. The values must match the selected platform.metadata
: In the metadata, pass the public key for accessing the VM via SSH. For more information, see VM instance metadata.bootDiskSpec
: Boot disk settings. Specify the ID of the selected image and disk size. The disk size must not be below the minimum value specified in the image details.networkInterfaceSpecs
: Network settings.subnetId
: ID of the selected subnet.primaryV4AddressSpec
: IP address to be assigned to the VM. To add a public IP address to your virtual machine, specify:"primaryV4AddressSpec": { "oneToOneNatSpec": { "ipVersion": "IPV4" } }
Read more about the request body format in the API reference API method.
Example
body.json
file:{ "folderId": "b1gvmob95yysaplct532", "name": "instance-demo-no-pwauth", "zoneId": "ru-central1-c", "platformId": "standard-v1", "resourcesSpec": { "memory": "2147483648", "cores": "2", }, "metadata": { "user-data": "#cloud-config\nusers:\n - name: user\n groups: sudo\n shell: /bin/bash\n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n ssh-authorized-keys:\n - ssh-rsa AAAAB3N... user@example.com" }, "bootDiskSpec": { "diskSpec": { "size": "2621440000", "imageId": "fd8rc75pn12fe3u2dnmb" } }, "networkInterfaceSpecs": [ { "subnetId": "b0c6n43ftldh30l0vfg2", "primaryV4AddressSpec": { "oneToOneNatSpec": { "ipVersion": "IPV4" } } } ] }
-
Create a virtual machine:
$ export IAM_TOKEN=CggaATEVAgA... $ curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${IAM_TOKEN}" \ -d '@body.json' \ https://compute.api.cloud.yandex.net/compute/v1/instances
If you don't have Terraform yet, install it and configure the Yandex.Cloud provider.
-
In the configuration file, describe the parameters of resources that you want to create:
Note
If you already have suitable resources, such as a cloud network and subnet, you don't need to describe them again. Use their names and IDs in the appropriate parameters.
yandex_compute_instance
: Description of the VM:name
: VM name.platform_id
: The platform.resources
: The number of vCPU cores and the amount of RAM available to the VM. The values must match the selected platform.boot_disk
: Boot disk settings. Specify the ID of the selected image. You can get the image ID from the list of public images.network_interface
: Network settings. Specify the ID of the selected subnet. To automatically assign a public IP address to the VM, setnat = true
.metadata
: In the metadata, pass the public key for accessing the VM via SSH. For more information, see VM instance metadata.
yandex_vpc_network
: Description of the cloud network.yandex_vpc_subnet
: Description of the subnet that the VM will be connected to.
Example configuration file structure:
resource "yandex_compute_instance" "vm-1" { name = "linux-vm" platform_id = "standard-v2" resources { cores = <number of vCPU cores> memory = <RAM in GB> } boot_disk { initialize_params { image_id = "<image ID>" } } network_interface { subnet_id = "${yandex_vpc_subnet.subnet-1.id}" nat = true } metadata = { ssh-keys = "<user name>:<SSH key contents>" } } resource "yandex_vpc_network" "network-1" { name = "network1" } resource "yandex_vpc_subnet" "subnet-1" { name = "subnet1" zone = "<availability zone>" network_id = "${yandex_vpc_network.network-1.id}" }
For more information about the resources you can create using Terraform, see the provider documentation.
-
Make sure that the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run the check using the command:
$ terraform plan
If the configuration is described correctly, the terminal displays a list of created resources and their parameters. If there are errors in the configuration, Terraform points them out.
-
-
Deploy the cloud resources.
-
If the configuration doesn't contain any errors, run the command:
$ terraform apply
-
Confirm that you want to create the resources.
Afterwards, all the necessary resources are created in the specified folder. You can check resource availability and their settings in management console.
-
When a VM is created, it is assigned an IP address and hostname (FQDN). This data can be used for SSH access.
You can make a public IP address static. For more information, see Making a VM's public IP address static.