launch.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. Launching instances
  2. ===================
  3. Depending on the cloud provider, instances can be launched using
  4. software-managed networking (e.g., VPC on AWS, Neutron on OpenStack) or the
  5. classic networking approach. Before being able to run below command, you will
  6. need a ``provider`` object (see `this page <setup.html>`_).
  7. Common launch data
  8. ------------------
  9. Before launching an instane, you need to decide on what image to launch
  10. as well as what type of instance. We will create those objects here are use
  11. them in both options below. The specified image ID is a base Ubuntu image on
  12. AWS so feel free to change it as desired.
  13. .. code-block:: python
  14. img = provider.compute.images.get('ami-d85e75b0')
  15. inst_type = provider.compute.instance_types.find(name='m1.small')[0]
  16. When launching an instance, you can also specify several optional arguments
  17. such as the security group, a key pair, or instance user data. To allow you to
  18. connect to the launched instances, we will also supply those parameters (note
  19. that we're making an assumption here these resources exist; if you don't have
  20. those resources, take a look at the `Getting Started <../getting_started.html>`_
  21. guide).
  22. .. code-block:: python
  23. kp = provider.security.key_pairs.find(name='cloudbridge_intro')[0]
  24. sg = provider.security.security_groups.list()[0]
  25. Launch with classic networking
  26. ------------------------------
  27. Launching an instance with the traditional networking model is straighforward,
  28. only needing to specify the basic parameters:
  29. .. code-block:: python
  30. inst = provider.compute.instances.create(
  31. name='Cloudbridge-basic', image=img, instance_type=inst_type,
  32. key_pair=kp, security_groups=[sg])
  33. Launch with private networking
  34. ------------------------------
  35. To start, we will create a private network and a corresponding subnet into
  36. which an instance will be launched. When creating the subnet, we need to
  37. set the address pool. For the AWS cloud, the subnet address pool needs to
  38. belong to the private network address space; for OpenStack, any address pool
  39. is acceptable. On AWS, we can obtain the private network address space via
  40. network object's ``cidr_block`` field (e.g., ``10.0.0.0/16``). Let's crate a
  41. subnet starting from the beginning of the block and allow up to 32 IP addresses
  42. into the subnet (``/27``):
  43. .. code-block:: python
  44. net = provider.network.create(name="Cloudbridge-net")
  45. net.cidr_block # '10.0.0.0/16'
  46. sn = net.create_subnet('10.0.0.1/27', "Cloudbridge-subnet")
  47. Once we hace created a private network, we'll define a launch configuration
  48. object to aggregate all the launch configuration options. The launch config
  49. can contain other launch options, such as the block storage mappings (see
  50. below). Finally, we can launch the instance:
  51. .. code-block:: python
  52. lc = provider.compute.instances.create_launch_config()
  53. lc.add_network_interface(sn.id)
  54. inst = provider.compute.instances.create(
  55. name='Cloudbridge-VPC', image=img, instance_type=inst_type,
  56. launch_config=lc, key_pair=kp, security_groups=[sg])
  57. Block device mapping
  58. --------------------
  59. Optionally, you may want to provide a block device mapping at launch,
  60. specifying volume or ephemeral storage mappings for the instance. While volumes
  61. can also be attached and mapped after instance boot using the volume service,
  62. specifying block device mappings at launch time is especially useful when it is
  63. necessary to resize the root volume.
  64. The code below demonstrates how to resize the root volume. For more information,
  65. refer to :class:`.LaunchConfig`.
  66. .. code-block:: python
  67. lc = provider.compute.instances.create_launch_config()
  68. lc.add_volume_device(source=img, size=11, is_root=True)
  69. inst = provider.compute.instances.create(
  70. name='Cloudbridge-BDM', image=img, instance_type=inst_type,
  71. launch_config=lc, key_pair=kp, security_groups=[sg])
  72. where img is the :class:`.Image` object to use for the root volume.
  73. After an instance has launched, you can access its properties:
  74. .. code-block:: python
  75. # Wait until ready
  76. inst.wait_till_ready()
  77. inst.state
  78. # 'running'
  79. inst.public_ips
  80. # [u'54.166.125.219']