launch.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 instance, 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') # Ubuntu 14.04 on AWS
  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. Private networking setup
  26. ------------------------
  27. Private networking gives you control over the networking setup for your
  28. instance(s) and is considered the preferred method for launching instances.
  29. Create a new private network
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. To start, we will create a private network and a corresponding subnet into
  32. which an instance will be launched. When creating the subnet, we need to
  33. set the address pool. For OpenStack, any address pool is acceptable while for
  34. the AWS cloud, the subnet address pool needs to belong to the private network
  35. address space; we can obtain the private network address space via
  36. network object's ``cidr_block`` field (e.g., ``10.0.0.0/16``). Let's crate a
  37. subnet starting from the beginning of the block and allow up to 32 IP addresses
  38. into the subnet (``/27``):
  39. .. code-block:: python
  40. net = provider.network.create(name="CloudBridge-net")
  41. net.cidr_block # '10.0.0.0/16'
  42. sn = net.create_subnet('10.0.0.1/27', "CloudBridge-subnet")
  43. Note that it may be necessary to also create a route for this new network. If
  44. that's the case, take a look at the
  45. `Getting Started <../getting_started.html>`_ document for an example.
  46. Retrieve an existing private network
  47. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. If you already have existing networks, we can simply reuse an existing one:
  49. .. code-block:: python
  50. provider.network.list() # Find a desired network ID
  51. net = provider.network.get('desired network ID')
  52. sn = net.subnets()[0] # Get a handle on a desired subnet
  53. Launch an instance
  54. ------------------
  55. Once we have a handle on a private network, we'll define a launch configuration
  56. object to aggregate all the launch configuration options. The launch config
  57. can contain other launch options, such as the block storage mappings (see
  58. below). Finally, we can launch the instance:
  59. .. code-block:: python
  60. lc = provider.compute.instances.create_launch_config()
  61. lc.add_network_interface(net.id)
  62. inst = provider.compute.instances.create(
  63. name='CloudBridge-VPC', image=img, instance_type=inst_type,
  64. launch_config=lc, key_pair=kp, security_groups=[sg])
  65. .. warning::
  66. CloudBridge version 0.1.0 does not uniformly deal with network abstractions
  67. for AWS and OpenStack providers. AWS takes a subnet ID for it's launch
  68. config while OpenStack takes a network ID. As a result, the user needs to
  69. make this distinction in their code and supply the correct value. For
  70. example, for AWS, above code needs to look like the following:
  71. ``lc.add_network_interface(sn.id)``. This has been corrected in newer code.
  72. Launch with default networking
  73. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. Launching an instance with the default networking model is straightforward,
  75. only needing to specify the basic parameters. This will only work for the case
  76. a default network exists for your account, which is provider-dependent and may
  77. not necessarily exist.
  78. For the case of AWS, an instance will be launched into the VPC where the
  79. specified security group belongs to. If no security group is specified, the
  80. instance will get launched into the *default* VPC, assuming such VPC exists.
  81. .. code-block:: python
  82. inst = provider.compute.instances.create(
  83. name='CloudBridge-basic', image=img, instance_type=inst_type,
  84. key_pair=kp, security_groups=[sg])
  85. Block device mapping
  86. ~~~~~~~~~~~~~~~~~~~~
  87. Optionally, you may want to provide a block device mapping at launch,
  88. specifying volume or ephemeral storage mappings for the instance. While volumes
  89. can also be attached and mapped after instance boot using the volume service,
  90. specifying block device mappings at launch time is especially useful when it is
  91. necessary to resize the root volume.
  92. The code below demonstrates how to resize the root volume. For more information,
  93. refer to :class:`.LaunchConfig`.
  94. .. code-block:: python
  95. lc = provider.compute.instances.create_launch_config()
  96. lc.add_volume_device(source=img, size=11, is_root=True)
  97. inst = provider.compute.instances.create(
  98. name='CloudBridge-BDM', image=img, instance_type=inst_type,
  99. launch_config=lc, key_pair=kp, security_groups=[sg])
  100. where ``img`` is the :class:`.Image` object to use for the root volume.
  101. After launch
  102. ------------
  103. After an instance has launched, you can access its properties:
  104. .. code-block:: python
  105. # Wait until ready
  106. inst.wait_till_ready() # This is a blocking call
  107. inst.state
  108. # 'running'
  109. Depending on the provider's networking setup, it may be necessary to explicitly
  110. assign a floating IP address to your instance. This can be done as follows:
  111. .. code-block:: python
  112. # List all the IP addresses and find the desired one
  113. provider.network.floating_ips()
  114. # Assign the desired IP to the instance
  115. inst.add_floating_ip('149.165.168.143')
  116. inst.refresh()
  117. inst.public_ips
  118. # [u'149.165.168.143']