launch.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. Retrieve an existing private network
  44. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. If you already have existing networks, we can simply reuse an existing one:
  46. .. code-block:: python
  47. provider.network.list() # Find a desired network ID
  48. net = provider.network.get('network ID')
  49. sn = net.subnets()[0] # Get a handle on desired subnet
  50. Launch an instance
  51. ------------------
  52. Once we have a handle on a private network, we'll define a launch configuration
  53. object to aggregate all the launch configuration options. The launch config
  54. can contain other launch options, such as the block storage mappings (see
  55. below). Finally, we can launch the instance:
  56. .. code-block:: python
  57. lc = provider.compute.instances.create_launch_config()
  58. lc.add_network_interface(sn.id)
  59. inst = provider.compute.instances.create(
  60. name='CloudBridge-VPC', image=img, instance_type=inst_type,
  61. launch_config=lc, key_pair=kp, security_groups=[sg])
  62. .. warning::
  63. There is still a problem with network abstractions in CloudBridge between
  64. AWS and OpenStack providers. AWS takes a subnet ID for it's launch config
  65. while OpenStack takes a network ID. For the time being, the user needs to
  66. make this distinction in their code and supply the correct value.
  67. For example, for OpenStack, above code needs to look like the following:
  68. ``lc.add_network_interface(net.id)``.
  69. Launch with default networking
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. Launching an instance with the default networking model is straightforward,
  72. only needing to specify the basic parameters. This will only work for the case
  73. a default network exists for your account, which is provider-dependent and may
  74. not necessarily exist.
  75. For the case of AWS, an instance will be launched into the VPC where the
  76. specified security group belongs to. If no security group is specified, the
  77. instance will get launched into the *default* VPC, assuming such VPC exists.
  78. .. code-block:: python
  79. inst = provider.compute.instances.create(
  80. name='CloudBridge-basic', image=img, instance_type=inst_type,
  81. key_pair=kp, security_groups=[sg])
  82. Block device mapping
  83. ~~~~~~~~~~~~~~~~~~~~
  84. Optionally, you may want to provide a block device mapping at launch,
  85. specifying volume or ephemeral storage mappings for the instance. While volumes
  86. can also be attached and mapped after instance boot using the volume service,
  87. specifying block device mappings at launch time is especially useful when it is
  88. necessary to resize the root volume.
  89. The code below demonstrates how to resize the root volume. For more information,
  90. refer to :class:`.LaunchConfig`.
  91. .. code-block:: python
  92. lc = provider.compute.instances.create_launch_config()
  93. lc.add_volume_device(source=img, size=11, is_root=True)
  94. inst = provider.compute.instances.create(
  95. name='CloudBridge-BDM', image=img, instance_type=inst_type,
  96. launch_config=lc, key_pair=kp, security_groups=[sg])
  97. where ``img`` is the :class:`.Image` object to use for the root volume.
  98. After launch
  99. ------------
  100. After an instance has launched, you can access its properties:
  101. .. code-block:: python
  102. # Wait until ready
  103. inst.wait_till_ready()
  104. inst.state
  105. # 'running'
  106. inst.public_ips
  107. # [u'54.166.125.219']
  108. Depending on the provider's networking setup, it may be necessary to explicitly
  109. assign a floating IP address to your instance. This can be done as follows:
  110. .. code-block:: python
  111. # List all the IP addresses and find the desired one
  112. provider.network.floating_ips()
  113. # Assign the IP to the instance
  114. inst.add_floating_ip('149.165.168.143')