如何通过OpenStack命令行管理VMs

I assume that there is an OpenStack deployment already up and running somewhere. I am going to use OpenStack CLI clients to manage VMs on the existing OpenStack setup.

Install OpenStack CLI Clients

The first step is to install necessary OpenStack command line clients.

On Debian, Ubuntu or Linux Mint:

$ sudo apt-get install python-pip
$ sudo pip install python-novaclient python-keystoneclient python-neutronclient

On CentOS, Fedora or RHEL:

$ sudo yum install python-pip
$ sudo pip install python-novaclient python-keystoneclient python-neutronclient

Set Environment Variables

In order to be able to use OpenStack CLI clients, you need to set up necessary environment variables. For this purpose, create a script named openrc.sh as follows.

$ sudo vi openrc.sh
export OS_USERNAME=dan
export OS_PASSWORD=my_password
export OS_TENANT_NAME=demo
export OS_AUTH_URL=http://192.168.10.10:5000/v2.0

In the above, OS_USERNAME/OS_PASSWORD are OpenStack user name and its password. OS_TENANT_NAME is the name of the project created. OS_AUTH_URL is the URL of the Keystone endpoint. Keystone is an OpenStack service responsible for authentication among different OpenStack components. You should replace 192.168.10.10 with the IP address of the host where OpenStack Keystone is running in your deployment.

Now, set the environment variables by running openrc.sh as follows.

$ source openrc.sh

At this point, you are ready to run OpenStack command line clients. Verify that you do not encounter any error when running:

$ nova list

It shows an empty result, it means everything is okay.

Create a Network

Now you are ready to create a network to connect your VMs to.

Create a new network named "xmodulo".

$ neutron net-create xmodulo

如何通过OpenStack命令行管理VMs_第1张图片

Create a new subnet named "xmodulo_subnet", and add it to the network just created.

$ neutron subnet-create xmodulo 10.0.0.0/24 --name xmodulo_subnet

如何通过OpenStack命令行管理VMs_第2张图片

Check a list of available networks to verify that the network has been created successfully.

$ neutron net-list

如何通过OpenStack命令行管理VMs_第3张图片

In the output, make a note of the "id" of the network you created. This id will be used when you create a VM later.

Launch a VM

Before creating and launch a VM, you need to know a few pieces of information first.

Find out the type of VM that you want to create. For that, run the following command.

$ nova flavor-list

如何通过OpenStack命令行管理VMs_第4张图片

In this example, I am going to choose the smallest VM type "m1.nano", which has 64 MB, 1 vCPU and no disk. Make a note of this type name.

Next, choose the VM image to use for your VM. To get a list of all available VM images, use this command:

$ nova image-list

如何通过OpenStack命令行管理VMs_第5张图片

Make a note of the ID of the image that you want to use for your VM.

Next, choose the type of security group to use for your VM. A security group determines inbound access rules for your VM. To find out available security groups:

$ nova secgroup-list

To check the access rules of the "default" security group, use this command:

$ nova secgroup-list-rules default

如何通过OpenStack命令行管理VMs_第6张图片

In this example, I am going to choose a security group named "default" for the VM. This security group happens to have no rule in it.

Finally, let's create a VM using the information obtained thus far. Specify the VM type (--flavor), ID of VM image (--image), ID of a network (net-id=) that you have found out. Replace [vm-name] with the name of your VM, which needs to be unique.

$ nova boot [vm-name] --flavor m1.nano --image d2b830be-37df-4fa9-90b2-91c472d19aaa --security-groups default --nic net-id=1cbcddcf-3a7d-481f-b6f2-a97c6447c925

如何通过OpenStack命令行管理VMs_第7张图片

To verify that the VM has been successfully created and launched, run this command:

$ nova list

如何通过OpenStack命令行管理VMs_第8张图片

Stop, Suspend or Purge a VM

When you stop a running VM, it gets shutdown completely. On the other hand, when you suspend a VM, it is temporarily frozen, and can be restarted from the suspende state any time. In both cases, the VM image still remains in OpenStack.

To stop a VM:

$ nova stop [vm-name]

To suspend a VM:

$ nova suspend [vm-name]

如何通过OpenStack命令行管理VMs_第9张图片

If you want to purge the image of either stopped or suspended VM from OpenStack, use the command:

$ nova delete [vm-name]

如何通过OpenStack命令行管理VMs_第10张图片

你可能感兴趣的:(如何通过OpenStack命令行管理VMs)