SME OpenStack Private Cloud Deployment Practice "19 manually specifying a fixed IP address when launching an instance from a Python client"

Source: Internet
Author: User

another way to create is to use the PY development tool, call the Openstackclient method to create an instance, the advantage is that we can arbitrarily specify the virtual machine IP address We want, the demand scenario is that one day we need to actively allocate a more well-known IP as a service componentor one day, you need to pack a snapshot of an instance, and after terminating the instance, you want to restore it to the original IP using a snapshot, you can use the following methods.  take the Win7 client as an example. Download install eclipse installation Firstinstall Eclipse's Python plugin, plug-in installation method can be self-Baiduthis machine, install python2.7 environmentafter installation, add Python to the system environment variable. Because we're going to use its cmd command line to download and install Openstackclient, the environment variable method can be self-Baidu Download the Python-pip tool plugin,unzip to the Python installation directory.  then enter the PIP directory, in the blank, press and hold shift+ right mouse button, select "Open Command Window Here" PIP installation openstackclientpip Install openstackclientafter the installation of the basic Openstackclient end is completed, the window can be closed directly, follow-up we debug on Eclipse on the call.   run Eclipse, create a new Python project, build a Pydev project you can write the project template that invokes the API. For example, I created a test.py module under my workspace directory openstackdashboad.   Manually create a dual-NIC ip instance script as follows, is also the reference website to provide, slightly modified, as follows I have some light blue Note, copy when remember to put < pink comments > can be deleted.  # Coding:utf-8Import TimeImport SYSFrom novaclient.client Import client  Reload (SYS)sys.setdefaultencoding ("Utf-8") def print_values_ip(ip_list):IP_DICT_LISL = []For IP in ip_list:print ("-" *35)print ("fixed_ip:%s"% ip.fixed_ip)print ("ID:%s"% ip.id)print ("instance_id:%s"% ip.instance_id)print ("IP:%s"% Ip.ip)print ("Pool:%s"% Ip.pool) def get_nova_credentials_v2():d = {}d[' Version ' = ' 2 'd[' username ' = "Demo"#像我们登录dashboard一样, to user named[' api_key ' = "Your tenant password"#像我们登录dashboard一样, to password authenticationd[' auth_url '] = "http://controller:5000/v2.0"#像我们平时登录dashboard一样, fill in the login addressd[' project_id ' = "Demo"#项目名称d[' region_name '] = "Regionone"#区域名称return D def print_hosts(host_list):For host in Host_list:print ("-" *35)print ("host_name:%s"% host.host_name)Print ("Service:%s"% host.service)Print ("zone:%s"% host.zone)print ("-" *35) if __name__ = = ' __main__ ':credentials = Get_nova_credentials_v2 ()nova_client = Client (**credentials)serviers = nova_client.servers.list ()# Print (serviers)image = Nova_client.images.find (name= "20G_CENTOS7_NOLVM")#这个是我们做好上传的centos7镜像名称, such as Cirros on the official web, can be a mirror name or a snapshot nameflavor = Nova_client.flavors.find (name= "c2-m4g-d20g")#这个是我们要的创建的主机类型和大小名称, like Tiny\small\large on the official web.net = Nova_client.networks.find (label= "1040100")#这个是我们定义好的, the name of the public network segment, such as the official Commons, which is the main declarative method for parsing to publicly get its net-id, and attached to the netNet2 = Nova_client.networks.find (label= "17216100")#这个是我们定义好的, the name of the private segment, such as the official private, the main declaration method is to parse to private to get its net-id, and attached to Net2nics = [{' Net-id ': net.id, ' v4-fixed-ip ': ' 10.40.100.27 '},{' Net-id ': net2.id, ' v4-fixed-ip ': ' 172.16.100.27 '}] #这个是我们要为net和net2手工指定的双网卡IP地址instance = nova_client.servers.create (name= "Vm27", Image=image,flavor=flavor, availability_zone= "Stg_zone", Nic S=nics)#vm27这个是为虚拟机指定一个主机名称, "Stg_zone" is where we assign the host to the designated zone zone. print ("Sleeping for 5s after create command")Time.sleep (5)print ("List of VMs")print (Nova_client.servers.list ())   after configuring, saving, running, and noticing that your IP is not previously assigned to Izumo, view the virtual state through the dashboard interface. Manually create a single-Nic ip instance script as follows# Coding:utf-8Import TimeImport SYSFrom novaclient.client Import client Reload (SYS)sys.setdefaultencoding ("Utf-8") def print_values_ip (ip_list):IP_DICT_LISL = []For IP in ip_list:print ("-" *35)print ("fixed_ip:%s"% ip.fixed_ip)print ("ID:%s"% ip.id)print ("instance_id:%s"% ip.instance_id)print ("IP:%s"% Ip.ip)print ("Pool:%s"% Ip.pool) def get_nova_credentials_v2 ():d = {}d[' Version ' = ' 2 'd[' username ' = "Demo"d[' api_key ' = "Your tenant password"d[' auth_url '] = "http://controller:5000/v2.0"d[' project_id ' = "Demo"d[' region_name '] = "Regionone"return D def print_hosts (host_list):For host in Host_list:print ("-" *35)print ("host_name:%s"% host.host_name)Print ("Service:%s"% host.service)Print ("zone:%s"% host.zone)print ("-" *35) if __name__ = = ' __main__ ':credentials = Get_nova_credentials_v2 ()nova_client = Client (**credentials)serviers = nova_client.servers.list ()# Print (serviers)image = Nova_client.images.find (name= "20G_CENTOS7.1_NOLVM")flavor = Nova_client.flavors.find (name= "c2-m4g-d20g")net = Nova_client.networks.find (label= "10120100")nics = [{' Net-id ': net.id, ' v4-fixed-ip ': ' 10.40.150.150 '}]instance = nova_client.servers.create (name= "Zabbixserver", Image=image,flavor=flavor, availability_zone= "Stg_ Zone ", Nics=nics)print ("Sleeping for 5s after create command")Time.sleep (5)print ("List of VMs")print (Nova_client.servers.list ())

SME OpenStack Private Cloud Deployment Practice "19 manually specifying a fixed IP address when launching an instance from a Python client"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.