1
0

cloud_controller.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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://opensuse.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 with
  136. Salt Virt, only the raw qcow image file is needed:
  137. https://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. https://download.opensuse.org/distribution/leap/15.1/jeos/openSUSE-Leap-15.1-JeOS.x86_64-15.1.0-kvm-and-xen-Current.qcow2.meta4
  145. SUSE
  146. ~~~~
  147. https://www.suse.com/products/server/jeos
  148. Ubuntu Linux
  149. ~~~~~~~~~~~~
  150. Images for Ubuntu Linux can be found here:
  151. http://cloud-images.ubuntu.com/
  152. Using Salt Virt
  153. ===============
  154. With hypervisors set up and virtual machine images ready, Salt can start
  155. issuing cloud commands using the `virt runner`.
  156. Start by running a Salt Virt hypervisor info command:
  157. .. code-block:: bash
  158. salt-run virt.host_info
  159. This will query the running hypervisor(s) for stats and display useful
  160. information such as the number of CPUs and amount of memory.
  161. You can also list all VMs and their current states on all hypervisor nodes:
  162. .. code-block:: bash
  163. salt-run virt.list
  164. Now that hypervisors are available a virtual machine can be provisioned, the
  165. ``virt.init`` routine will create a new virtual machine:
  166. .. code-block:: bash
  167. salt-run virt.init centos1 2 512 salt://centos.img
  168. The Salt Virt runner will now automatically select a hypervisor to deploy
  169. the new virtual machine on. Using ``salt://`` assumes that the CentOS virtual
  170. machine image is located in the root of the :ref:`file-server` on the master.
  171. When images are cloned (i.e. copied locally after retrieval from the file
  172. server), the destination directory on the hypervisor minion is determined by the
  173. ``virt:images`` config option; by default this is ``/srv/salt-images/``.
  174. When a VM is initialized using ``virt.init``, the image is copied to the
  175. hypervisor using ``cp.cache_file`` and will be mounted and seeded with a minion.
  176. Seeding includes setting pre-authenticated keys on the new machine. A minion
  177. will only be installed if one can not be found on the image using the default
  178. arguments to ``seed.apply``.
  179. .. note::
  180. The biggest bottleneck in starting VMs is when the Salt Minion needs to be
  181. installed. Making sure that the source VM images already have Salt
  182. installed will GREATLY speed up virtual machine deployment.
  183. You can also deploy an image on a particular minion by directly calling the
  184. ``virt`` execution module with an absolute image path. This can be quite handy for
  185. testing:
  186. .. code-block:: bash
  187. salt 'hypervisor*' virt.init centos1 2 512 image=/var/lib/libvirt/images/centos.img
  188. Now that the new VM has been prepared, it can be seen via the ``virt.query``
  189. command:
  190. .. code-block:: bash
  191. salt-run virt.query
  192. This command will return data about all of the hypervisors and respective
  193. virtual machines.
  194. Now that the new VM is booted, it should have contacted the Salt Master. A
  195. ``test.ping`` will reveal if the new VM is running.
  196. QEMU Copy on Write Support
  197. ==========================
  198. For fast image cloning, you can use the `qcow`_ disk image format.
  199. Pass the ``enable_qcow`` flag and a `.qcow2` image path to `virt.init`:
  200. .. code-block:: bash
  201. salt 'hypervisor*' virt.init centos1 2 512 image=/var/lib/libvirt/images/centos.qcow2 enable_qcow=True start=False
  202. .. note::
  203. Beware that attempting to boot a qcow image too quickly after cloning
  204. can result in a race condition where libvirt may try to boot the machine
  205. before image seeding has completed. For that reason, it is recommended to
  206. also pass ``start=False`` to ``virt.init``.
  207. Also know that you **must not** modify the original base image without
  208. first making a copy and then *rebasing* all overlay images onto it.
  209. See the ``qemu-img rebase`` `usage docs <rebase>`_.
  210. Migrating Virtual Machines
  211. ==========================
  212. Salt Virt comes with full support for virtual machine migration. Using
  213. the libvirt state in the above formula makes migration possible.
  214. A few things need to be available to support migration. Many operating systems
  215. turn on firewalls when originally set up; the firewall needs to be opened up
  216. to allow for libvirt and kvm to cross communicate and execution migration
  217. routines. On Red Hat based hypervisors in particular, port 16514 needs to be
  218. opened on hypervisors:
  219. .. code-block:: bash
  220. iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 16514 -j ACCEPT
  221. .. note::
  222. More in-depth information regarding distribution specific firewall settings can be found in:
  223. :ref:`Opening the Firewall up for Salt <firewall>`
  224. Salt also needs the ``virt:tunnel`` option to be turned on. This flag tells Salt
  225. to run migrations securely via the libvirt TLS tunnel and to use port 16514.
  226. Without ``virt:tunnel``, libvirt tries to bind to random ports when running
  227. migrations.
  228. To turn on ``virt:tunnel``, simply apply it to the master config file:
  229. .. code-block:: yaml
  230. virt:
  231. tunnel: True
  232. Once the master config has been updated, restart the master and send out a call
  233. to the minions to refresh the pillar to pick up on the change:
  234. .. code-block:: bash
  235. salt \* saltutil.refresh_modules
  236. Now, migration routines can be run! To migrate a VM, simply run the Salt Virt
  237. migrate routine:
  238. .. code-block:: console
  239. salt-run virt.migrate centos <new hypervisor>
  240. VNC Consoles
  241. ============
  242. Although not enabled by default, Salt Virt can also set up VNC consoles allowing
  243. for remote visual consoles to be opened up. When creating a new VM using
  244. ``virt.init``, pass the ``enable_vnc=True`` parameter to have a console
  245. configured for the new VM.
  246. The information from a ``virt.query`` routine will display the VNC console port
  247. for the specific VMs:
  248. .. code-block:: yaml
  249. centos
  250. CPU: 2
  251. Memory: 524288
  252. State: running
  253. Graphics: vnc - hyper6:5900
  254. Disk - vda:
  255. Size: 2.0G
  256. File: /srv/salt-images/ubuntu2/system.qcow2
  257. File Format: qcow2
  258. Nic - ac:de:48:98:08:77:
  259. Source: br0
  260. Type: bridge
  261. The line `Graphics: vnc - hyper6:5900` holds the key. First the port named,
  262. in this case 5900, will need to be available in the hypervisor's firewall.
  263. Once the port is open, then the console can be easily opened via vncviewer:
  264. .. code-block:: bash
  265. vncviewer hyper6:5900
  266. By default there is no VNC security set up on these ports, which suggests that
  267. keeping them firewalled and mandating that SSH tunnels be used to access these
  268. VNC interfaces. Keep in mind that activity on a VNC interface that is accessed
  269. can be viewed by any other user that accesses that same VNC interface, and any
  270. other user logging in can also operate with the logged in user on the virtual
  271. machine.
  272. Conclusion
  273. ==========
  274. Now with Salt Virt running, new hypervisors can be seamlessly added just by
  275. running the above states on new bare metal machines, and these machines will be
  276. instantly available to Salt Virt.
  277. .. links
  278. .. _qcow:
  279. https://en.wikipedia.org/wiki/Qcow
  280. .. _rebase:
  281. https://docs.fedoraproject.org/en-US/Fedora/18/html/Virtualization_Administration_Guide/sect-Virtualization-Tips_and_tricks-Using_qemu_img.html