📜 ⬆️ ⬇️

Working with Virtual Private Cloud API: Console Clients

OpenStack API

We continue a series of publications about the new service “Virtual Private Cloud”. Today we will talk about the OpenStack API and show how you can interact with it using console clients.

Create user


Before you start working with the API, you need to create a new user and add it to the project. In the “Virtual Private Cloud” menu, select the “Users” item:

VPC user creation
')
A page with a list of users will open:

api users

The list is still empty. Click on the button "Create user", in the window that opens, enter his name and again click on the button "Create". The password for logging in to the panel will be generated automatically. View the properties of the new user by clicking on the icon next to his name in the list:

OpenStack API

Click on the link “Add to project” and select from the list that opens the projects to which the created user will have access.

After that, the user will be displayed in the control panels of these projects. Next to the user name will be a link by which you can access project resources through a browser:

Open stack API

Let's follow this link and enter the project under the account of the created user. After that, go to the tab "Access" and download the RC-file (it is a script with which console clients can authenticate in the Identity API v3).

Install the software


To configure the system to work with the project, you will need to install additional software. In this article we provide installation instructions for OC Ubuntu 14.04. For other operating systems, commands may differ; instructions for OS Debian 7.0 and CentOS 6.5 can be found directly in the control panel (tab "Access").

Install the following packages:

$ apt-get update $ apt-get install curl python-pip python-dev git libxml2-dev libxslt1-dev python-keystoneclient python-heatclient python-novaclient python-glanceclient python-neutronclient 

Next, install the software, which is either completely absent or present in the repositories of Ubuntu, but in older versions:

 $ pip install git+https://github.com/openstack/python-cinderclient $ pip install cliff --upgrade $ pip install python-openstackclient 

After that execute the command:

 $ source rc.sh 

The program will ask for a password. We will re-enter the password of the user, under whose account we entered the external panel.
Now you can get to work.

View network information


To create virtual machines, you need a network. View a list of available networks:

 $ neutron net-list 

 +--------------------------------------+------------------+-----------------------------------------------------+ | id | name | subnets | +--------------------------------------+------------------+-----------------------------------------------------+ | 1c037362-487f-4103-a73b-6cba3f5532dc | nat | b7be542a-2eef-465e-aacd-34a0c83e6afa 192.168.0.0/24 | | ab2264dd-bde8-4a97-b0da-5fea63191019 | external-network | 102a9263-2d84-4335-acfb-6583ac8e70aa | | | | aa9e4fc4-63b0-432e-bcbd-82a613310acb | | fce90252-7d99-4fc7-80ae-ef763d12938d | newnetwork | 5a1a68f9-b885-47b7-9c7e-6f0e08145e3b 192.168.1.0/24 | +--------------------------------------+------------------+-----------------------------------------------------+ 


Each network has an identification number (column ID); it will need to be specified when creating a new server.

You can activate the network configuration via a graphical interface (for more details, see here ).

Server operations


