Share
Embed
Transcript
36 slides
-
$ whoami • @nasa9084 • LINE Corp. • Go /
Kubernetes Engineer -
What is “systemd”? 1. A “System and Service Manager” for
Linux • An init system (like sysv/init) • A service manager 2. A software suite for Linux • Including 1. -
What is “systemd”? 1. A “System and Service Manager” for
Linux • An init system • A service manager • Manages services as “unit” 2. A software suite for Linux • Including 1. • systemd(service manager), journald, systemd-tmpfiles, … -
Boot up process w/ systemd • Run /usr/lib/systemd/systemd as PID=1
• Construct a dependency tree from default.target (target is a group of units) • Run dependencies concurrently -
How to configure systemd? • systemd is configured via plain-text
files • Called “unit file” • Ini-style configuration file • There’re various type of units -
UZQF %FTDSJQUJPO TFSWJDF 4FSWJDFEBFNPONBOBHFNFOU TPDLFU 4PDLFUCBTFEBDUJWBUJPO MJLFYJOFUE UBSHFU (SPVQPGVOJUT EFWJDF
TZTGTVEFWEFWJDFT EFWTEBEFWTEBEFWJDF NPVOU 'JMFTZTUFNNPVOUQPJOU GTUBCXJMMCFDPOWFSUFE BVUPNPVOU 'JMFTZTUFNBVUPNPVOUQPJOU GTUBCXJMMCFDPOWFSUFE TXBQ 4XBQEFWJDFTPSNFNPSZQBHJOH GTUBCXJMMCFDPOWFSUFE UJNFS 5JNFS MJLFDSPOUBC QBUI 1BUINPOJUPSFECZTZTUFNE TDPQF (SPVQJOHXPSLFSQSPDFTTFTUPNBOBHFSFTPVSDFT TMJDF (SPVQJOH4FSWJDFTBOE4DPQFT Major unit types -
How to use systemd units? • Use systemctl to operate
systemd units • Analyze system status • Use systemd units • Manage power -
Systemctl commands TUBUVT TIPXTZTUFNTUBUVT MJTUVOJUT TIPXMJTUPGSVOOJOHVOJUT TUBUVT6/*5 TIPXVOJUTUBUVT TUBSU6/*5 TUBSUVOJUJNNFEJBUFMZ
TUPQ6/*5 TUPQVOJUJNNFEJBUFMZ SFTUBSU6/*5 SFTUBSUVOJU SFMPBE6/*5 SFMPBEVOJU FOBCMF6/*5 FOBCMFVOJUPOCPPU EJTBCMF6/*5 EJTBCMFVOJUPOCPPU EBFNPOSFMPBE SFMPBETZTUFNE SFCPPU SFCPPUTZTUFN QPXFSP⒎ TIVUEPXOBOEQPXFSP⒎TZTUFN -
Where is systemd unit files? • Basically placed in two
places: • /usr/lib/systemd/system/ • Units provided by installed packages • /etc/systemd/system/ (primary) • Units installed by the system admin -
The structure of unit files • Unit files consists of
three sections 1. [Unit] section • Generic information about the unit 2. [Install] section • Installation information for the unit 3. Type-specific section -
[Unit] Section %FTDSJQUJPO "IVNBOSFBEBCMFOBNFGPSUIFVOJU %PDVNFOUBUJPO 63*TSFGFSFODJOHEPDVNFOUBUJPO 8BOUT 3FRVJSFT %FQFOEFODZPOPUIFSVOJUT 6/*5@/".&XBOUTEJSIBTTBNFGVOD
$POqJDUT /FHBUJWF%FQFOEFODZPOPUIFSVOJUT #FGPSF "GUFS %FpOFPSEFSJOHCXVOJUT $POEJUJPO999 "TTFU999 $POEJUJPOTGPSUIFVOJU -
Systemd Service Unit • Daemon / Service definition controlled and
supervised by systemd • The extension is .service • Most of the cases, WantedBy may be multi- user.target -
[Service] Section Options 5ZQF TFSWJDFTUBSUVQUZQF &YFD4UBSU &YFD3FMPBE &YFD4UPQ $PNNBOETUPTUBSU SFMPBE
TUPQ 3FTUBSU XIFUIFSUIFTFSWJDFTIBMMCF SFTUBSUFEXIFO &OWJSPONFOU EFpOFFOWJSPONFOUWBSJBCMFT 8PSLJOH%JSFDUPSZ EFpOFUIFEJSFDUPSZUPXPSL -
Service Startup Type • Simple(default): expected the unit started immediately
after the main service process has been forked off • forking: expected the process will call fork() • oneshot: expected the unit up after the main process exits -
Restart &YJUDBVTFT OP BMXBZT PO TVDDFTT PO GBJMVSF PO BCOPSNBM
PO BCPSU $MFBOFYJU $MFBOTJHOBM 㾎 㾎 6ODMFBOFYJU 㾎 㾎 6ODMFBO TJHOBM 㾎 㾎 㾎 㾎 5JNFPVU 㾎 㾎 㾎 -
systemd timer • Timer-based service activation like crontab • The
extension is .timer • foo.timer calls foo.service by default • WantedBy=timers.target • systemctl list-timers to list up current active timers • Two type of timer -
Monotonic Timers • Activated after a time span relatively •
OnActivateSec: after activated the timer • OnBootSec: after booted up the machine • OnStartupSec: after systemd was started • OnUnitActiveSec: after last activated • OnUnitInactiveSec: after last deactivated -
Realtime Timers • a.k.a. wallclock timers • Activate on a
calendar event • The same way that cron jobs do • Use OnCalendar option -
Realtime Timer Configs • Configure using systemd time spec •
DayOfWeek Year-Month-Day Hour:Minute:Second Timezone • Some shorthand is predefined • … , monthly, weekly, yearly, quarterly… • Weekday is defined as English name • Wed or Wednesday • * is for wildcard, .. is for range, / is for repetition, ~ is for last -
UJNFTUBNQ NFBOJOH 5IV 'SJ PGUIFTU UIPGFWFSZ NPOUIPGUIFZFBSCVUJGJU JTB5IVSTEBZPS'SJEBZ
d UIFUIJSEMBTUEBZJO'FCSVBSZ CVJMEFSTDPOUPLZP XFFLMZ PGFWFSZ.POEBZ .PO d UIFMBTU.POEBZJO.BZ Calendar Event Examples -
PROS: Diff with Crontab • Jobs can be easily ran
independently of timers • So easy to debug • Jobs can be set up to depend on other units • Jobs can be set up with specific environment • The logs are reported into journal -
CONS: Diff with Crontab • Creation redundant • Need to
create two files: service and timer • Need to run two times systemctl • There’s no built-in equivalent to MAILTO • But can configure like MAILTO -
systemd-run • run the specified command in a transient •
to create transient service • to create transient timer # systemd-run --on-calendar=weekly curl -X POST https://... -
What is journald? • A log system provided by systemd
• systemd-journald.service • Save logs into /var/run/log/journal • Auto-collect logs from stdout/stderr of systemd services • Syslog messages from local processes • Message posted to journald API -
How to Read journald Logs? • Use journalctl to read
journald logs • Journals are binary-formatted • There're many, useful options • Match by fields • Colored -
PQUJPO EFTDSJQUJPO F JNNFEJBUFMZKVNQUPUIFFOE S TIPXUIFOFXFTUFOUSJFTpSTU G GPMMPXUIFKPVSOBM MJLFUBJMG V6/*5
TIPXMPHTGSPNUIFTQFDJpFEVOJU OPQBHFS EPOPUQJQFPVUJOUPBQBHF MFTTJTVTFECZEFGBVMU EJTLVTBHF TIPXEJTLVTBHFPGBMMKPVSOBMpMFT P DIBOHFKPVSOBMPVUQVUNPEF journalctl options -
NPEF EFTDSJQUJPO TIPSU EFGBVMU BMNPTUTBNFXJUITZTMPH WFSCPTF TIPXUIFGVMMTUSVDUVSFEFOUSZJUFNTXJUIBMMpFMET KTPO GPSNBUTFOUSJFTBT+40/ POFQFSMJOF
KTPOQSFUUZ TBNFXJUI+40/CVUIVNBOSFBEBCMFGPSN KTPOTTF GPSNBUTFOUSJFTBT4FSWFS4FOU&WFOUT DBU TIPXWFSZTJNQMFQSJOU Journal output mode -
Configure journald • /etc/systemd/journald.conf • Max / Keep Disk Usage
• Log Rate Limit • Forwarding to rsyslog