cloud_controller.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. .. _cloud-controller:
  2. ==========================
  3. Salt as a Cloud Controller
  4. ==========================
  5. In Salt 0.14.0, an advanced cloud control system were introduced, allow
  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. The
  14. cloud that can scale and is fully featured. Salt Virt comes with the
  15. ability to set up and manage complex virtual machine networking, powerful
  16. image and disk management, as well as virtual machine migration with and without
  17. shared storage.
  18. This means that Salt Virt can be used to create a cloud from a blade center
  19. and a SAN, but can also create a cloud out of a swarm of Linux Desktops
  20. without a single shared storage system. Salt Virt can make clouds from
  21. truly commodity hardware, but can also stand up the power of specialized
  22. hardware as well.
  23. Setting up Hypervisors
  24. ======================
  25. The first step to set up the hypervisors involves getting the correct software
  26. installed and setting up the hypervisor network interfaces.
  27. Installing Hypervisor Software
  28. ------------------------------
  29. Salt Virt is made to be hypervisor agnostic but currently the only fully
  30. implemented hypervisor is KVM via libvirt.
  31. The required software for a hypervisor is libvirt and kvm. For advanced
  32. features install libguestfs or qemu-nbd.
  33. .. note::
  34. Libguestfs and qemu-nbd allow for virtual machine images to be mounted
  35. before startup and get pre-seeded with configurations and a salt minion
  36. This sls will set up the needed software for a hypervisor, and run the routines
  37. to set up the libvirt pki keys.
  38. .. note::
  39. Package names and setup used is Red Hat specific, different package names
  40. will be required for different platforms
  41. .. code-block:: yaml
  42. libvirt:
  43. pkg.installed: []
  44. file.managed:
  45. - name: /etc/sysconfig/libvirtd
  46. - contents: 'LIBVIRTD_ARGS="--listen"'
  47. - require:
  48. - pkg: libvirt
  49. virt.keys:
  50. - require:
  51. - pkg: libvirt
  52. service.running:
  53. - name: libvirtd
  54. - require:
  55. - pkg: libvirt
  56. - network: br0
  57. - libvirt: libvirt
  58. - watch:
  59. - file: libvirt
  60. libvirt-python:
  61. pkg.installed: []
  62. libguestfs:
  63. pkg.installed:
  64. - pkgs:
  65. - libguestfs
  66. - libguestfs-tools
  67. Hypervisor Network Setup
  68. ------------------------
  69. The hypervisors will need to be running a network bridge to serve up network
  70. devices for virtual machines, this formula will set up a standard bridge on
  71. a hypervisor connecting the bridge to eth0:
  72. .. code-block:: yaml
  73. eth0:
  74. network.managed:
  75. - enabled: True
  76. - type: eth
  77. - bridge: br0
  78. br0:
  79. network.managed:
  80. - enabled: True
  81. - type: bridge
  82. - proto: dhcp
  83. - require:
  84. - network: eth0
  85. Virtual Machine Network Setup
  86. -----------------------------
  87. Salt Virt comes with a system to model the network interfaces used by the
  88. deployed virtual machines; by default a single interface is created for the
  89. deployed virtual machine and is bridged to ``br0``. To get going with the
  90. default networking setup, ensure that the bridge interface named ``br0`` exists
  91. on the hypervisor and is bridged to an active network device.
  92. .. note::
  93. To use more advanced networking in Salt Virt, read the `Salt Virt
  94. Networking` document:
  95. :ref:`Salt Virt Networking <vm-nic-profiles>`
  96. Libvirt State
  97. -------------
  98. One of the challenges of deploying a libvirt based cloud is the distribution
  99. of libvirt certificates. These certificates allow for virtual machine
  100. migration. Salt comes with a system used to auto deploy these certificates.
  101. Salt manages the signing authority key and generates keys for libvirt clients
  102. on the master, signs them with the certificate authority and uses pillar to
  103. distribute them. This is managed via the ``libvirt`` state. Simply execute this
  104. formula on the minion to ensure that the certificate is in place and up to
  105. date:
  106. .. note::
  107. The above formula includes the calls needed to set up libvirt keys.
  108. .. code-block:: yaml
  109. libvirt_keys:
  110. virt.keys
  111. Getting Virtual Machine Images Ready
  112. ====================================
  113. Salt Virt, requires that virtual machine images be provided as these are not
  114. generated on the fly. Generating these virtual machine images differs greatly
  115. based on the underlying platform.
  116. Virtual machine images can be manually created using KVM and running through
  117. the installer, but this process is not recommended since it is very manual and
  118. prone to errors.
  119. Virtual Machine generation applications are available for many platforms:
  120. kiwi: (openSUSE, SLES, RHEL, CentOS)
  121. https://suse.github.io/kiwi/
  122. vm-builder:
  123. https://wiki.debian.org/VMBuilder
  124. .. seealso:: :formula_url:`vmbuilder-formula`
  125. Once virtual machine images are available, the easiest way to make them
  126. available to Salt Virt is to place them in the Salt file server. Just copy an
  127. image into ``/srv/salt`` and it can now be used by Salt Virt.
  128. For purposes of this demo, the file name ``centos.img`` will be used.
  129. Existing Virtual Machine Images
  130. -------------------------------
  131. Many existing Linux distributions distribute virtual machine images which
  132. can be used with Salt Virt. Please be advised that NONE OF THESE IMAGES ARE
  133. SUPPORTED BY SALTSTACK.
  134. CentOS
  135. ~~~~~~
  136. These images have been prepared for OpenNebula but should work without issue with
  137. Salt Virt, only the raw qcow image file is needed:
  138. http://wiki.centos.org/Cloud/OpenNebula
  139. Fedora Linux
  140. ~~~~~~~~~~~~
  141. Images for Fedora Linux can be found here:
  142. https://alt.fedoraproject.org/cloud
  143. openSUSE
  144. ~~~~~~~~
  145. 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
  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
  163. nodes:
  164. .. code-block:: bash
  165. salt-run virt.list
  166. Now that hypervisors are available a virtual machine can be provisioned.
  167. The ``virt.init`` routine will create a new virtual machine:
  168. .. code-block:: bash
  169. salt-run virt.init centos1 2 512 salt://centos.img
  170. The Salt Virt runner will now automatically select a hypervisor to deploy
  171. the new virtual machine on. Using ``salt://`` assumes that the CentOS virtual
  172. machine image is located in the root of the :ref:`file-server` on the master.
  173. When images are cloned (i.e. copied locatlly after retrieval from the file server)
  174. the destination directory on the hypervisor minion is determined by the ``virt:images``
  175. config option; by default this is ``/srv/salt-images/``.
  176. When a VM is initialized using ``virt.init`` the image is copied to the hypervisor
  177. using ``cp.cache_file`` and will be mounted and seeded with a minion. Seeding includes
  178. setting pre-authenticated keys on the new machine. A minion will only be installed if
  179. one can not be found on the image using the default 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 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.version`` 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, and 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 read in:
  223. :ref:`Opening the Firewall up for Salt <firewall>`
  224. Salt also needs the ``virt:tunnel`` option to be turned on.
  225. This flag tells Salt to run migrations securely via the libvirt TLS tunnel and to
  226. use port 16514. Without ``virt:tunnel`` libvirt tries to bind to random ports when
  227. running 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:: bash
  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 for remote visual
  243. consoles to be opened up. When creating a new VM using ``virt.init`` pass the ``enable_vnc=True``
  244. parameter to have a console configured for the new VM.
  245. The information from a ``virt.query`` routine will display the vnc console port for the specific vms:
  246. .. code-block:: yaml
  247. centos
  248. CPU: 2
  249. Memory: 524288
  250. State: running
  251. Graphics: vnc - hyper6:5900
  252. Disk - vda:
  253. Size: 2.0G
  254. File: /srv/salt-images/ubuntu2/system.qcow2
  255. File Format: qcow2
  256. Nic - ac:de:48:98:08:77:
  257. Source: br0
  258. Type: bridge
  259. The line `Graphics: vnc - hyper6:5900` holds the key. First the port named,
  260. in this case 5900, will need to be available in the hypervisor's firewall.
  261. Once the port is open, then the console can be easily opened via vncviewer:
  262. .. code-block:: bash
  263. vncviewer hyper6:5900
  264. By default there is no VNC security set up on these ports, which suggests that
  265. keeping them firewalled and mandating that SSH tunnels be used to access these
  266. VNC interfaces. Keep in mind that activity on a VNC interface that is accessed
  267. can be viewed by any other user that accesses that same VNC interface, and any other
  268. user logging in can also operate with the logged in user on the virtual
  269. machine.
  270. Conclusion
  271. ==========
  272. Now with Salt Virt running, new hypervisors can be seamlessly added just by
  273. running the above states on new bare metal machines, and these machines will be
  274. instantly available to Salt Virt.
  275. .. links
  276. .. _qcow:
  277. https://en.wikipedia.org/wiki/Qcow
  278. .. _rebase:
  279. https://docs.fedoraproject.org/en-US/Fedora/18/html/Virtualization_Administration_Guide/sect-Virtualization-Tips_and_tricks-Using_qemu_img.html