View a list of available images:

 $ glance image-list 

 +--------------------------------------+--------------------------+-------------+------------------+-------------+--------+ | ID | Name | Disk Format | Container Format | Size | Status | +--------------------------------------+--------------------------+-------------+------------------+-------------+--------+ | 552bc246-5ae7-4b48-9a64-e1e881a64cab | CentOS 6 32-bit | raw | bare | 219152384 | active | | 708a7642-80ab-486e-a031-e6b6a652004c | CentOS 6 32-bit | raw | bare | 2147483648 | active | | 978d81c0-c508-412d-9847-fb8cec294410 | CentOS 6 64-bit | raw | bare | 263192576 | active | | ee5d5bb7-8a31-467a-8bbf-f6f5bbb79334 | CentOS 6 64-bit | raw | bare | 2147483648 | active | | 647bce00-5f29-49fe-9e83-8b33cb188d17 | CentOS 7 64-bit | raw | bare | 2147483648 | active | | dff9df74-b7b3-44b0-92f3-40cb4dfd9a94 | CoreOS | qcow2 | ovf | 449839104 | active | | 3eda89b9-9ce0-47b7-9907-a2978d88632e | CoreOS | qcow2 | ovf | 413007872 | active | | d2033c50-e8f4-4ff6-9c21-cade02007f34 | Debian 7 (Wheezy) 32-bit | raw | bare | 10485760 | active | | ba78ce9b-f800-4fb2-ad85-a68ca0f19cb8 | Debian 7 (Wheezy) 32-bit | raw | bare | 2147483648 | active | | b2c8bc6a-dbb8-4a1a-ab8e-c63f5f2b9bdf | Debian 7 (Wheezy) 64-bit | raw | bare | 11534336 | active | | 18a18569-389c-4144-82ae-e5e85862fca4 | Debian 7 (Wheezy) 64-bit | raw | bare | 2147483648 | active | | 8c3233c9-25cd-4181-a422-aa24032255cc | OpenSUSE 13.1 32-bit | raw | bare | 74448896 | active | | d965d37c-6796-40bd-8966-d0d7f7f41313 | OpenSUSE 13.1 32-bit | raw | bare | 3221225472 | active | | b77015d0-3eba-4841-9d02-7e9d606d343a | OpenSUSE 13.1 64-bit | raw | bare | 76546048 | active | | b20a1e1a-3c81-4d13-926f-eb39546b9b36 | OpenSUSE 13.1 64-bit | raw | bare | 3221225472 | active | | c168e0e5-c01e-44ec-be36-1c10e2da94a5 | selectel-rescue-initrd | ari | ari | 13665966 | active | | 0b117761-4ab5-40d7-a610-127d1e10206f | selectel-rescue-kernel | aki | aki | 5634192 | active | | c2fce974-4aeb-473a-9475-176207c3f293 | Ubuntu 12.04 LTS 32-bit | raw | bare | 22020096 | active | | eeb9143c-1500-4086-8025-307bc96fc467 | Ubuntu 12.04 LTS 32-bit | raw | bare | 2147483648 | active | | dbdd5cb3-f73f-4d98-85e9-eb333463e431 | Ubuntu 12.04 LTS 64-bit | raw | bare | 26214400 | active | | c1231800-9423-4018-b138-af8860ea8239 | Ubuntu 12.04 LTS 64-bit | raw | bare | 2147483648 | active | | c61cfa0d-3f7b-489f-8e55-4904a0d6e830 | Ubuntu 14.04 LTS 32-bit | raw | bare | 26214400 | active | | fbb2bb25-5058-4f06-85c8-6d3ca268e686 | Ubuntu 14.04 LTS 32-bit | raw | bare | 2147483648 | active | | e024042b-80f5-4eea-ae29-733ae32f65e6 | Ubuntu 14.04 LTS 64-bit | raw | bare | 33554432 | active | | f10ab2a9-478d-4401-9371-384bd9731156 | Ubuntu 14.04 LTS 64-bit | raw | bare | 2147483648 | active | | 6a4b53e6-109c-4fc0-9535-b97bc2912de6 | windows_2012_final | raw | bare | 10737418240 | active | +--------------------------------------+--------------------------+-------------+------------------+-------------+--------+ 


Select the desired image, copy its id - it will be needed when creating the server.
Now we will create the required server configuration (in OpenStac terminology, they are denoted by the term flavor, which is difficult to translate into Russian):

 $ nova flavor-create < > auto < > <  > < > 

The size of the hard disk in this command should be set to zero. In our implementation, as a root (system) disk, the Cinder volume is attached to the machine. This solution was chosen for reasons of flexibility: unlike local disks (in Amazon’s terminology, they are called instance store), Cinder volumes can be disconnected and attached to other machines.

The auto key in this command means that the server configuration ID will be generated automatically:

 +------------------------------------+------+---------+----+---------+----+-----+-----------+---------+ |ID |Name |Memory,MB|Disk|Ephemeral|Swap|VCPUs|RXTX_Factor|Is_Public| +------------------------------------+------+---------+----+---------+----+-----+-----------+---------+ |fc275dcc-f51a-48c3-b0c3-c3fdd300dd65|myflvr| 1024 | 0 | 0 | | 2 | 1.0 | True | +------------------------------------+------+---------+----+---------+----+-----+-----------+---------+ 


