launch.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. Launching instances
  2. ===================
  3. Before being able to run below commands, you will need a ``provider`` object
  4. (see `this page <setup.html>`_).
  5. Common launch data
  6. ------------------
  7. Before launching an instance, you need to decide what image to launch
  8. as well as what type of instance. We will create those objects here. The
  9. specified image ID is a base Ubuntu image on AWS so feel free to change it as
  10. desired. For instance type, we're going to let CloudBridge figure out what's
  11. the appropriate name on a given provider for an instance with at least 2 CPUs
  12. and 4 GB RAM.
  13. .. code-block:: python
  14. img = provider.compute.images.get('ami-759bc50a') # Ubuntu 16.04 on AWS
  15. vm_type = sorted([t for t in provider.compute.vm_types
  16. if t.vcpus >= 2 and t.ram >= 4],
  17. key=lambda x: x.vcpus*x.ram)[0]
  18. In addition, CloudBridge instances must be launched into a private subnet.
  19. While it is possible to create complex network configurations as shown in the
  20. `Private networking`_ section, if you don't particularly care in which subnet
  21. the instance is launched, CloudBridge provides a convenience function to
  22. quickly obtain a default subnet for use. We just need to supply a zone to use.
  23. .. code-block:: python
  24. zone = provider.compute.regions.get(provider.region_name).zones[0]
  25. subnet = provider.networking.subnets.get_or_create_default(zone)
  26. When launching an instance, you can also specify several optional arguments
  27. such as the firewall (aka security group), a key pair, or instance user data.
  28. To allow you to connect to the launched instances, we will also supply those
  29. parameters (note that we're making an assumption here these resources exist;
  30. if you don't have those resources under your account, take a look at the
  31. `Getting Started <../getting_started.html>`_ guide).
  32. .. code-block:: python
  33. kp = provider.security.key_pairs.find(name='cloudbridge-intro')[0]
  34. fw = provider.security.vm_firewalls.list()[0]
  35. Launch an instance
  36. ------------------
  37. Once we have all the desired pieces, we'll use them to launch an instance:
  38. .. code-block:: python
  39. inst = provider.compute.instances.create(
  40. name='cloudbridge-vpc', image=img, vm_type=vm_type,
  41. subnet=subnet, zone=zone, key_pair=kp, vm_firewalls=[fw])
  42. Private networking
  43. ~~~~~~~~~~~~~~~~~~
  44. Private networking gives you control over the networking setup for your
  45. instance(s) and is considered the preferred method for launching instances. To
  46. launch an instance with an explicit private network, you can create a custom
  47. network and make sure it has internet connectivity. You can then launch into
  48. that subnet.
  49. .. code-block:: python
  50. net = self.provider.networking.networks.create(
  51. name='my-network', cidr_block='10.0.0.0/16')
  52. sn = net.create_subnet(name='my-subnet', cidr_block='10.0.0.0/28')
  53. # make sure subnet has internet access
  54. router = self.provider.networking.routers.create(network=net, name='my-router')
  55. router.attach_subnet(sn)
  56. gateway = net.gateways.get_or_create_inet_gateway()
  57. router.attach_gateway(gateway)
  58. inst = provider.compute.instances.create(
  59. name='cloudbridge-vpc', image=img, vm_type=vm_type,
  60. subnet=sn, zone=zone, key_pair=kp, vm_firewalls=[fw])
  61. For more information on how to create and setup a private network, take a look
  62. at `Networking <./networking.html>`_.
  63. Block device mapping
  64. ~~~~~~~~~~~~~~~~~~~~
  65. Optionally, you may want to provide a block device mapping at launch,
  66. specifying volume or ephemeral storage mappings for the instance. While volumes
  67. can also be attached and mapped after instance boot using the volume service,
  68. specifying block device mappings at launch time is especially useful when it is
  69. necessary to resize the root volume.
  70. The code below demonstrates how to resize the root volume. For more information,
  71. refer to :class:`.LaunchConfig`.
  72. .. code-block:: python
  73. lc = provider.compute.instances.create_launch_config()
  74. lc.add_volume_device(source=img, size=11, is_root=True)
  75. inst = provider.compute.instances.create(
  76. name='cloudbridge-bdm', image=img, vm_type=vm_type,
  77. launch_config=lc, key_pair=kp, vm_firewalls=[fw],
  78. subnet=subnet, zone=zone)
  79. where ``img`` is the :class:`.Image` object to use for the root volume.
  80. After launch
  81. ------------
  82. After an instance has launched, you can access its properties:
  83. .. code-block:: python
  84. # Wait until ready
  85. inst.wait_till_ready() # This is a blocking call
  86. inst.state
  87. # 'running'
  88. Depending on the provider's networking setup, it may be necessary to explicitly
  89. assign a floating IP address to your instance. This can be done as follows:
  90. .. code-block:: python
  91. # Create a new floating IP address
  92. fip = provider.networking.floating_ips.create()
  93. # Assign the desired IP to the instance
  94. inst.add_floating_ip(fip)
  95. inst.refresh()
  96. inst.public_ips
  97. # [u'149.165.168.143']