walkthrough_macosx.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. .. _tutorial-macos-walk-through:
  2. ======================================================================
  3. The macOS (Maverick) Developer Step By Step Guide To Salt Installation
  4. ======================================================================
  5. This document provides a step-by-step guide to installing a Salt cluster
  6. consisting of one master, and one minion running on a local VM hosted on macOS.
  7. .. note::
  8. This guide is aimed at developers who wish to run Salt in a virtual machine.
  9. The official (Linux) walkthrough can be found
  10. `here <http://docs.saltstack.com/topics/tutorials/walkthrough.html>`_.
  11. The 5 Cent Salt Intro
  12. =====================
  13. Since you're here you've probably already heard about Salt, so you already
  14. know Salt lets you configure and run commands on hordes of servers easily.
  15. Here's a brief overview of a Salt cluster:
  16. - Salt works by having a "master" server sending commands to one or multiple
  17. "minion" servers. The master server is the "command center". It is
  18. going to be the place where you store your configuration files, aka: "which
  19. server is the db, which is the web server, and what libraries and software
  20. they should have installed". The minions receive orders from the master.
  21. Minions are the servers actually performing work for your business.
  22. - Salt has two types of configuration files:
  23. 1. the "salt communication channels" or "meta" or "config" configuration
  24. files (not official names): one for the master (usually is /etc/salt/master
  25. , **on the master server**), and one for minions (default is
  26. /etc/salt/minion or /etc/salt/minion.conf, **on the minion servers**). Those
  27. files are used to determine things like the Salt Master IP, port, Salt
  28. folder locations, etc.. If these are configured incorrectly, your minions
  29. will probably be unable to receive orders from the master, or the master
  30. will not know which software a given minion should install.
  31. 2. the "business" or "service" configuration files (once again, not an
  32. official name): these are configuration files, ending with ".sls" extension,
  33. that describe which software should run on which server, along with
  34. particular configuration properties for the software that is being
  35. installed. These files should be created in the /srv/salt folder by default,
  36. but their location can be changed using ... /etc/salt/master configuration file!
  37. .. note::
  38. This tutorial contains a third important configuration file, not to
  39. be confused with the previous two: the virtual machine provisioning
  40. configuration file. This in itself is not specifically tied to Salt, but
  41. it also contains some Salt configuration. More on that in step 3. Also
  42. note that all configuration files are YAML files. So indentation matters.
  43. .. note::
  44. Salt also works with "masterless" configuration where a minion is
  45. autonomous (in which case salt can be seen as a local configuration tool),
  46. or in "multiple master" configuration. See the documentation for more on
  47. that.
  48. Before Digging In, The Architecture Of The Salt Cluster
  49. -------------------------------------------------------
  50. Salt Master
  51. ***********
  52. The "Salt master" server is going to be the Mac OS machine, directly. Commands
  53. will be run from a terminal app, so Salt will need to be installed on the Mac.
  54. This is going to be more convenient for toying around with configuration files.
  55. Salt Minion
  56. ***********
  57. We'll only have one "Salt minion" server. It is going to be running on a
  58. Virtual Machine running on the Mac, using VirtualBox. It will run an Ubuntu
  59. distribution.
  60. Step 1 - Configuring The Salt Master On Your Mac
  61. ================================================
  62. `Official Documentation
  63. <http://docs.saltstack.com/topics/installation/osx.html>`_
  64. Because Salt has a lot of dependencies that are not built in macOS, we will use
  65. Homebrew to install Salt. Homebrew is a package manager for Mac, it's great, use
  66. it (for this tutorial at least!). Some people spend a lot of time installing
  67. libs by hand to better understand dependencies, and then realize how useful a
  68. package manager is once they're configuring a brand new machine and have to do
  69. it all over again. It also lets you *uninstall* things easily.
  70. .. note::
  71. Brew is a Ruby program (Ruby is installed by default with your Mac). Brew
  72. downloads, compiles, and links software. The linking phase is when compiled
  73. software is deployed on your machine. It may conflict with manually
  74. installed software, especially in the /usr/local directory. It's ok,
  75. remove the manually installed version then refresh the link by typing
  76. ``brew link 'packageName'``. Brew has a ``brew doctor`` command that can
  77. help you troubleshoot. It's a great command, use it often. Brew requires
  78. xcode command line tools. When you run brew the first time it asks you to
  79. install them if they're not already on your system. Brew installs
  80. software in /usr/local/bin (system bins are in /usr/bin). In order to use
  81. those bins you need your $PATH to search there first. Brew tells you if
  82. your $PATH needs to be fixed.
  83. .. tip::
  84. Use the keyboard shortcut ``cmd + shift + period`` in the "open" macOS
  85. dialog box to display hidden files and folders, such as .profile.
  86. Install Homebrew
  87. ----------------
  88. Install Homebrew here https://brew.sh/
  89. Or just type
  90. .. code-block:: bash
  91. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  92. Now type the following commands in your terminal (you may want to type ``brew
  93. doctor`` after each to make sure everything's fine):
  94. .. code-block:: bash
  95. brew install python
  96. brew install swig
  97. brew install zmq
  98. .. note::
  99. zmq is ZeroMQ. It's a fantastic library used for server to server network
  100. communication and is at the core of Salt efficiency.
  101. Install Salt
  102. ------------
  103. You should now have everything ready to launch this command:
  104. .. code-block:: bash
  105. pip install salt
  106. .. note::
  107. There should be no need for ``sudo pip install salt``. Brew installed
  108. Python for your user, so you should have all the access. In case you
  109. would like to check, type ``which python`` to ensure that it's
  110. /usr/local/bin/python, and ``which pip`` which should be
  111. /usr/local/bin/pip.
  112. Now type ``python`` in a terminal then, ``import salt``. There should be no
  113. errors. Now exit the Python terminal using ``exit()``.
  114. Create The Master Configuration
  115. -------------------------------
  116. If the default /etc/salt/master configuration file was not created,
  117. copy-paste it from here:
  118. http://docs.saltstack.com/ref/configuration/examples.html#configuration-examples-master
  119. .. note::
  120. ``/etc/salt/master`` is a file, not a folder.
  121. Salt Master configuration changes. The Salt master needs a few customization
  122. to be able to run on macOS:
  123. .. code-block:: bash
  124. sudo launchctl limit maxfiles 4096 8192
  125. In the /etc/salt/master file, change max_open_files to 8192 (or just add the
  126. line: ``max_open_files: 8192`` (no quote) if it doesn't already exists).
  127. You should now be able to launch the Salt master:
  128. .. code-block:: bash
  129. sudo salt-master --log-level=all
  130. There should be no errors when running the above command.
  131. .. note::
  132. This command is supposed to be a daemon, but for toying around, we'll keep
  133. it running on a terminal to monitor the activity.
  134. Now that the master is set, let's configure a minion on a VM.
  135. Step 2 - Configuring The Minion VM
  136. ==================================
  137. The Salt minion is going to run on a Virtual Machine. There are a lot of
  138. software options that let you run virtual machines on a mac, But for this
  139. tutorial we're going to use VirtualBox. In addition to virtualBox, we will use
  140. Vagrant, which allows you to create the base VM configuration.
  141. Vagrant lets you build ready to use VM images, starting from an OS image and
  142. customizing it using "provisioners". In our case, we'll use it to:
  143. * Download the base Ubuntu image
  144. * Install salt on that Ubuntu image (Salt is going to be the "provisioner"
  145. for the VM).
  146. * Launch the VM
  147. * SSH into the VM to debug
  148. * Stop the VM once you're done.
  149. Install VirtualBox
  150. ------------------
  151. Go get it here: https://www.virtualbox.org/wiki/Downloads (click on VirtualBox
  152. for macOS hosts => x86/amd64)
  153. Install Vagrant
  154. ---------------
  155. Go get it here: https://www.vagrantup.com/downloads.html and choose the latest version
  156. (1.3.5 at time of writing), then the .dmg file. Double-click to install it.
  157. Make sure the ``vagrant`` command is found when run in the terminal. Type
  158. ``vagrant``. It should display a list of commands.
  159. Create The Minion VM Folder
  160. ---------------------------
  161. Create a folder in which you will store your minion's VM. In this tutorial,
  162. it's going to be a minion folder in the $home directory.
  163. .. code-block:: bash
  164. cd $home
  165. mkdir minion
  166. Initialize Vagrant
  167. ------------------
  168. From the minion folder, type
  169. .. code-block:: bash
  170. vagrant init
  171. This command creates a default Vagrantfile configuration file. This
  172. configuration file will be used to pass configuration parameters to the Salt
  173. provisioner in Step 3.
  174. Import Precise64 Ubuntu Box
  175. ---------------------------
  176. .. code-block:: bash
  177. vagrant box add precise64 http://files.vagrantup.com/precise64.box
  178. .. note::
  179. This box is added at the global Vagrant level. You only need to do it
  180. once as each VM will use this same file.
  181. Modify the Vagrantfile
  182. ----------------------
  183. Modify ./minion/Vagrantfile to use th precise64 box. Change the ``config.vm.box``
  184. line to:
  185. .. code-block:: yaml
  186. config.vm.box = "precise64"
  187. Uncomment the line creating a host-only IP. This is the ip of your minion
  188. (you can change it to something else if that IP is already in use):
  189. .. code-block:: yaml
  190. config.vm.network :private_network, ip: "192.168.33.10"
  191. At this point you should have a VM that can run, although there won't be much
  192. in it. Let's check that.
  193. Checking The VM
  194. ---------------
  195. From the $home/minion folder type:
  196. .. code-block:: bash
  197. vagrant up
  198. A log showing the VM booting should be present. Once it's done you'll be back
  199. to the terminal:
  200. .. code-block:: bash
  201. ping 192.168.33.10
  202. The VM should respond to your ping request.
  203. Now log into the VM in ssh using Vagrant again:
  204. .. code-block:: bash
  205. vagrant ssh
  206. You should see the shell prompt change to something similar to
  207. ``vagrant@precise64:~$`` meaning you're inside the VM. From there, enter the
  208. following:
  209. .. code-block:: bash
  210. ping 10.0.2.2
  211. .. note::
  212. That ip is the ip of your VM host (the macOS host). The number is a
  213. VirtualBox default and is displayed in the log after the Vagrant ssh
  214. command. We'll use that IP to tell the minion where the Salt master is.
  215. Once you're done, end the ssh session by typing ``exit``.
  216. It's now time to connect the VM to the salt master
  217. Step 3 - Connecting Master and Minion
  218. =====================================
  219. Creating The Minion Configuration File
  220. --------------------------------------
  221. Create the ``/etc/salt/minion`` file. In that file, put the
  222. following lines, giving the ID for this minion, and the IP of the master:
  223. .. code-block:: yaml
  224. master: 10.0.2.2
  225. id: 'minion1'
  226. file_client: remote
  227. Minions authenticate with the master using keys. Keys are generated
  228. automatically if you don't provide one and can accept them later on. However,
  229. this requires accepting the minion key every time the minion is destroyed or
  230. created (which could be quite often). A better way is to create those keys in
  231. advance, feed them to the minion, and authorize them once.
  232. Preseed minion keys
  233. -------------------
  234. From the minion folder on your Mac run:
  235. .. code-block:: bash
  236. sudo salt-key --gen-keys=minion1
  237. This should create two files: minion1.pem, and minion1.pub.
  238. Since those files have been created using sudo, but will be used by vagrant,
  239. you need to change ownership:
  240. .. code-block:: bash
  241. sudo chown youruser:yourgroup minion1.pem
  242. sudo chown youruser:yourgroup minion1.pub
  243. Then copy the .pub file into the list of accepted minions:
  244. .. code-block:: bash
  245. sudo cp minion1.pub /etc/salt/pki/master/minions/minion1
  246. Modify Vagrantfile to Use Salt Provisioner
  247. ------------------------------------------
  248. Let's now modify the Vagrantfile used to provision the Salt VM. Add the
  249. following section in the Vagrantfile (note: it should be at the same
  250. indentation level as the other properties):
  251. .. code-block:: yaml
  252. # salt-vagrant config
  253. config.vm.provision :salt do |salt|
  254. salt.run_highstate = true
  255. salt.minion_config = "/etc/salt/minion"
  256. salt.minion_key = "./minion1.pem"
  257. salt.minion_pub = "./minion1.pub"
  258. end
  259. Now destroy the vm and recreate it from the /minion folder:
  260. .. code-block:: bash
  261. vagrant destroy
  262. vagrant up
  263. If everything is fine you should see the following message:
  264. .. code-block:: bash
  265. "Bootstrapping Salt... (this may take a while)
  266. Salt successfully configured and installed!"
  267. Checking Master-Minion Communication
  268. ------------------------------------
  269. To make sure the master and minion are talking to each other, enter the
  270. following:
  271. .. code-block:: bash
  272. sudo salt '*' test.version
  273. You should see your minion answering with its salt version. It's now time to do some
  274. configuration.
  275. Step 4 - Configure Services to Install On the Minion
  276. ====================================================
  277. In this step we'll use the Salt master to instruct our minion to install
  278. Nginx.
  279. Checking the system's original state
  280. ------------------------------------
  281. First, make sure that an HTTP server is not installed on our minion.
  282. When opening a browser directed at ``http://192.168.33.10/`` You should get an
  283. error saying the site cannot be reached.
  284. Initialize the top.sls file
  285. ---------------------------
  286. System configuration is done in ``/srv/salt/top.sls`` (and subfiles/folders),
  287. and then applied by running the :py:func:`state.apply
  288. <salt.modules.state.apply_>` function to have the Salt master order its minions
  289. to update their instructions and run the associated commands.
  290. First Create an empty file on your Salt master (macOS machine):
  291. .. code-block:: bash
  292. touch /srv/salt/top.sls
  293. When the file is empty, or if no configuration is found for our minion
  294. an error is reported:
  295. .. code-block:: bash
  296. sudo salt 'minion1' state.apply
  297. This should return an error stating: **No Top file or external nodes data
  298. matches found**.
  299. Create The Nginx Configuration
  300. ------------------------------
  301. Now is finally the time to enter the real meat of our server's configuration.
  302. For this tutorial our minion will be treated as a web server that needs to
  303. have Nginx installed.
  304. Insert the following lines into ``/srv/salt/top.sls`` (which should current be
  305. empty).
  306. .. code-block:: yaml
  307. base:
  308. 'minion1':
  309. - bin.nginx
  310. Now create ``/srv/salt/bin/nginx.sls`` containing the following:
  311. .. code-block:: yaml
  312. nginx:
  313. pkg.installed:
  314. - name: nginx
  315. service.running:
  316. - enable: True
  317. - reload: True
  318. Check Minion State
  319. ------------------
  320. Finally, run the :py:func:`state.apply <salt.modules.state.apply_>` function
  321. again:
  322. .. code-block:: bash
  323. sudo salt 'minion1' state.apply
  324. You should see a log showing that the Nginx package has been installed
  325. and the service configured. To prove it, open your browser and navigate to
  326. http://192.168.33.10/, you should see the standard Nginx welcome page.
  327. Congratulations!
  328. Where To Go From Here
  329. =====================
  330. A full description of configuration management within Salt (sls files among
  331. other things) is available here:
  332. http://docs.saltstack.com/en/latest/index.html#configuration-management