ID must be copied - it will also be needed when creating the server.
Then create an SSH key:

 $ nova keypair-add < > < > $ chmod 600 

This command will output a private key to the file, with which you can connect to virtual machines via SSH (the connection command in this case will look like this: ssh -i <file name> <server IP address>).

If you already have the private key-public key pair, you can specify the public key as an argument:

 $ nova keypair-add < > pub-key <   > 

For example:

 $ nova keypair-add myKey --pub-key /home/user/.ssh/id_rsa.pub 

You can also add SSH keys from the control panel (the Access tab in the project properties).

After selecting the image and configuration, we proceed directly to the creation of the server:

 $ nova boot < > <flavor_id> --nic net-id=<id > --key-name myKey --block-device id=<image_id>,source=image,dest=volume,size=0,device=vda,bootindex=0 

After the boot command, we indicate the server name, then the selected image ID, configuration ID, and SSH key.

When creating our control panel and images, we tried to exclude the possibility of transmitting passwords in an unencrypted form over the network. That is why our images do not accept the password, but its hash generated by the control panel.

To access the machine from the console, it will be enough to enter a login and press Enter. SSH login will be possible only with a key.

Disk operations


You can create a new disk and connect it to the server using the command:

 $ cinder create --name < > <, > 

In case of successful disk creation, a table with its main characteristics is displayed on the console:
 +-------------------+--------------------------------------+ | Property | Value | +-------------------+--------------------------------------+ | attachments | [] | | availability_zone | ru-1a | | bootable | false | | created_at | 2014-10-23T11:10:15.000000 | | description | None | | encrypted | False | | id | 76586803-9cfd-4f75-931d-0a4dee98e496 | | metadata | {} | | name | mydisk | | size | 5 | | snapshot_id | None | | source_volid | None | | status | creating | | user_id | 6f862e43d4a84f359928948fb658d695 | | volume_type | default | +-------------------+--------------------------------------+ 

To connect the disk to the server, copy the id parameter from this table and execute the command:

 $ nova volume-attach < > <id > 

Creating and Assigning an IP Address


To create an external IP address where the server will be accessible from the Internet, run the following command:

 $ neutron floatingip-create external-network 

Assign the address to the server:

 $ nova floating-ip-associate < > <> 

Power Management and Reboot


There are two ways to restart the server: software and hardware (via power-off) reboot.

For a soft reset, use the following command:

 $ nova reboot < > 

and for hardware -

 $ nova reboot --hard < > 

Power management is performed using the start and stop commands:

 #   $ nova start < > #   $ nova stop < > 

Network Port Operations


Create a new network:

 $ neutron net-create < > 


Upon execution of the command, the table with the network information will be displayed on the console:
 +----------------+--------------------------------------+ | Field | Value | +----------------+--------------------------------------+ | admin_state_up | True | | id | add73ca5-6120-43bd-bb56-d1d8d71d21ac | | name | localnet | | shared | False | | status | ACTIVE | | subnets | | | tenant_id | d15391cc95474b1ab6bd81fb2a73bc5c | +----------------+--------------------------------------+ 

You can create a subnet on this network using the following command:

 $ neutron subnet-create --name 192.168.1.0/24 192.168.1.0/24 <id > 


(we take the network id from the output of the previous command).

Then create a port on the network:

 $ neutron port-create < > 

and connect the server to it:

 $ nova interface-attach --port-id <id > < > 

Conclusion


This article is just a brief introduction to the OpenStack API. If you have questions about working with console clients, we will answer them in the comments.

In more detail, the features of interaction with the API will be discussed in the following publications.

Readers who for one reason or another can not leave comments here are invited to our blog .

Source: https://habr.com/ru/post/242001/


All Articles