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.

504 lines
17 KiB

  1. Palvelinten hallinta - Harjoitus 4
  2. ==============
  3. *Disclaimer:*
  4. --------------
  5. Tämä harjoitus on tehty osana Haaga-Helian Tietojenkäsittelyn koulutusohjelman kurssia [Palvelinten hallinta (ICT4TN022, kevät 2018)](http://www.haaga-helia.fi/fi/opinto-opas/opintojaksokuvaukset/ICT4TN022). Kurssin pitäjänä toimii [Tero Karvinen](http://terokarvinen.com/), joka on määritellyt tämän harjoituksen tehtävänkuvaukset. Tehtävien vastaukset ovat Pekka Heleniuksen (allekirjoittanut) tuottamia.
  6. *SISÄLLYSLUETTELO*
  7. --------------
  8. - [Harjoituksen esivaatimus - kahden minion-koneen asentaminen Vagrantilla](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/h4.md#harjoituksen-esivaatimus---kahden-minion-koneen-asentaminen-vagrantilla)
  9. - [b) Tee kahdella orjalla esimerkki, jossa orjat saavat eri muuttujan pilarista. Tarkista ‘pillars.items’, että kummalekin orjalle mene eri tieto. Tee jokin muu kuin tunnilla tehty sshd-esimerkki.](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/h4.md#b-tee-kahdella-orjalla-esimerkki-jossa-orjat-saavat-eri-muuttujan-pilarista-tarkista-pillarsitems-ett%C3%A4-kummalekin-orjalle-mene-eri-tieto-tee-jokin-muu-kuin-tunnilla-tehty-sshd-esimerkki)
  10. - [c) Tee kahdella orjalla esimerkki, jossa toinen orja saa muuttujan pilarista ja toinen käyttää oletusarvoa (pillar.get). Tee jokin muu kuin tunnilla tehty sshd-esimerkki.](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/h4.md#c-tee-kahdella-orjalla-esimerkki-jossa-toinen-orja-saa-muuttujan-pilarista-ja-toinen-k%C3%A4ytt%C3%A4%C3%A4-oletusarvoa-pillarget-tee-jokin-muu-kuin-tunnilla-tehty-sshd-esimerkki)
  11. Harjoituksen esivaatimuksena on käyttää useampaa Minion-konetta Salt:lla. Useamman Salt-minionin luomiseen on monta eri tapaa, kuten
  12. - 1) luodaan jokaiselle minionille oma virtuaalikoneensa (Oracle VirtualBox, VMWare, Vagrant...), jotka kykenevät muodostamaan yhteyden Master-koneeseen
  13. - 2) luodaan jokaiselle minionille oma konttinsa (docker, systemd-nspawn), jotka kykenevät muodostamaan yhteyden Master-koneeseen
  14. - 3) käytetään olemassa olevia virtuaalikoneita, jotka pyörivät esimerkiksi palvelinsaleissa
  15. - 4) luodaan joko fyysiselle tai virtuaaliselle tietokoneelle useampi minion-konfiguraatio omine ID:ineen (huonoin!)
  16. Toteutetaan vaihtoehto 1) Vagrantilla.
  17. Harjoituksen esivaatimus - kahden minion-koneen asentaminen Vagrantilla
  18. --------------
  19. Toteutetaan minion-koneet [Vagrant-virtualisoinnilla](https://en.wikipedia.org/wiki/Vagrant_(software)).
  20. Tässä vaiheessa oletetaan, että koneelta löytyy Salt-master jo valmiina (ks. mm. [harjoitus 1](https://raw.githubusercontent.com/Fincer/central-management-of-multiple-servers/master/h1.md)).
  21. Asennetaan vagrant
  22. ```
  23. sudo apt-get -y install vagrant
  24. ```
  25. Tietokoneelta löytyy ennestään _virtualbox_ asennettuna, mutta se voidaan asentaa myös:
  26. ```
  27. sudo apt-get -y install virtualbox
  28. ```
  29. Vagrant käyttää laatikoita (boxes) virtualisoitujen ympäristöjen toteuttamisessa. Käytän harjoituksessa laatikkoa [debian/jessie64](https://app.vagrantup.com/debian/boxes/jessie64). Tässä täytyy olla tarkkana, ettei käytettyjen Salt:ien versiot ja API:t rikkoonnu! Tuotantoympäristössä nämä on tarkistettava ja katsottava huolella!
  30. Luodaan kaksi Salt minion -konetta ajamalla seuraava Vagrantfile Master-koneen myöhemmin valittavassa alihakemistossa:
  31. [Vagrantfile - Multiple Salt minions](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/scripts/vagrant_salt-minions/Vagrantfile)
  32. **HUOM!** Vagrant-ratkaisu ei toimi hyvin jo-virtualisoiduissa työympäristöissä, eli Vagrantia tulisi ajaa "fyysisellä" koneella.
  33. Ladataan yllä mainittu Vagrantfile master-koneen alihakemistoon /srv/salt/vagrant_minions:
  34. ```
  35. sudo mkdir -p /srv/salt/vagrant_minions
  36. sudo wget https://raw.githubusercontent.com/Fincer/central-management-of-multiple-servers/master/scripts/vagrant_salt-minions/Vagrantfile -O /srv/salt/vagrant_minions/Vagrantfile
  37. ```
  38. Ladatussa Vagrantfile:ssä on jo ennestään määritetty luotavaksi kaksi Salt-minionia: minion_1 ja minion_2 laatikkotemplatella debian/jessie64.
  39. **HUOM!** Monet _vagrant_ -komennot on sidottu työhakemistoon. Ennestään luodut virtuaalikoneet tulee ne tuhota siitä hakemistosta käsin, jossa koneet on luotu (ja siis jossa Vagrantfile sijaitsee). Vagrant luo tähän hakemistoon piilohakemiston _.vagrant/machines_, johon koneet on luotu.
  40. Annetaan master-koneen normaalille ylläpitäjälle (UID: 1000, GID: 1000 ja kuuluu sudo-ryhmään) omistusoikeus kansioon /srv/salt/vagrant_minions:
  41. ```
  42. sudo chown 1000:1000 -R /srv/salt/vagrant_minions
  43. ```
  44. Luodaan tällä käyttäjällä tuohon hakemistoon viittaava symbolinen linkki kys. käyttäjän kotihakemistoon:
  45. ```
  46. ln -s /srv/salt/vagrant_minions $HOME/vagrant-minions
  47. ```
  48. **HUOM!** Edellä olevaa ei ole pakko tehdä, ja se on täysin vapaaehtoista. Halusin tehdä sen selkeyden vuoksi.
  49. Ajetaan ladattu Vagrantfile:
  50. ```
  51. cd $HOME/vagrant-minions
  52. vagrant up
  53. ```
  54. Testataan laatikoiden minion_1 ja minion_2 SSH-toimivuus ajamalla:
  55. ```
  56. vagrant ssh minion_1
  57. vagrant ssh minion_2
  58. ```
  59. ![vagrant-connection-test](https://raw.githubusercontent.com/Fincer/central-management-of-multiple-servers/master/images/vagrant-connection-test.png)
  60. Hyväksytään nämä minion-koneet masterilla:
  61. ```
  62. [23/04/2018 19:24:57 - fincer: vagrant ]$ sudo salt-key -A
  63. The following keys are going to be accepted:
  64. Unaccepted Keys:
  65. minion_1
  66. minion_2
  67. Proceed? [n/Y] y
  68. Key for minion minion_1 accepted.
  69. Key for minion minion_2 accepted.
  70. ```
  71. **b)** Tee kahdella orjalla esimerkki, jossa orjat saavat eri muuttujan pilarista. Tarkista ‘pillars.items’, että kummalekin orjalle mene eri tieto. Tee jokin muu kuin tunnilla tehty sshd-esimerkki.
  72. --------------
  73. **Vastaus:**
  74. Ajetaan root-käyttäjänä (sudo-komento) seuraava shell-skripti master-koneella:
  75. [Pekka Helenius - salt_pillar_sample.sh](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/scripts/salt_pillar_sample.sh)
  76. Masterilta saatu output:
  77. ```
  78. [23/04/2018 19:41:45 - fincer: vagrant ]$ sudo sh pillar_sample.sh
  79. base:
  80. 'minion_1':
  81. - minion-1
  82. 'minion_2':
  83. - minion-2
  84. test_variable: 'secret like coffee shop wants to say hello to the world'
  85. test_variable: 'hidden miniart: superman vs. hulk figures'
  86. This is my pillarfile which has the following content:
  87. {{ pillar['test_variable'] }}
  88. pillar_file:
  89. file.managed:
  90. - user: 1000
  91. - group: 1000
  92. - name: /tmp/pillarfile_for_{{ grains['id'] }}
  93. - source: salt://files/pillarfile
  94. - makedirs: True
  95. - template: jinja
  96. minion_2:
  97. True
  98. minion_1:
  99. True
  100. **Salt -- pillar.items output**
  101. minion_2:
  102. ----------
  103. test_variable:
  104. hidden miniart: superman vs. hulk figures
  105. minion_1:
  106. ----------
  107. test_variable:
  108. secret like coffee shop wants to say hello to the world
  109. **Salt -- state.apply output**
  110. minion_1:
  111. ----------
  112. ID: pillar_file
  113. Function: file.managed
  114. Name: /tmp/pillarfile_for_minion_1
  115. Result: True
  116. Comment: File /tmp/pillarfile_for_minion_1 is in the correct state
  117. Started: 16:42:18.786164
  118. Duration: 54.806 ms
  119. Changes:
  120. Summary for minion_1
  121. ------------
  122. Succeeded: 1
  123. Failed: 0
  124. ------------
  125. Total states run: 1
  126. Total run time: 54.806 ms
  127. minion_2:
  128. ----------
  129. ID: pillar_file
  130. Function: file.managed
  131. Name: /tmp/pillarfile_for_minion_2
  132. Result: True
  133. Comment: File /tmp/pillarfile_for_minion_2 is in the correct state
  134. Started: 16:42:18.886370
  135. Duration: 57.26 ms
  136. Changes:
  137. Summary for minion_2
  138. ------------
  139. Succeeded: 1
  140. Failed: 0
  141. ------------
  142. Total states run: 1
  143. Total run time: 57.260 ms
  144. **Salt -- get file output with head command**
  145. minion_2:
  146. This is my pillarfile which has the following content:
  147. hidden miniart: superman vs. hulk figures
  148. minion_1:
  149. This is my pillarfile which has the following content:
  150. secret like coffee shop wants to say hello to the world
  151. ```
  152. **c)** Tee kahdella orjalla esimerkki, jossa toinen orja saa muuttujan pilarista ja toinen käyttää oletusarvoa (pillar.get). Tee jokin muu kuin tunnilla tehty sshd-esimerkki.
  153. --------------
  154. **Vastaus:**
  155. Asennetaan molemmille orjille Apache HTTP daemon seuraavaa shell-skriptiä käyttäen:
  156. [salt_pillar_apache_sample.sh](https://github.com/Fincer/central-management-of-multiple-servers/blob/master/scripts/salt_pillar_apache_sample.sh)
  157. Masterilta saatu output:
  158. ```
  159. [24/04/2018 01:57:10 - fincer: vagrant ]$ sudo sh apache_sample.sh
  160. {% if grains['id'] == 'minion_2' %}
  161. site_data: '{{ salt['cmd.run']('uname -a') }}'
  162. {% endif %}
  163. <VirtualHost *:80>
  164. ServerName {{ servername }}
  165. ServerAlias {{ serveralias }}
  166. ServerAdmin webmaster@localhost
  167. DocumentRoot {{ ('/var/www/html/' + grains['id'] + '/') }}
  168. ErrorLog /error.log
  169. CustomLog /access.log combined
  170. </VirtualHost>
  171. {{ pillar.get('site_data','Nothing interesting here') }}
  172. {% set servername = grains['os'].lower() + '.' + grains['id'] + '.com' %}
  173. {% set serveralias = 'www.' + grains['os'].lower() + '.' + grains['id'] + '.com' %}
  174. apache_install:
  175. pkg.installed:
  176. - pkgs:
  177. - apache2
  178. - curl
  179. sample_page_conf:
  180. file.managed:
  181. - name: /etc/apache2/sites-available/{{ grains['id'] }}.conf
  182. - source: salt://apache/samplesite.conf
  183. - mode: 0644
  184. - user: root
  185. - group: root
  186. - template: jinja
  187. - context:
  188. servername: {{ servername }}
  189. serveralias: {{ serveralias }}
  190. - require:
  191. - pkg: apache_install
  192. enable_sample_page:
  193. cmd.run:
  194. - name: 'a2ensite {{ grains['id'] }}.conf'
  195. - require:
  196. - file: sample_page_conf
  197. sample_page_content:
  198. file.managed:
  199. - mode: 0644
  200. - user: root
  201. - group: root
  202. - makedirs: True
  203. - template: jinja
  204. - name: {{ ('/var/www/html/' + grains['id'] + '/index.html') }}
  205. - source: salt://apache/sampleindex.html
  206. - require:
  207. - cmd: enable_sample_page
  208. add_vhost_domain:
  209. file.append:
  210. - name: /etc/hosts
  211. - text: 127.0.0.1 {{ servername }}
  212. - require:
  213. - file: sample_page_content
  214. restart_httpd:
  215. service.running:
  216. - name: apache2.service
  217. - watch:
  218. - file: add_vhost_domain
  219. - cmd: enable_sample_page
  220. test_page:
  221. cmd.run:
  222. - name: 'curl -s {{ servername }}'
  223. - require:
  224. - service: restart_httpd
  225. **Salt -- pillar.items output**
  226. minion_1:
  227. ----------
  228. test_variable:
  229. secret like coffee shop wants to say hello to the world
  230. minion_2:
  231. ----------
  232. site_data:
  233. Linux fincer-laptop 4.15.10-1-ARCH #1 SMP PREEMPT Thu Mar 15 12:24:34 UTC 2018 x86_64 GNU/Linux
  234. test_variable:
  235. hidden miniart: superman vs. hulk figures
  236. **Salt -- state.apply output**
  237. minion_1:
  238. ----------
  239. ID: apache_install
  240. Function: pkg.installed
  241. Result: True
  242. Comment: All specified packages are already installed
  243. Started: 22:59:25.325084
  244. Duration: 273.26 ms
  245. Changes:
  246. ----------
  247. ID: sample_page_conf
  248. Function: file.managed
  249. Name: /etc/apache2/sites-available/minion_1.conf
  250. Result: True
  251. Comment: File /etc/apache2/sites-available/minion_1.conf is in the correct state
  252. Started: 22:59:25.599368
  253. Duration: 51.619 ms
  254. Changes:
  255. ----------
  256. ID: enable_sample_page
  257. Function: cmd.run
  258. Name: a2ensite minion_1.conf
  259. Result: True
  260. Comment: Command "a2ensite minion_1.conf" run
  261. Started: 22:59:25.651513
  262. Duration: 20.174 ms
  263. Changes:
  264. ----------
  265. pid:
  266. 13401
  267. retcode:
  268. 0
  269. stderr:
  270. stdout:
  271. Site minion_1 already enabled
  272. ----------
  273. ID: sample_page_content
  274. Function: file.managed
  275. Name: /var/www/html/minion_1/index.html
  276. Result: True
  277. Comment: File /var/www/html/minion_1/index.html is in the correct state
  278. Started: 22:59:25.671991
  279. Duration: 9.937 ms
  280. Changes:
  281. ----------
  282. ID: add_vhost_domain
  283. Function: file.append
  284. Name: /etc/hosts
  285. Result: True
  286. Comment: File /etc/hosts is in correct state
  287. Started: 22:59:25.682126
  288. Duration: 1.011 ms
  289. Changes:
  290. ----------
  291. ID: restart_httpd
  292. Function: service.running
  293. Name: apache2.service
  294. Result: True
  295. Comment: Service restarted
  296. Started: 22:59:25.697483
  297. Duration: 2145.224 ms
  298. Changes:
  299. ----------
  300. apache2.service:
  301. True
  302. ----------
  303. ID: test_page
  304. Function: cmd.run
  305. Name: curl -s ubuntu.minion_1.com
  306. Result: True
  307. Comment: Command "curl -s ubuntu.minion_1.com" run
  308. Started: 22:59:27.842981
  309. Duration: 9.994 ms
  310. Changes:
  311. ----------
  312. pid:
  313. 13523
  314. retcode:
  315. 0
  316. stderr:
  317. stdout:
  318. Nothing interesting here
  319. Summary for minion_1
  320. ------------
  321. Succeeded: 7 (changed=3)
  322. Failed: 0
  323. ------------
  324. Total states run: 7
  325. Total run time: 2.511 s
  326. minion_2:
  327. ----------
  328. ID: apache_install
  329. Function: pkg.installed
  330. Result: True
  331. Comment: All specified packages are already installed
  332. Started: 22:59:25.453349
  333. Duration: 273.908 ms
  334. Changes:
  335. ----------
  336. ID: sample_page_conf
  337. Function: file.managed
  338. Name: /etc/apache2/sites-available/minion_2.conf
  339. Result: True
  340. Comment: File /etc/apache2/sites-available/minion_2.conf is in the correct state
  341. Started: 22:59:25.728257
  342. Duration: 10.512 ms
  343. Changes:
  344. ----------
  345. ID: enable_sample_page
  346. Function: cmd.run
  347. Name: a2ensite minion_2.conf
  348. Result: True
  349. Comment: Command "a2ensite minion_2.conf" run
  350. Started: 22:59:25.739209
  351. Duration: 18.428 ms
  352. Changes:
  353. ----------
  354. pid:
  355. 13160
  356. retcode:
  357. 0
  358. stderr:
  359. stdout:
  360. Site minion_2 already enabled
  361. ----------
  362. ID: sample_page_content
  363. Function: file.managed
  364. Name: /var/www/html/minion_2/index.html
  365. Result: True
  366. Comment: File /var/www/html/minion_2/index.html is in the correct state
  367. Started: 22:59:25.757909
  368. Duration: 8.736 ms
  369. Changes:
  370. ----------
  371. ID: add_vhost_domain
  372. Function: file.append
  373. Name: /etc/hosts
  374. Result: True
  375. Comment: File /etc/hosts is in correct state
  376. Started: 22:59:25.766840
  377. Duration: 0.933 ms
  378. Changes:
  379. ----------
  380. ID: restart_httpd
  381. Function: service.running
  382. Name: apache2.service
  383. Result: True
  384. Comment: Service restarted
  385. Started: 22:59:25.779507
  386. Duration: 2102.595 ms
  387. Changes:
  388. ----------
  389. apache2.service:
  390. True
  391. ----------
  392. ID: test_page
  393. Function: cmd.run
  394. Name: curl -s ubuntu.minion_2.com
  395. Result: True
  396. Comment: Command "curl -s ubuntu.minion_2.com" run
  397. Started: 22:59:27.882439
  398. Duration: 11.578 ms
  399. Changes:
  400. ----------
  401. pid:
  402. 13282
  403. retcode:
  404. 0
  405. stderr:
  406. stdout:
  407. Linux fincer-laptop 4.15.10-1-ARCH #1 SMP PREEMPT Thu Mar 15 12:24:34 UTC 2018 x86_64 GNU/Linux
  408. Summary for minion_2
  409. ------------
  410. Succeeded: 7 (changed=3)
  411. Failed: 0
  412. ------------
  413. Total states run: 7
  414. Total run time: 2.427 s
  415. ```
  416. Luotujen HTML-sivustojen sisältö:
  417. ```
  418. [24/04/2018 01:59:28 - fincer: vagrant ]$ sudo salt 'minion_1' cmd.run 'curl -s ubuntu.minion_1.com'
  419. minion_1:
  420. Nothing interesting here
  421. [24/04/2018 02:00:47 - fincer: vagrant ]$ sudo salt 'minion_2' cmd.run 'curl -s ubuntu.minion_2.com'
  422. minion_2:
  423. Linux fincer-laptop 4.15.10-1-ARCH #1 SMP PREEMPT Thu Mar 15 12:24:34 UTC 2018 x86_64 GNU/Linux
  424. ```