cloud_controller.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. .. _cloud-controller:
  2. ==========================
  3. Salt as a Cloud Controller
  4. ==========================
  5. In Salt 0.14.0, an advanced cloud control system was introduced, allowing
  6. private cloud VMs to be managed directly with Salt. This system is generally
  7. referred to as :strong:`Salt Virt`.
  8. The Salt Virt system already exists and is installed within Salt itself. This
  9. means that besides setting up Salt, no additional salt code needs to be
  10. deployed.
  11. .. note::
  12. The ``libvirt`` python module and the ``certtool`` binary are required.
  13. The main goal of Salt Virt is to facilitate a very fast and simple cloud that
  14. can scale and is fully featured. Salt Virt comes with the ability to set up and
  15. manage complex virtual machine networking, powerful image and disk management,
  16. and virtual machine migration with and without shared storage.
  17. This means that Salt Virt can be used to create a cloud from a blade center
  18. and a SAN, but can also create a cloud out of a swarm of Linux Desktops
  19. without a single shared storage system. Salt Virt can make clouds from
  20. truly commodity hardware, but can also stand up the power of specialized
  21. hardware as well.
  22. Setting up Hypervisors
  23. ======================
  24. The first step to set up the hypervisors involves getting the correct software
  25. installed and setting up the hypervisor network interfaces.
  26. Installing Hypervisor Software
  27. ------------------------------
  28. Salt Virt is made to be hypervisor agnostic but currently, the only fully
  29. implemented hypervisor is KVM via libvirt.
  30. The required software for a hypervisor is libvirt and kvm. For advanced
  31. features, install libguestfs or qemu-nbd.
  32. .. note::
  33. Libguestfs and qemu-nbd allow for virtual machine images to be mounted
  34. before startup and get pre-seeded with configurations and a salt minion.
  35. This sls will set up the needed software for a hypervisor, and run the routines
  36. to set up the libvirt pki keys.
  37. .. note::
  38. Package names and setup used is Red Hat specific. Different package names
  39. will be required for different platforms.
  40. .. code-block:: yaml
  41. libvirt:
  42. pkg.installed: []
  43. file.managed:
  44. - name: /etc/sysconfig/libvirtd
  45. - contents: 'LIBVIRTD_ARGS="--listen"'
  46. - require:
  47. - pkg: libvirt
  48. virt.keys:
  49. - require:
  50. - pkg: libvirt
  51. service.running:
  52. - name: libvirtd
  53. - require:
  54. - pkg: libvirt
  55. - network: br0
  56. - libvirt: libvirt
  57. - watch:
  58. - file: libvirt
  59. libvirt-python:
  60. pkg.installed: []
  61. libguestfs:
  62. pkg.installed:
  63. - pkgs:
  64. - libguestfs
  65. - libguestfs-tools
  66. Hypervisor Network Setup
  67. ------------------------
  68. The hypervisors will need to be running a network bridge to serve up network
  69. devices for virtual machines. This formula will set up a standard bridge on
  70. a hypervisor connecting the bridge to eth0:
  71. .. code-block:: yaml
  72. eth0:
  73. network.managed:
  74. - enabled: True
  75. - type: eth
  76. - bridge: br0
  77. br0:
  78. network.managed:
  79. - enabled: True
  80. - type: bridge
  81. - proto: dhcp
  82. - require:
  83. - network: eth0
  84. Virtual Machine Network Setup
  85. -----------------------------
  86. Salt Virt comes with a system to model the network interfaces used by the
  87. deployed virtual machines. By default, a single interface is created for the
  88. deployed virtual machine and is bridged to ``br0``. To get going with the
  89. default networking setup, ensure that the bridge interface named ``br0`` exists
  90. on the hypervisor and is bridged to an active network device.
  91. .. note::
  92. To use more advanced networking in Salt Virt, read the `Salt Virt
  93. Networking` document:
  94. :ref:`Salt Virt Networking <vm-nic-profiles>`
  95. Libvirt State
  96. -------------
  97. One of the challenges of deploying a libvirt based cloud is the distribution
  98. of libvirt certificates. These certificates allow for virtual machine
  99. migration. Salt comes with a system used to auto deploy these certificates.
  100. Salt manages the signing authority key and generates keys for libvirt clients
  101. on the master, signs them with the certificate authority, and uses pillar to
  102. distribute them. This is managed via the ``libvirt`` state. Simply execute this
  103. formula on the minion to ensure that the certificate is in place and up to
  104. date:
  105. .. note::
  106. The above formula includes the calls needed to set up libvirt keys.
  107. .. code-block:: yaml
  108. libvirt_keys:
  109. virt.keys
  110. Getting Virtual Machine Images Ready
  111. ====================================
  112. Salt Virt requires that virtual machine images be provided as these are not
  113. generated on the fly. Generating these virtual machine images differs greatly
  114. based on the underlying platform.
  115. Virtual machine images can be manually created using KVM and running through
  116. the installer, but this process is not recommended since it is very manual and
  117. prone to errors.
  118. Virtual Machine generation applications are available for many platforms:
  119. kiwi: (openSUSE, SLES, RHEL, CentOS)
  120. https://suse.github.io/kiwi/
  121. vm-builder:
  122. https://wiki.debian.org/VMBuilder
  123. .. seealso:: :formula_url:`vmbuilder-formula`
  124. Once virtual machine images are available, the easiest way to make them
  125. available to Salt Virt is to place them in the Salt file server. Just copy an
  126. image into ``/srv/salt`` and it can now be used by Salt Virt.
  127. For purposes of this demo, the file name ``centos.img`` will be used.
  128. Existing Virtual Machine Images
  129. -------------------------------
  130. Many existing Linux distributions distribute virtual machine images which
  131. can be used with Salt Virt. Please be advised that NONE OF THESE IMAGES ARE
  132. SUPPORTED BY SALTSTACK.
  133. CentOS
  134. ~~~~~~
  135. These images have been prepared for OpenNebula but should work without issue
  136. with Salt Virt. Only the raw qcow image file is needed:
  137. http://wiki.centos.org/Cloud/OpenNebula
  138. Fedora Linux
  139. ~~~~~~~~~~~~
  140. Images for Fedora Linux can be found here:
  141. https://alt.fedoraproject.org/cloud
  142. openSUSE
  143. ~~~~~~~~
  144. http://download.opensuse.org/repositories/openSUSE:/Leap:/42.1:/Images/images
  145. (look for JeOS-for-kvm-and-xen variant)
  146. SUSE
  147. ~~~~
  148. https://www.suse.com/products/server/jeos
  149. Ubuntu Linux
  150. ~~~~~~~~~~~~
  151. Images for Ubuntu Linux can be found here:
  152. http://cloud-images.ubuntu.com/
  153. Using Salt Virt
  154. ===============
  155. With hypervisors set up and virtual machine images ready, Salt can start
  156. issuing cloud commands using the `virt runner`.
  157. Start by running a Salt Virt hypervisor info command:
  158. .. code-block:: bash
  159. salt-run virt.host_info
  160. This will query the running hypervisor(s) for stats and display useful
  161. information such as the number of CPUs and amount of memory.
  162. You can also list all VMs and their current states on all hypervisor nodes:
  163. .. code-block:: bash
  164. salt-run virt.list
  165. Now that hypervisors are available a virtual machine can be provisioned, the
  166. ``virt.init`` routine will create a new virtual machine:
  167. .. code-block:: bash
  168. salt-run virt.init centos1 2 512 salt://centos.img
  169. The Salt Virt runner will now automatically select a hypervisor to deploy
  170. the new virtual machine on. Using ``salt://`` assumes that the CentOS virtual
  171. machine image is located in the root of the :ref:`file-server` on the master.
  172. When images are cloned (i.e. copied locally after retrieval from the file
  173. server), the destination directory on the hypervisor minion is determined by the
  174. ``virt:images`` config option; by default this is ``/srv/salt-images/``.
  175. When a VM is initialized using ``virt.init``, the image is copied to the
  176. hypervisor using ``cp.cache_file`` and will be mounted and seeded with a minion.
  177. Seeding includes setting pre-authenticated keys on the new machine. A minion
  178. will only be installed if one can not be found on the image using the default
  179. arguments to ``seed.apply``.
  180. .. note::
  181. The biggest bottleneck in starting VMs is when the Salt Minion needs to be
  182. installed. Making sure that the source VM images already have Salt
  183. installed will GREATLY speed up virtual machine deployment.
  184. You can also deploy an image on a particular minion by directly calling the
  185. ``virt`` execution module with an absolute image path. This can be quite handy for
  186. testing:
  187. .. code-block:: bash
  188. salt 'hypervisor*' virt.init centos1 2 512 image=/var/lib/libvirt/images/centos.img
  189. Now that the new VM has been prepared, it can be seen via the ``virt.query``
  190. command:
  191. .. code-block:: bash
  192. salt-run virt.query
  193. This command will return data about all of the hypervisors and respective
  194. virtual machines.
  195. Now that the new VM is booted, it should have contacted the Salt Master. A
  196. ``test.version`` will reveal if the new VM is running.
  197. QEMU Copy on Write Support
  198. ==========================
  199. For fast image cloning, you can use the `qcow`_ disk image format.
  200. Pass the ``enable_qcow`` flag and a `.qcow2` image path to `virt.init`:
  201. .. code-block:: bash
  202. salt 'hypervisor*' virt.init centos1 2 512 image=/var/lib/libvirt/images/centos.qcow2 enable_qcow=True start=False
  203. .. note::
  204. Beware that attempting to boot a qcow image too quickly after cloning
  205. can result in a race condition where libvirt may try to boot the machine
  206. before image seeding has completed. For that reason, it is recommended to
  207. also pass ``start=False`` to ``virt.init``.
  208. Also know that you **must not** modify the original base image without
  209. first making a copy and then *rebasing* all overlay images onto it.
  210. See the ``qemu-img rebase`` `usage docs <rebase>`_.
  211. Migrating Virtual Machines
  212. ==========================
  213. Salt Virt comes with full support for virtual machine migration. Using
  214. the libvirt state in the above formula makes migration possible.
  215. A few things need to be available to support migration. Many operating systems
  216. turn on firewalls when originally set up; the firewall needs to be opened up
  217. to allow for libvirt and kvm to cross communicate and execution migration
  218. routines. On Red Hat based hypervisors in particular, port 16514 needs to be
  219. opened on hypervisors:
  220. .. code-block:: bash
  221. iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 16514 -j ACCEPT
  222. .. note::
  223. More in-depth information regarding distribution specific firewall settings can be found in:
  224. :ref:`Opening the Firewall up for Salt <firewall>`
  225. Salt also needs the ``virt:tunnel`` option to be turned on. This flag tells Salt
  226. to run migrations securely via the libvirt TLS tunnel and to use port 16514.
  227. Without ``virt:tunnel``, libvirt tries to bind to random ports when running
  228. migrations.
  229. To turn on ``virt:tunnel``, simply apply it to the master config file:
  230. .. code-block:: yaml
  231. virt:
  232. tunnel: True
  233. Once the master config has been updated, restart the master and send out a call
  234. to the minions to refresh the pillar to pick up on the change:
  235. .. code-block:: bash
  236. salt \* saltutil.refresh_modules
  237. Now, migration routines can be run! To migrate a VM, simply run the Salt Virt
  238. migrate routine:
  239. .. code-block:: bash
  240. salt-run virt.migrate centos <new hypervisor>
  241. VNC Consoles
  242. ============
  243. Although not enabled by default, Salt Virt can also set up VNC consoles allowing
  244. for remote visual consoles to be opened up. When creating a new VM using
  245. ``virt.init``, pass the ``enable_vnc=True`` parameter to have a console
  246. configured for the new VM.
  247. The information from a ``virt.query`` routine will display the VNC console port
  248. for the specific VMs:
  249. .. code-block:: yaml
  250. centos
  251. CPU: 2
  252. Memory: 524288
  253. State: running
  254. Graphics: vnc - hyper6:5900
  255. Disk - vda:
  256. Size: 2.0G
  257. File: /srv/salt-images/ubuntu2/system.qcow2
  258. File Format: qcow2
  259. Nic - ac:de:48:98:08:77:
  260. Source: br0
  261. Type: bridge
  262. The line `Graphics: vnc - hyper6:5900` holds the key. First the port named,
  263. in this case 5900, will need to be available in the hypervisor's firewall.
  264. Once the port is open, then the console can be easily opened via vncviewer:
  265. .. code-block:: bash
  266. vncviewer hyper6:5900
  267. By default there is no VNC security set up on these ports, which suggests that
  268. keeping them firewalled and mandating that SSH tunnels be used to access these
  269. VNC interfaces. Keep in mind that activity on a VNC interface that is accessed
  270. can be viewed by any other user that accesses that same VNC interface, and any
  271. other user logging in can also operate with the logged in user on the virtual
  272. machine.
  273. Conclusion
  274. ==========
  275. Now with Salt Virt running, new hypervisors can be seamlessly added just by
  276. running the above states on new bare metal machines, and these machines will be
  277. instantly available to Salt Virt.
  278. .. links
  279. .. _qcow:
  280. https://en.wikipedia.org/wiki/Qcow
  281. .. _rebase:
  282. https://docs.fedoraproject.org/en-US/Fedora/18/html/Virtualization_Administration_Guide/sect-Virtualization-Tips_and_tricks-Using_qemu_img.html