launch.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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-f4cc1de2') # Ubuntu 16.04 on AWS
  15. inst_type = sorted([t for t in provider.compute.instance_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 where the
  21. instance is launched, CloudBridge provides a convenience function to quickly
  22. 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 security group, a key pair, or instance user data. To allow you to
  27. connect to the launched instances, we will also supply those parameters (note
  28. that we're making an assumption here these resources exist; if you don't have
  29. 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. sg = provider.security.security_groups.list()[0]
  34. Launch an instance
  35. ------------------
  36. Once we have all the desired pieces, we'll use them to launch an instance:
  37. .. code-block:: python
  38. inst = provider.compute.instances.create(
  39. name='CloudBridge-VPC', image=img, instance_type=inst_type,
  40. subnet=subnet, key_pair=kp, security_groups=[sg])
  41. Private networking
  42. ~~~~~~~~~~~~~~~~~~
  43. Private networking gives you control over the networking setup for your
  44. instance(s) and is considered the preferred method for launching instances. To
  45. launch an instance with an explicit private network, you can create a custom
  46. network and make sure it has internet connectivity. You can then launch into
  47. that subnet.
  48. .. code-block:: python
  49. net = self.provider.networking.networks.create(
  50. name='my-network', cidr_block='10.0.0.0/16')
  51. sn = net.create_subnet(name='my-subnet', cidr_block='10.0.0.0/28')
  52. # make sure subnet has internet access
  53. router = self.provider.networking.routers.create(network=net, name='my-router')
  54. router.attach_subnet(sn)
  55. gateway = self.provider.networking.gateways.get_or_create_inet_gateway(name)
  56. router.attach_gateway(gateway)
  57. inst = provider.compute.instances.create(
  58. name='CloudBridge-VPC', image=img, instance_type=inst_type,
  59. subnet=sn, key_pair=kp, security_groups=[sg])
  60. For more information on how to create and setup a private network, take a look
  61. at `Networking <./networking.html>`_.
  62. Block device mapping
  63. ~~~~~~~~~~~~~~~~~~~~
  64. Optionally, you may want to provide a block device mapping at launch,
  65. specifying volume or ephemeral storage mappings for the instance. While volumes
  66. can also be attached and mapped after instance boot using the volume service,
  67. specifying block device mappings at launch time is especially useful when it is
  68. necessary to resize the root volume.
  69. The code below demonstrates how to resize the root volume. For more information,
  70. refer to :class:`.LaunchConfig`.
  71. .. code-block:: python
  72. lc = provider.compute.instances.create_launch_config()
  73. lc.add_volume_device(source=img, size=11, is_root=True)
  74. inst = provider.compute.instances.create(
  75. name='CloudBridge-BDM', image=img, instance_type=inst_type,
  76. launch_config=lc, key_pair=kp, security_groups=[sg])
  77. where ``img`` is the :class:`.Image` object to use for the root volume.
  78. After launch
  79. ------------
  80. After an instance has launched, you can access its properties:
  81. .. code-block:: python
  82. # Wait until ready
  83. inst.wait_till_ready() # This is a blocking call
  84. inst.state
  85. # 'running'
  86. Depending on the provider's networking setup, it may be necessary to explicitly
  87. assign a floating IP address to your instance. This can be done as follows:
  88. .. code-block:: python
  89. # List all the IP addresses and find the desired one
  90. provider.network.floating_ips()
  91. # Assign the desired IP to the instance
  92. inst.add_floating_ip('149.165.168.143')
  93. inst.refresh()
  94. inst.public_ips
  95. # [u'149.165.168.143']