Palvelinten hallinta kurssi – harjoitus 1
Harjoitus on osa kurssia Palvelinten hallinta (http://terokarvinen.com/2020/configuration-managment-systems-palvelinten-hallinta-ict4tn022-spring-2020/)
h1 Hei maailma, verkon yli ja idempotenssi.
Tein harjoituksen virtuaalipalvelimelle, jotka loin DigitalOceaniin (toinen oli jo olemassa Linux-kurssin jäljiltä).
Aloitin harjoituksen tekemisen n. 16.15 ja lopetin klo 17.35.
a) Asenna Salt ja siihen uusi orja. Voit tehdä ne esimerkiksi uudelle virtuaalikoneelle, niin pääset kokeilemaan puhtaalta pöydältä.
Käytin tätä sivua apuna: http://terokarvinen.com/2018/salt-quickstart-salt-stack-master-and-slave-on-ubuntu-linux
Loin ensin DigitalOceaniin uuden Linux-palvelimen, josta teen masterin. Aiemmalla Linux-kurssilla tehdystä palvelimesta teen sen slaven.
Seuraavat vaiheet tein vuorotellen aina, kun niiden vuoro oli:
Master:
- sudo apt-get -y install salt-master
- sudo ufw allow 4505/tcp
- sudo ufw allow 4506/tcp
- sudo salt-key -A
- unaccepted keys: slave1
- proceed? [n/Y]
- y
- key for minion slave1 accepted
- sudo salt ’*’ cmd.run ’whoami’
- slave1:
- root
- slave1:
Slave
- sudo apt-get -y install salt-minion
- sudoedit /etc/salt/minion
- master: 134.122.80.89
- id: slave1
- sudo systemctl restart salt-minion.service
Nyt on molemmissa koneissa asennettuna Salt sekä molemilla niiden oma rooli. ’whoami’ testasi, että slave1 on tuo toinen kone.
b) Tee saltille idempotenssi hei maailma (siis tiedostosta, foo.sls)
Ohje: http://terokarvinen.com/2018/salt-states-i-want-my-computers-like-this
Master:
- sudo mkdir -p /srv/salt/
- sudoedit /srv/salt/hello.sls
- /tmp/hellotero.txt:
- file.managed:
- – source: salt://hellotero.txt
- sudoedit /srv/salt/hellotero.txt
- See you at http://TeroKarvinen.com
- sudo salt ’*’ state.apply hello
Tulos:
| slave1: ———- ID: /tmp/hellotero.txt Function: file.managed Result: True Comment: File /tmp/hellotero.txt updated Started: 13:57:37.672878 Duration: 41.099 ms Changes: ———- diff: New file mode: 0644 Summary for slave1 ———— Succeeded: 1 (changed=1) Failed: 0 ———— Total states run: 1 Total run time: 41.099 ms |
Eli tiedoston ajaminen slave1-koneella onnistui.
Seuraava vaihe tekee niin, että kaikki orja-koneet tekevät tämän komennon: (minulla on nyt vain yksi, joten tästä ei nyt ole niin hyötyä)
- sudoedit /srv/salt/top.sls
- base:
- ’*’:
- – hello
Jos en halua odottaa puolta tuntia, jolloin tuo ylempi tulee toimintaan, sen voi laittaa toimimaan heti:
- sudo salt ’*’ state.highstate
Tuloksena onnistunut ajo:
| slave1: ———- ID: /tmp/hellotero.txt Function: file.managed Result: True Comment: File /tmp/hellotero.txt is in the correct state Started: 13:59:37.993508 Duration: 43.517 ms Changes: Summary for slave1 ———— Succeeded: 1 Failed: 0 ———— Total states run: 1 Total run time: 43.517 ms |
d) Kerää tietoa koneesta saltin avulla (grains.items)
(lähde: https://docs.saltstack.com/en/latest/topics/grains/)
”Salt comes with an interface to derive information about the underlying system. This is called the grains interface, because it presents salt with grains of information. Grains are collected for the operating system, domain name, IP address, kernel, OS type, memory, and many other system properties.”
Selvitä slave1:n IP-osoite:
- sudo salt slave1 cmd.run ’hostname -I’
- slave1:
- 139.59.154.219 10.19.0.6
- slave1:
Erilaisia teknisiä tietoja slave1:stä:
- sudo salt slave1 grains.items
| slave1: ———- pythonexecutable: /usr/bin/python3 pythonpath: – /usr/bin – /usr/lib/python36.zip – /usr/lib/python3.6SSDs: biosreleasedate: 12/12/2017 biosversion: 20171212 cpu_flags: [poistettu] cpu_model: Intel(R) Xeon(R) CPU E5-2650L v3 @ 1.80GHz cpuarch: x86_64 disks: [poistettu] dns: ———- domain: ip4_nameservers: – 127.0.0.53 ip6_nameservers: nameservers: – 127.0.0.53 options: – edns0 search: sortlist: domain: fqdn: [fully qualified domain name] harj4 fqdn_ip4: – 127.0.1.1 fqdn_ip6: gid: 0 gpus: |_ ———- model: QXL paravirtual graphic card vendor: unknown groupname: root host: harj4 hwaddr_interfaces: ———- eth0: f2:d9:90:b5:3c:7c lo: 00:00:00:00:00:00 id: slave1 init: systemd ip4_interfaces: ———- eth0: – 139.59.154.219 – 10.19.0.6 lo: – 127.0.0.1 ip6_interfaces: ———- eth0: – fe80::f0d9:90ff:feb5:3c7c lo: – ::1 ip_interfaces: ———- eth0: – 139.59.154.219 – 10.19.0.6 – fe80::f0d9:90ff:feb5:3c7c lo: – 127.0.0.1 – ::1 ipv4: – 10.19.0.6 – 127.0.0.1 – 139.59.154.219 ipv6: – ::1 – fe80::f0d9:90ff:feb5:3c7c kernel: Linux kernelrelease: 4.15.0-66-generic locale_info: ———- defaultencoding: UTF-8 defaultlanguage: en_US detectedencoding: UTF-8 localhost: harj4 lsb_distrib_codename: bionic lsb_distrib_description: Ubuntu 18.04.3 LTS lsb_distrib_id: Ubuntu lsb_distrib_release: 18.04 machine_id: bee8ddc9d0804e818dcfdb9c122ecf62 manufacturer: DigitalOcean master: 134.122.80.89 mdadm: mem_total: 985 nodename: harj4 num_cpus: 1 num_gpus: 1 os: Ubuntu os_family: Debian osarch: amd64 oscodename: bionic osfinger: Ubuntu-18.04 osfullname: Ubuntu osmajorrelease: 18 osrelease: 18.04 osrelease_info: – 18 – 4 path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin pid: 4648 productname: Droplet ps: ps -efHww – /usr/lib/python3.6/lib-dynload – /usr/local/lib/python3.6/dist-packages – /usr/lib/python3/dist-packages pythonversion: – 3 – 6 – 8 – final – 0 saltpath: /usr/lib/python3/dist-packages/salt saltversion: 2017.7.4 saltversioninfo: – 2017 – 7 – 4 – 0 serialnumber: 181796589 server_id: 317551988 shell: /bin/sh systemd: ———- features: +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid version: 237 uid: 0 username: root uuid: bee8ddc9-d080-4e81-8dcf-db9c122ecf62 virtual: kvm zmqversion: 4.2.5 |
Komennolla sai esiin paljon tietoa virtuaalikoneesta.
Selvitä, mikä hypervisor pyörii tällä noodilla:
- sudo salt slave1 grains.item virtual
- slave1:
- ———-
- virtual:
- kvm’
(KVM = Kernel-based Virtual Machine)
e) Kokeile jotain toista tilaa kuin file.managed. Tärkeitä ovat pkg.installed, file.managed, service.running, file.symlink, user.present, group.present. Ohjeita saa esim ’sudo salt kissa sys.state_doc pkg.installed|less’
Neuvoja sain:
- https://docs.saltstack.com/en/latest/ref/states/all/salt.states.group.html
- https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html
- https://docs.saltstack.com/en/getstarted/config/functions.html
Lisäsin tällaista siihen b:ssä tekemääni hello.sls tiedostoon:
Testaus:
- sudo salt ’*’ state.apply hello
Tulos:
| slave1: ———- ID: /tmp/hellotero.txt Function: file.managed Result: True Comment: File /tmp/hellotero.txt is in the correct state Started: 14:25:36.263197 Duration: 29.147 ms Changes: ———- ID: pinta Function: pkg.installed Result: True Comment: The following packages were installed/updated: pinta Started: 14:25:40.534437 Duration: 46137.546 ms Changes: ———- [poistettu] ———- ID: /tmp/moi.txt Function: file.managed Result: True Comment: File /tmp/moi.txt updated Started: 14:26:26.681403 Duration: 35.177 ms Changes: ———- diff: New file mode: 0644 ———- ID: ssh.service Function: service.running Result: True Comment: The service ssh.service is already running Started: 14:26:28.071702 Duration: 42.664 ms Changes: ———- ID: user account for pete Function: user.present Name: pete Result: True Comment: New user pete created Started: 14:26:28.117322 Duration: 72.968 ms Changes: ———- fullname: gid: 1002 groups: – pete – sudo home: /home/pete homephone: name: pete passwd: x roomnumber: shell: /bin/bash uid: 1002 workphone: ———- ID: group cheese Function: group.present Result: False Comment: Failed to create new group group cheese Started: 14:26:28.192096 Duration: 13.888 ms Changes: Summary for slave1 ———— Succeeded: 5 (changed=3) Failed: 1 ———— Total states run: 6 Total run time: 46.331 s ERROR: Minions returned with non-zero exit code |
Positin tuolta tuon pintan asennuksen.
Group failed, koska sitä ei näköjään ole olmassa? Lisäsin sen cheese ryhmän siihen adduser alle, missä luodaan se sudo ryhmä, johon käyttäjä liitetään.
Testaus:
| slave1: ———- ID: /tmp/hellotero.txt Function: file.managed Result: True Comment: File /tmp/hellotero.txt is in the correct state Started: 14:30:42.816061 Duration: 33.407 ms Changes: ———- ID: /tmp/moi.txt Function: file.managed Result: True Comment: File /tmp/moi.txt is in the correct state Started: 14:30:42.849707 Duration: 11.72 ms Changes: ———- ID: ssh.service Function: service.running Result: True Comment: The service ssh.service is already running Started: 14:30:44.097251 Duration: 53.756 ms Changes: ———- ID: user account for pete Function: user.present Name: pete Result: False Comment: The following group(s) are not present: cheese Started: 14:30:44.153433 Duration: 4.027 ms Changes: ———- ID: group cheese Function: group.present Result: False Comment: Failed to create new group group cheese Started: 14:30:44.158356 Duration: 11.857 ms Changes: Summary for slave1 ———— Succeeded: 3 Failed: 2 ———— Total states run: 5 Total run time: 114.767 ms ERROR: Minions returned with non-zero exit code |
Edelleen failed. Näköjään ryhmien pitää olla jo valmiina. Niitä ei voi luoda tällä tavalla.
Poistin hello.sls-tiedostosta tuon cheese ryhmän.
Testaus:
- sudo salt ’*’ state.apply hello
| slave1: ———- ID: /tmp/hellotero.txt Function: file.managed Result: True Comment: File /tmp/hellotero.txt is in the correct state Started: 14:31:57.088298 Duration: 26.571 ms Changes: ———- ID: /tmp/moi.txt Function: file.managed Result: True Comment: File /tmp/moi.txt is in the correct state Started: 14:31:57.115059 Duration: 11.86 ms Changes: ———- ID: ssh.service Function: service.running Result: True Comment: The service ssh.service is already running Started: 14:31:58.295491 Duration: 48.768 ms Changes: ———- ID: user account for pete Function: user.present Name: pete Result: True Comment: User pete is present and up to date Started: 14:31:58.346374 Duration: 4.293 ms Changes: Summary for slave1 ———— Succeeded: 4 Failed: 0 ———— Total states run: 4 Total run time: 91.492 ms |
Nyt tuli pelkkää vihreätä!
Kävin vielä tarkistamassa, mitä slave1 sanoo:
- less /etc/passwd
- pete:x:1002:1002::/home/pete:/bin/bash
- cd /tmp
- ls
- (siellä ne kaksi teksti tiedostoa ovat:) hellotero.txt ja moi.txt
f) Vapaaehtoinen: Laita Salt Master palvelimelle, joka näkyy Internetiin. Silloin orjat saavat siihen yhteyden tulimuurin läpi, ei-julkisista (NAT) osoitteista ja vaikkapa virtuaalikoneista.
Tein jo!