2
0

launch.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.
  23. .. code-block:: python
  24. subnet = provider.networking.subnets.get_or_create_default()
  25. When launching an instance, you can also specify several optional arguments
  26. such as the firewall (aka security group), a key pair, or instance user data.
  27. To allow you to connect to the launched instances, we will also supply those
  28. parameters (note that we're making an assumption here these resources exist;
  29. if you don't have those resources under your account, take a look at the
  30. `Getting Started <../getting_started.html>`_ guide).
  31. .. code-block:: python
  32. kp = provider.security.key_pairs.find(name='cloudbridge-intro')[0]
  33. fw = provider.security.vm_firewalls.list()[0]
  34. Launch an instance
  35. ------------------
  36. Once we have all the desired pieces, we'll use them to launch an instance.
  37. Note that the instance is launched in the provider's default region and zone,
  38. and can be overridden by changing the provider config.
  39. .. code-block:: python
  40. inst = provider.compute.instances.create(
  41. label='cloudbridge-vpc', image=img, vm_type=vm_type,
  42. subnet=subnet, key_pair=kp, vm_firewalls=[fw])
  43. Private networking
  44. ~~~~~~~~~~~~~~~~~~
  45. Private networking gives you control over the networking setup for your
  46. instance(s) and is considered the preferred method for launching instances. To
  47. launch an instance with an explicit private network, you can create a custom
  48. network and make sure it has internet connectivity. You can then launch into
  49. that subnet.
  50. .. code-block:: python
  51. net = self.provider.networking.networks.create(
  52. label='my-network', cidr_block='10.0.0.0/16')
  53. sn = net.subnets.create(label='my-subnet', cidr_block='10.0.0.0/28')
  54. # make sure subnet has internet access
  55. router = self.provider.networking.routers.create(label='my-router', network=net)
  56. router.attach_subnet(sn)
  57. gateway = net.gateways.get_or_create()
  58. router.attach_gateway(gateway)
  59. inst = provider.compute.instances.create(
  60. label='cloudbridge-vpc', image=img, vm_type=vm_type,
  61. subnet=sn, key_pair=kp, vm_firewalls=[fw])
  62. For more information on how to create and setup a private network, take a look
  63. at `Networking <./networking.html>`_.
  64. Block device mapping
  65. ~~~~~~~~~~~~~~~~~~~~
  66. Optionally, you may want to provide a block device mapping at launch,
  67. specifying volume or ephemeral storage mappings for the instance. While volumes
  68. can also be attached and mapped after instance boot using the volume service,
  69. specifying block device mappings at launch time is especially useful when it is
  70. necessary to resize the root volume.
  71. The code below demonstrates how to resize the root volume. For more information,
  72. refer to :class:`.LaunchConfig`.
  73. .. code-block:: python
  74. lc = provider.compute.instances.create_launch_config()
  75. lc.add_volume_device(source=img, size=11, is_root=True)
  76. inst = provider.compute.instances.create(
  77. label='cloudbridge-bdm', image=img, vm_type=vm_type,
  78. launch_config=lc, key_pair=kp, vm_firewalls=[fw],
  79. subnet=subnet)
  80. where ``img`` is the :class:`.Image` object to use for the root volume.
  81. After launch
  82. ------------
  83. After an instance has launched, you can access its properties:
  84. .. code-block:: python
  85. # Wait until ready
  86. inst.wait_till_ready() # This is a blocking call
  87. inst.state
  88. # 'running'
  89. Depending on the provider's networking setup, it may be necessary to explicitly
  90. assign a floating IP address to your instance. This can be done as follows:
  91. .. code-block:: python
  92. # Create a new floating IP address
  93. fip = provider.networking.floating_ips.create()
  94. # Assign the desired IP to the instance
  95. inst.add_floating_ip(fip)
  96. inst.refresh()
  97. inst.public_ips
  98. # [u'149.165.168.143']