Manage multiple server & client computers with SaltStack (finnish)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

685 lines
20 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. Central management of multiple servers - Exercise 1
  2. ==============
  3. *Disclaimer:*
  4. --------------
  5. This exercise is a part of [Configuration Management (ICT4TN022, spring 2018) // Palvelinten hallinta (ICT4TN022, kevät 2018)](http://www.haaga-helia.fi/fi/opinto-opas/opintojaksokuvaukset/ICT4TN022) school course organized as a part of Information Technology studies in Haaga-Helia university of Applied Sciences, Helsinki, Finland. Course lecturer [Tero Karvinen](http://terokarvinen.com/) has defined the original assignment descriptions in Finnish presented in this document in English. Answers and translations have been written by Pekka Helenius (me, ~ Fincer).
  6. *Table of contents*
  7. --------------
  8. - [c) Install Salt master and minion using pull architecture (e.g. master takes server role). You can set up the master and the slave on the same computer. Test the configuration by issuing salt commands remotely.](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/exercises/h1.md#c-install-salt-master-and-minion-using-pull-architecture-eg-master-takes-server-role-you-can-set-up-the-master-and-the-slave-on-the-same-computer-test-the-configuration-by-issuing-salt-commands-remotely)
  9. - [d) Test a salt-state example by Laine or modify some existing salt state configuration. Test the Salt-state functionality. Be aware that Laine has some unifinished examples on his repository (such as Battlenet installation on Windows)](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/exercises/h1.md#d-test-a-salt-state-example-by-laine-or-modify-some-existing-salt-state-configuration-test-the-salt-state-functionality-be-aware-that-laine-has-some-unifinished-examples-on-his-repository-such-as-battlenet-installation-on-windows)
  10. - [e) Collect system information from Salt minion computers by using Salt grains interface.](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/exercises/h1.md#e-collect-system-information-from-salt-minion-computers-by-using-salt-grains-interface)
  11. - [f) Real life test. Set up a real SaltStack configuration on your own computer or using your existing virtual server. (Note: It is recommended to test SaltStack in real life but if unsuccessful, configure a virtual environment for this assignment)](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/exercises/h1.md#f-real-life-test-set-up-a-real-saltstack-configuration-on-your-own-computer-or-using-your-existing-virtual-server-note-it-is-recommended-to-test-saltstack-in-real-life-but-if-unsuccessful-configure-a-virtual-environment-for-this-assignment)
  12. **c)** Install Salt master and minion using pull architecture (e.g. master takes server role). You can set up the master and the slave on the same computer. Test the configuration by issuing salt commands remotely.
  13. --------------
  14. **Answer:**
  15. Both `salt-master` and `salt-ssh` (+ SSH server daemon) should be installed on the host computer, `salt-minion` and SSH client on the client computer. We install both Salt minion and master to the same computer in this assignment.
  16. ```
  17. sudo apt-get update && sudo apt-get install salt-common salt-master salt-minion salt-ssh ssh
  18. ```
  19. Start Salt master service:
  20. ```
  21. sudo systemctl enable salt-master.service
  22. sudo systemctl start salt-master.service
  23. ```
  24. Check status of the Salt master service:
  25. ```
  26. systemctl is-active salt-master.service
  27. ```
  28. or
  29. ```
  30. systemctl status salt-master.service
  31. ```
  32. Start Salt minion service:
  33. ```
  34. sudo systemctl enable salt-minion.service
  35. sudo systemctl start salt-minion.service
  36. ```
  37. Check status of the Salt minion service:
  38. ```
  39. systemctl is-active salt-minion.service
  40. ```
  41. or
  42. ```
  43. systemctl status salt-minion.service
  44. ```
  45. Find out Salt master IP address by issuing `ifconfig` command*. IP address is the value of `inet` field in output.
  46. *On Debian-based Linux distributions, it is possible to find out IP addresses by issuing `hostname -I` command (see [hostname -I](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=562830)) as this command is not widely available on other Linux distributions by default.
  47. We shall configure Salt master IP address (or hostname) for the Salt minion service by editing Salt minion configuration file `/etc/salt/minion`. In addition, we shall add an ID `slave` to the minion service.
  48. Issue command `sudo nano /etc/salt/minion` or `sudoedit /etc/salt/minion` and add the following entries:
  49. ```
  50. master: 127.0.0.1
  51. id: slave
  52. ```
  53. Restart Salt minion service:
  54. ```
  55. sudo systemctl restart salt-minion.service
  56. ```
  57. Run the following commands in your Salt master:
  58. ```
  59. sudo salt-key
  60. sudo salt-key -A
  61. ```
  62. Sample output:
  63. ```
  64. fincer@computer:~$ sudo salt-key
  65. Accepted Keys:
  66. Denied Keys:
  67. Unaccepted Keys:
  68. slave
  69. Rejected Keys:
  70. fincer@computer:~$ sudo salt-key -A
  71. The following keys are going to be accepted:
  72. Unaccepted Keys:
  73. slave
  74. Proceed? [n/Y] y
  75. Key for minion slave accepted.
  76. ```
  77. Restart Salt minion again (may not be necessary):
  78. ```
  79. sudo systemctl restart salt-minion.service
  80. ```
  81. Run a sample command in order to test the Salt master/minion configuration:
  82. ```
  83. fincer@computer:~$ sudo salt '*' cmd.run "ls /boot"
  84. slave:
  85. System.map-4.15.0-13-generic
  86. abi-4.15.0-13-generic
  87. config-4.15.0-13-generic
  88. grub
  89. initrd.img-4.15.0-13-generic
  90. memtest86+.bin
  91. memtest86+.elf
  92. memtest86+_multiboot.bin
  93. retpoline-4.15.0-13-generic
  94. vmlinuz-4.15.0-13-generic
  95. ```
  96. **d)** Test a salt-state example by Laine or modify some existing salt state configuration. Test the Salt-state functionality. Be aware that Laine has some unifinished examples on his repository (such as Battlenet installation on Windows)
  97. --------------
  98. **Answer:**
  99. Create folder path `/srv/salt` on the Salt master:
  100. ```
  101. sudo mkdir -p /srv/salt
  102. ```
  103. Add new Salt state files `/srv/salt/top.sls` and `/srv/salt/firewall.sls`:
  104. ```
  105. sudo touch /srv/salt/{top.sls,firewall.sls}
  106. ```
  107. Add the following contents into the `/srv/salt/firewall.sls`:
  108. ```
  109. ufw:
  110. pkg.installed
  111. ufw-enable:
  112. cmd.run:
  113. - name: 'ufw --force enable'
  114. - require:
  115. - pkg: ufw
  116. ```
  117. Reference: [joonaleppalahti - firewall.sls](https://github.com/joonaleppalahti/CCM/blob/master/salt/srv/salt/firewall.sls)
  118. Add the following contents into the `/srv/salt/top.sls`:
  119. ```
  120. base:
  121. 'slave':
  122. - firewall
  123. ```
  124. where `slave` refers to a minion ID* on the network, and `firewall` refers to the Salt state file `/srv/salt/firewall.sls` found on the Salt master. In this case, contents and configurations declared in `/srv/salt/firewall.sls` are supplied to the minion `slave`.
  125. *Salt accepts regular expressions in minion ID field and, therefore, allows multiple minions to be matched. Any matching Salt minion on the network gets the configuration defined by Salt master. For instance, the following `/srv/salt/top.sls` configuration would match any minion ID starting with `slave` phrase (e.g. slave01, slavea, slave534, slave4 etc.)
  126. ```
  127. base:
  128. 'slave*':
  129. - firewall
  130. ```
  131. Run the following command on Salt master:
  132. ```
  133. sudo salt '*' state.highstate
  134. ```
  135. The previous command should print the following output:
  136. ```
  137. fincer@computer:~$ sudo salt '*' state.highstate
  138. slave:
  139. ----------
  140. ID: ufw
  141. Function: pkg.installed
  142. Result: True
  143. Comment: All specified packages are already installed
  144. Started: 13:28:16.290668
  145. Duration: 858.482 ms
  146. Changes:
  147. ----------
  148. ID: ufw-enable
  149. Function: cmd.run
  150. Name: ufw --force enable
  151. Result: True
  152. Comment: Command "ufw --force enable" run
  153. Started: 13:28:17.190531
  154. Duration: 843.309 ms
  155. Changes:
  156. ----------
  157. pid:
  158. 10265
  159. retcode:
  160. 0
  161. stderr:
  162. stdout:
  163. Firewall is active and enabled on system startup
  164. Summary for slave
  165. ------------
  166. Succeeded: 2 (changed=1)
  167. Failed: 0
  168. ------------
  169. Total states run: 2
  170. Total run time: 1.718 s
  171. ```
  172. UFW firewall was already installed on the minion computer but it was not enabled properly. Therefore, only one modification was applied to the Salt minion computer configuration, although two commands were issued.
  173. **e)** Collect system information from Salt minion computers by using Salt grains interface.
  174. --------------
  175. **Answer:**
  176. Grains of every Salt minion (which have been accepted beforehand by the Salt master) can be printed out by issuing the following command on the Salt master:
  177. ```
  178. sudo salt '*' grains.ls
  179. ```
  180. Sample output, returned by a Salt minion (asterix (`*`) is a regex for matching any character, i.e. any Salt minion ID):
  181. ```
  182. fincer@computer:~$ sudo salt '*' grains.items | grep saltversion -C 4
  183. - candidate
  184. - 1
  185. saltpath:
  186. /usr/lib/python3/dist-packages/salt
  187. saltversion:
  188. 2017.7.4
  189. saltversioninfo:
  190. - 2017
  191. - 7
  192. - 4
  193. - 0
  194. ```
  195. Systemd information of a Salt minion (supported features and version number):
  196. ```
  197. fincer@computer:~$ sudo salt '*' grains.item systemd
  198. slave:
  199. ----------
  200. systemd:
  201. ----------
  202. features:
  203. +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid
  204. version:
  205. 237
  206. ```
  207. etc.
  208. **f)** Real life test. Set up a real SaltStack configuration on your own computer or using your existing virtual server. (Note: It is recommended to test SaltStack in real life but if unsuccessful, configure a virtual environment for this assignment)
  209. --------------
  210. **Answer:**
  211. **NOTE:** `salt-ssh` was not used in this assignment!
  212. Let's set up two Salt computers on the same network. One takes Salt master role, the other one takes Salt minion role.
  213. - IP address of the Salt master is 10.11.11.101 (ifconfig)
  214. - IP address of the Salt minion is 10.11.11.102 (ifconfig)
  215. - IP addresses were determined with DHCP server.
  216. We shall set up Master and Minion system configurations by following instructions of the assignment [c)]((https://github.com/Fincer/central-management-of-multiple-servers/blob/master/exercises/h1.md#c-install-salt-master-and-minion-using-pull-architecture-eg-master-takes-server-role-you-can-set-up-the-master-and-the-slave-on-the-same-computer-test-the-configuration-by-issuing-salt-commands-remotely)). On the Salt minion, add Salt master IP and Salt minion ID into `/etc/salt/minion` file:
  217. ```
  218. master: 10.11.11.101
  219. id: slave_computer
  220. ```
  221. after which you should run the following command on the Salt master (NOTE: Salt minion may not be immediately listed by `salt-key` command):
  222. ```
  223. sudo salt-key && sudo salt-key -A
  224. ```
  225. Output:
  226. ```
  227. fincer@master:~$ sudo salt-key && sudo salt-key -A
  228. Accepted Keys:
  229. Denied Keys:
  230. Unaccepted Keys:
  231. slave_computer
  232. Rejected Keys:
  233. The following keys are going to be accepted:
  234. Unaccepted Keys:
  235. slave_computer
  236. Proceed? [n/Y] y
  237. Key for minion slave_computer accepted.
  238. ```
  239. Let's install LAMP server environment and insert `/var/www/html/index.php` to the Salt minion computer. Run the following commands on Salt master:
  240. ```
  241. sudo mkdir -p /srv/salt
  242. echo "<?php phpinfo(); ?>" | sudo tee /srv/salt/index.php
  243. sudo wget https://raw.githubusercontent.com/joonaleppalahti/CCM/master/salt/srv/salt/lamp.sls -O /srv/salt/lamp.sls
  244. sudo sed -i '14,18d; s/salt\:\/\/webserver\//salt\:\/\//' /srv/salt/lamp.sls
  245. sudo salt 'slave*' state.apply lamp
  246. ```
  247. Salt state file `lamp.sls` includes definitions for `index.html` which are not defined in this assignment. We delete those definitions by issuing `sed` command above. In addition, references to subfolder `webserver` are also deleted with `sed` command.
  248. Output of successfully executed `salt` command (run on the Salt master):
  249. ```
  250. fincer@master:~$ cat /srv/salt/lamp.sls
  251. install_lamp:
  252. pkg.installed:
  253. - pkgs:
  254. - apache2
  255. - libapache2-mod-php
  256. /var/www/html/index.php:
  257. file:
  258. - managed
  259. - source: salt://index.php
  260. - require:
  261. - pkg: install_lamp
  262. fincer@master:~$ sudo salt 'slave*' state.apply lamp
  263. slave_computer:
  264. ----------
  265. ID: install_lamp
  266. Function: pkg.installed
  267. Result: True
  268. Comment: 2 targeted packages were installed/updated.
  269. Started: 15:07:24.960653
  270. Duration: 50817.827 ms
  271. Changes:
  272. ----------
  273. apache2:
  274. ----------
  275. new:
  276. 2.4.29-1ubuntu4
  277. old:
  278. apache2-api-20120211:
  279. ----------
  280. new:
  281. 1
  282. old:
  283. apache2-api-20120211-openssl1.1:
  284. ----------
  285. new:
  286. 1
  287. old:
  288. apache2-bin:
  289. ----------
  290. new:
  291. 2.4.29-1ubuntu4
  292. old:
  293. apache2-data:
  294. ----------
  295. new:
  296. 2.4.29-1ubuntu4
  297. old:
  298. apache2-utils:
  299. ----------
  300. new:
  301. 2.4.29-1ubuntu4
  302. old:
  303. httpd:
  304. ----------
  305. new:
  306. 1
  307. old:
  308. httpd-cgi:
  309. ----------
  310. new:
  311. 1
  312. old:
  313. libapache2-mod-php:
  314. ----------
  315. new:
  316. 1:7.2+60ubuntu1
  317. old:
  318. libapache2-mod-php7.2:
  319. ----------
  320. new:
  321. 7.2.3-1ubuntu1
  322. old:
  323. libapr1:
  324. ----------
  325. new:
  326. 1.6.3-2
  327. old:
  328. libaprutil1:
  329. ----------
  330. new:
  331. 1.6.1-2
  332. old:
  333. libaprutil1-dbd-sqlite3:
  334. ----------
  335. new:
  336. 1.6.1-2
  337. old:
  338. libaprutil1-ldap:
  339. ----------
  340. new:
  341. 1.6.1-2
  342. old:
  343. liblua5.2-0:
  344. ----------
  345. new:
  346. 5.2.4-1.1build1
  347. old:
  348. php-calendar:
  349. ----------
  350. new:
  351. 1
  352. old:
  353. php-cli:
  354. ----------
  355. new:
  356. 1
  357. old:
  358. php-common:
  359. ----------
  360. new:
  361. 1:60ubuntu1
  362. old:
  363. php-ctype:
  364. ----------
  365. new:
  366. 1
  367. old:
  368. php-exif:
  369. ----------
  370. new:
  371. 1
  372. old:
  373. php-fileinfo:
  374. ----------
  375. new:
  376. 1
  377. old:
  378. php-ftp:
  379. ----------
  380. new:
  381. 1
  382. old:
  383. php-iconv:
  384. ----------
  385. new:
  386. 1
  387. old:
  388. php-json:
  389. ----------
  390. new:
  391. 1
  392. old:
  393. php-opcache:
  394. ----------
  395. new:
  396. 1
  397. old:
  398. php-pdo:
  399. ----------
  400. new:
  401. 1
  402. old:
  403. php-phar:
  404. ----------
  405. new:
  406. 1
  407. old:
  408. php-posix:
  409. ----------
  410. new:
  411. 1
  412. old:
  413. php-readline:
  414. ----------
  415. new:
  416. 1
  417. old:
  418. php-shmop:
  419. ----------
  420. new:
  421. 1
  422. old:
  423. php-sockets:
  424. ----------
  425. new:
  426. 1
  427. old:
  428. php-sysvmsg:
  429. ----------
  430. new:
  431. 1
  432. old:
  433. php-sysvsem:
  434. ----------
  435. new:
  436. 1
  437. old:
  438. php-sysvshm:
  439. ----------
  440. new:
  441. 1
  442. old:
  443. php-tokenizer:
  444. ----------
  445. new:
  446. 1
  447. old:
  448. php7.2-calendar:
  449. ----------
  450. new:
  451. 1
  452. old:
  453. php7.2-cli:
  454. ----------
  455. new:
  456. 7.2.3-1ubuntu1
  457. old:
  458. php7.2-common:
  459. ----------
  460. new:
  461. 7.2.3-1ubuntu1
  462. old:
  463. php7.2-ctype:
  464. ----------
  465. new:
  466. 1
  467. old:
  468. php7.2-exif:
  469. ----------
  470. new:
  471. 1
  472. old:
  473. php7.2-fileinfo:
  474. ----------
  475. new:
  476. 1
  477. old:
  478. php7.2-ftp:
  479. ----------
  480. new:
  481. 1
  482. old:
  483. php7.2-gettext:
  484. ----------
  485. new:
  486. 1
  487. old:
  488. php7.2-iconv:
  489. ----------
  490. new:
  491. 1
  492. old:
  493. php7.2-json:
  494. ----------
  495. new:
  496. 7.2.3-1ubuntu1
  497. old:
  498. php7.2-opcache:
  499. ----------
  500. new:
  501. 7.2.3-1ubuntu1
  502. old:
  503. php7.2-pdo:
  504. ----------
  505. new:
  506. 1
  507. old:
  508. php7.2-phar:
  509. ----------
  510. new:
  511. 1
  512. old:
  513. php7.2-posix:
  514. ----------
  515. new:
  516. 1
  517. old:
  518. php7.2-readline:
  519. ----------
  520. new:
  521. 7.2.3-1ubuntu1
  522. old:
  523. php7.2-shmop:
  524. ----------
  525. new:
  526. 1
  527. old:
  528. php7.2-sockets:
  529. ----------
  530. new:
  531. 1
  532. old:
  533. php7.2-sysvmsg:
  534. ----------
  535. new:
  536. 1
  537. old:
  538. php7.2-sysvsem:
  539. ----------
  540. new:
  541. 1
  542. old:
  543. php7.2-sysvshm:
  544. ----------
  545. new:
  546. 1
  547. old:
  548. php7.2-tokenizer:
  549. ----------
  550. new:
  551. 1
  552. old:
  553. phpapi-20170718:
  554. ----------
  555. new:
  556. 1
  557. old:
  558. ----------
  559. ID: /var/www/html/index.php
  560. Function: file.managed
  561. Result: True
  562. Comment: File /var/www/html/index.php updated
  563. Started: 15:08:15.783571
  564. Duration: 67.439 ms
  565. Changes:
  566. ----------
  567. diff:
  568. New file
  569. mode:
  570. 0644
  571. Summary for slave_computer
  572. ------------
  573. Succeeded: 2 (changed=2)
  574. Failed: 0
  575. ------------
  576. Total states run: 2
  577. Total run time: 50.885 s
  578. ```
  579. And:
  580. ```
  581. fincer@master:~$ sudo salt 'slave_computer' cmd.run 'dpkg --get-selections | grep apache'
  582. slave_computer:
  583. apache2 install
  584. apache2-bin install
  585. apache2-data install
  586. apache2-utils install
  587. libapache2-mod-php install
  588. libapache2-mod-php7.2 install
  589. ```