systemd攻略
相關(guān)文檔
紅帽7官方文檔 SYSTEM ADMINISTRATOR'S GUIDE
Systemd 入門教程:實(shí)戰(zhàn)篇 -阮一峰
帶著疑惑看文檔
第一關(guān):基礎(chǔ)問題
- systemd背景基礎(chǔ)命令有哪些?
- jounalctl日志管理命令知多少?
- 如何開機(jī)啟動(dòng)httpd.service?
- systemctl list-units 和 systemctl list-unit-files 輸出有什么區(qū)別?
- systemctl --failed 輸出的是什么?
- static invalid 是什么意思?
#systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
crond.service enabled
dbus-org.freedesktop.network1.service invalid
cpupower.service disabled
第二關(guān):中級(jí)問題
- systemd中的unit是什么概念?
- unit文件類型有哪些?
- 如何編寫一個(gè)unit文件?
- unit文件中哪些參數(shù)是常見的,重要的?
第三關(guān):進(jìn)階問題
- DBUS是什么?
- Target是什么?Target 與 傳統(tǒng) RunLevel 的對(duì)應(yīng)關(guān)系?
#systemctl list-units
slices.target
dbus.socket
test.slice
sshd.service
sys-devices-platform-serial8250-tty-ttyS3.device
brandbot.path
logstore.mount
systemd-journald.socket
systemd-tmpfiles-clean.timer
proc-sys-fs-binfmt_misc.automount
- 6u的cgroup和7u的cgroup有哪些區(qū)別?
- 7u的cgroup中的slice,scope,service的概念區(qū)別是什么?
- 如何在6u上管理cgroup?libcgroup
- 如何在7u上管理cgroup?systemd-run
- 在7u上從臨時(shí)創(chuàng)建cgroup到永久創(chuàng)建cgroup需要做什么?
- cgroup中cpu管理中的CPUShares,cpu.cfs_period_us 和 cpu.cfs_quota_us 你需要清楚
- cgroup中mem管理中的memory.max_usage_in_bytes 參數(shù)你需要清楚
- cgroup中cpuacct管理中cpuacct.stat統(tǒng)計(jì)使用量,你需要知道
- cpu,mem,等等的管理,你需要對(duì)
/sys/fs/cgroup/memory/有大致的了解
[root@jiangyi02.sqa.zmf /usr/lib/systemd/system]
#cat /proc/1049/cgroup
10:memory:/
9:cpuset:/
8:hugetlb:/
7:perf_event:/
6:cpuacct,cpu:/
5:blkio:/
4:freezer:/
3:devices:/
2:net_cls:/
1:name=systemd:/system.slice/syslog-ng.service
- 一些命令
#systemd-cgls
#mount|awk '$5=="cgroup" {print $0}'
如何讀man手冊
在linux上,其實(shí)很多知識(shí)在man手冊里的都可以獲得的,但是,對(duì)于新鮮的systemd,也許作為小白,可能一開始還真不太知道該man什么?強(qiáng)大的man手冊本身背后的邏輯和關(guān)聯(lián)性很好的反映了systemd背后知識(shí)體系的關(guān)系
首先,應(yīng)該先#man 1 systemd 這里入手 ,你將對(duì)systemd有一個(gè)全面的了解,并且知道systemd有12種類型的unit:
-
看完#man 1 systemd,你肯定會(huì)對(duì)12種類型的unit有很多的疑問,究竟這些unit的細(xì)節(jié);
man systemd.service man systemd.socket man systemd.target man systemd.device man systemd.mount man systemd.automount man systemd.snapshot man systemd.timer man systemd.swap man systemd.path man systemd.slice man systemd.scope
最為特殊的是systemd.special,從這里你將獲知很多的系統(tǒng)特殊的東西:
man systemd.special
basic.target, bluetooth.target, ctrl-alt-del.target, cryptsetup.target, cryptsetup-pre.target, dbus.service, dbus.socket, default.target,
display-manager.service, emergency.target, exit.target, final.target, getty.target, graphical.target, halt.target, hibernate.target,
hybrid-sleep.target, initrd-fs.target, kbrequest.target, kexec.target, local-fs.target, local-fs-pre.target, multi-user.target, network.target,
network-online.target, network-pre.target, nss-lookup.target, nss-user-lookup.target, paths.target, poweroff.target, printer.target, reboot.target,
remote-fs.target, remote-fs-pre.target, rescue.target, initrd-root-fs.target, rpcbind.target, runlevel2.target, runlevel3.target, runlevel4.target,
runlevel5.target, shutdown.target, sigpwr.target, sleep.target, smartcard.target, sockets.target, sound.target, suspend.target, swap.target,
sysinit.target, syslog.socket, system-update.target, time-sync.target, timers.target, umount.target, -.slice, system.slice, user.slice,
machine.slice
- 然后,你將開始對(duì)每一類型的unit文件的語法和參數(shù)使用有疑問,此時(shí),你應(yīng)該#man 5 systemd.unit
systemd全面了解:about unit types
#man 1 systemd
systemd provides a dependency system between various entities called "units" of 12 different types.
majority of units are configured in unit configuration files, whose syntax and basic set of options is described in systemd.unit(5)
The following unit types are available:
1. Service units, which start and control daemons and the processes they consist of. For details see systemd.service(5).
2. Socket units, which encapsulate local IPC or network sockets in the system, useful for socket-based activation. For details about socket units
see systemd.socket(5), for details on socket-based activation and other forms of activation, see daemon(7).
3. Target units are useful to group units, or provide well-known synchronization points during boot-up, see systemd.target(5).
4. Device units expose kernel devices in systemd and may be used to implement device-based activation. For details see systemd.device(5).
5. Mount units control mount points in the file system, for details see systemd.mount(5).
6. Automount units provide automount capabilities, for on-demand mounting of file systems as well as parallelized boot-up. See
systemd.automount(5).
7. Snapshot units can be used to temporarily save the state of the set of systemd units, which later may be restored by activating the saved
snapshot unit. For more information see systemd.snapshot(5).
8. Timer units are useful for triggering activation of other units based on timers. You may find details in systemd.timer(5).
9. Swap units are very similar to mount units and encapsulate memory swap partitions or files of the operating system. They are described in
systemd.swap(5).
10. Path units may be used to activate other services when file system objects change or are modified. See systemd.path(5).
11. Slice units may be used to group units which manage system processes (such as service and scope units) in a hierarchical tree for resource
management purposes. See systemd.slice(5).
12. Scope units are similar to service units, but manage foreign processes instead of starting them as well. See systemd.scope(5).
帶你劃重點(diǎn)
如何自己寫一個(gè)unit?
我們經(jīng)常寫的肯定是service unit,首先,
- [Unit]是每個(gè)unit文件都需要有的
- [Service] 區(qū)塊:定義如何啟動(dòng)當(dāng)前服務(wù),
- [Install區(qū)塊],定義如何安裝這個(gè)配置文件,即怎樣做到開機(jī)啟動(dòng)。
#systemctl cat httpd.service
# /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server //
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=notify
CPUShares=1500
MemoryLimit=1G
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
依賴關(guān)系和前后順序
依賴關(guān)系:Requires和Wants
前后順序:After,Before
依賴關(guān)系,前后順序是比較容易混淆的,當(dāng)使用Requires和Wants的時(shí)候,
- 如果不搭配After,Before使用,比如:
unit A Requires unit B, 那么A和B會(huì)一起啟動(dòng),如果B失敗了,A也就失敗了,只有B成功了,A才能成功;
unit A Wants unit B, 那么A和B會(huì)一起啟動(dòng),如果B失敗了,A也就失敗了,只有B成功了,A才能成功;
- 建議搭配After,Before使用,比如:
unit A Requires unit B, 那么同時(shí)最好unit A中寫上:After=B
unit處理依賴關(guān)系:Requires和Wants的區(qū)別?
使用systemd時(shí),可通過正確編寫單元配置文件來解決其依賴關(guān)系。典型的情況是,單元A要求單元B在A啟動(dòng)之前運(yùn)行。在此情況下,向單元A配置文件中的 [Unit] 段添加 Requires=B 和 After=B 即可。若此依賴關(guān)系是可選的,可添加 Wants=B 和 After=B。請(qǐng)注意 Wants= 和Requires= 并不意味著 After=,即如果 After= 選項(xiàng)沒有制定,這兩個(gè)單元將被并行啟動(dòng)。
依賴關(guān)系通常被用在服務(wù)(service)而不是目標(biāo)(target)上。例如, network.target 一般會(huì)被某個(gè)配置網(wǎng)絡(luò)接口的服務(wù)引入,所以,將自定義的單元排在該服務(wù)之后即可,因?yàn)?network.target 已經(jīng)啟動(dòng)。
unit文件中,服務(wù)類型: Type= 參數(shù)
編寫自定義的 service 文件時(shí),可以選擇幾種不同的服務(wù)啟動(dòng)方式。啟動(dòng)方式可通過配置文件 [Service] 段中的 Type= 參數(shù)進(jìn)行設(shè)置。
- Type=simple(默認(rèn)值):systemd認(rèn)為該服務(wù)將立即啟動(dòng)。服務(wù)進(jìn)程不會(huì)fork。如果該服務(wù)要啟動(dòng)其他服務(wù),不要使用此類型啟動(dòng),除非該服務(wù)是socket激活型。
- Type=forking:systemd認(rèn)為當(dāng)該服務(wù)進(jìn)程fork,且父進(jìn)程退出后服務(wù)啟動(dòng)成功。對(duì)于常規(guī)的守護(hù)進(jìn)程(daemon),除非你確定此啟動(dòng)方式無法滿足需求,使用此類型啟動(dòng)即可。使用此啟動(dòng)類型應(yīng)同時(shí)指定 PIDFile=,以便systemd能夠跟蹤服務(wù)的主進(jìn)程。
- Type=oneshot:這一選項(xiàng)適用于只執(zhí)行一項(xiàng)任務(wù)、隨后立即退出的服務(wù)。可能需要同時(shí)設(shè)置 RemainAfterExit=yes 使得 systemd 在服務(wù)進(jìn)程退出之后仍然認(rèn)為服務(wù)處于激活狀態(tài)。
- Type=notify:與 Type=simple 相同,但約定服務(wù)會(huì)在就緒后向 systemd 發(fā)送一個(gè)信號(hào)。這一通知的實(shí)現(xiàn)由 libsystemd-daemon.so 提供。
- Type=dbus:若以此方式啟動(dòng),當(dāng)指定的 BusName 出現(xiàn)在DBus系統(tǒng)總線上時(shí),systemd認(rèn)為服務(wù)就緒。
- Type=idle: systemd會(huì)等待所有任務(wù)處理完成后,才開始執(zhí)行idle類型的單元。其他行為和Type=simple 類似。
type的更多解釋可以參考 systemd.service(5)。
什么是DBus?
參考鏈接
IntroductionToDBus at IntroductionToDBus
Install
D-Bus is enabled automatically when using systemd because dbus is a dependency of systemd.
DBus是一種IPC機(jī)制
DBus是一種IPC機(jī)制,由freedesktop.org項(xiàng)目提供,使用GPL許可證發(fā)行,用于進(jìn)程間通信或進(jìn)程與內(nèi)核的通信。
注:Linux中的IPC通信機(jī)制還包括,管道(fifo),共享內(nèi)存,信號(hào)量,消息隊(duì)列,Socket等。
DBus進(jìn)程間通信主要有三層架構(gòu):
- 底層接口層:主要是通過libdbus這個(gè)函數(shù)庫,給予系統(tǒng)使用DBus的能力。
- 總線層:主要Message bus daemon這個(gè)總線守護(hù)進(jìn)程提供的,在Linux系統(tǒng)啟動(dòng)時(shí)運(yùn)行,負(fù)責(zé)進(jìn)程間的消息路由和傳遞,其中包括Linux內(nèi)核和Linux桌面環(huán)境的消息傳遞??偩€守護(hù)進(jìn)程可同時(shí)與多個(gè)應(yīng)用程序相連,并能把來自一個(gè)應(yīng)用程序的消息路由到0或者多個(gè)其他程序。
- 應(yīng)用封裝層:通過一系列基于特定應(yīng)用程序框架將DBus的底層接口封裝成友好的Wrapper庫,供不同開發(fā)人員使用(DBus官方主頁http://www.freedesktop.org/wiki/Software/dbus,提供了大部分編程語言的DBus庫版本)。比如libdbus-glib, libdbus-python.
Target和傳統(tǒng)的啟動(dòng)級(jí)別之間的關(guān)系
啟動(dòng)級(jí)別(runlevel)是一個(gè)舊的概念?,F(xiàn)在,systemd 引入了一個(gè)和啟動(dòng)級(jí)別功能相似又不同的概念——目標(biāo)(target)。不像數(shù)字表示的啟動(dòng)級(jí)別,每個(gè)目標(biāo)都有名字和獨(dú)特的功能,并且能同時(shí)啟用多個(gè)。一些目標(biāo)繼承其他目標(biāo)的服務(wù),并啟動(dòng)新服務(wù)。systemd 提供了一些模仿 sysvinit 啟動(dòng)級(jí)別的目標(biāo),仍可以使用舊的 telinit 啟動(dòng)級(jí)別 命令切換。
獲取當(dāng)前目標(biāo)
不要使用 runlevel 命令了:
$ systemctl list-units --type=target
修改默認(rèn)啟動(dòng)級(jí)別/目標(biāo)
開機(jī)啟動(dòng)進(jìn)的目標(biāo)是 default.target,默認(rèn)鏈接到 graphical.target (大致相當(dāng)于原來的啟動(dòng)級(jí)別5)??梢酝ㄟ^內(nèi)核參數(shù)更改默認(rèn)啟動(dòng)級(jí)別:
- systemd.unit=multi-user.target (大致相當(dāng)于級(jí)別3)
- systemd.unit=rescue.target (大致相當(dāng)于級(jí)別1)
另一個(gè)方法是修改 default.target??梢酝ㄟ^ systemctl 修改它:
# systemctl set-default multi-user.target
要覆蓋已經(jīng)設(shè)置的default.target,請(qǐng)使用 force:
# systemctl set-default -f multi-user.target
可以在 systemctl 的輸出中看到命令執(zhí)行的效果:鏈接 /etc/systemd/system/default.target 被創(chuàng)建,指向新的默認(rèn)啟動(dòng)級(jí)別。
定時(shí)器timer是什么鬼?
定時(shí)器是以 .timer 為后綴的配置文件,記錄由system的里面由時(shí)間觸發(fā)的動(dòng)作, 定時(shí)器可以替代 cron 的大部分功能。詳情參閱 systemd/Timers (簡體中文).
服務(wù)單元
每個(gè) .timer 文件所在目錄都得有一個(gè)對(duì)應(yīng)的 .service 文件(如 foo.timer 和 foo.service)。.timer 用于激活并控制 .service 文件。 .service 文件中不需要包含 [Install] 部分,因?yàn)檫@由 timer 單元接管。必要時(shí)通過在定時(shí)器的 [Timer] 部分指定 Unit= 選項(xiàng)來控制一個(gè)與定時(shí)器不同名的服務(wù)單元。
管理
使用 timer 單元時(shí)像其他單元一樣 enable 或 start 即可(別忘了添加 .timer 后綴)。要查看所有已啟用的定時(shí)器,運(yùn)行:
$ systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Thu 2014-07-10 19:37:03 CEST 11h left Wed 2014-07-09 19:37:03 CEST 12h ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Fri 2014-07-11 00:00:00 CEST 15h left Thu 2014-07-10 00:00:13 CEST 8h ago logrotate.timer logrotate.service
systemd-journald和syslog之間的秘密
[root@baseos_server01.cloud.dg /home/ahao.mah]
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 4.4G 43G 10% /
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 364K 32G 1% /dev/shm
tmpfs 32G 57M 32G 1% /run 日志存在這里就是存在內(nèi)存里,明顯不合理,有的docker機(jī)器這里日志量,占用內(nèi)存量很大,需要解決;
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 243M 71M 157M 31% /boot
tmpfs 6.3G 0 6.3G 0% /run/user/0
tmpfs 6.3G 0 6.3G 0% /run/user/122575
如何查看journal二進(jìn)制日志文件?
[root@baseos_server01.cloud.dg /home/ahao.mah]
#ll /run/log/journal/613fd1717b844226af5ea83f4849d6dd/system.journal
-rw-r-x---+ 1 root systemd-journal 58720256 Aug 15 10:51 /run/log/journal/613fd1717b844226af5ea83f4849d6dd/system.journal
使用查看xxx.journal的方法有兩種:
第一種:指定目標(biāo)文件
[root@baseos_server01.cloud.dg /home/ahao.mah]
#journalctl --file /run/log/journal/613fd1717b844226af5ea83f4849d6dd/system.journal
第二種:指定目標(biāo)目錄
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#journalctl -D /var/log/journal/
問題:systemd-journald請(qǐng)不用占用我的內(nèi)存
這里有個(gè)問題,是很多docker的機(jī)器,內(nèi)存本來就不多,systemd-journald 產(chǎn)生的日志導(dǎo)入/run/log/journal/里,占用了大量的內(nèi)存空間,不合理;
通過查看man手冊,可以知道#man 5 journald.conf 你會(huì)發(fā)現(xiàn),Storage=的值可以是"volatile", "persistent", "auto" and "none",但是,默認(rèn)的是auto,
- volatile代表日志只存在內(nèi)存中,即/run/log/journal/
- persistent代表日志只存在磁盤中,即/var/log/journal/
- auto代表日志存在磁盤中,或者內(nèi)存中,這個(gè)取決于你是否創(chuàng)建/var/log/journal/目錄!!這個(gè)也算是一個(gè)坑吧,看來大家都需要手動(dòng)
mkdir -p /var/log/journal/;systemctl restart systemd-journald來解放自己的內(nèi)存了?。。?/li> - none,表示,日志不保留,全部drop,只有當(dāng)你決定不使用systemd-journald的時(shí)候,你可以使用!
7u的systemd-journald默認(rèn)幫你存多少日志量?
- 默認(rèn)日志最大限制為所在文件系統(tǒng)容量的 10%,即:如果 /var/log/journal 儲(chǔ)存在 50GiB 的根分區(qū)中,那么日志最多存儲(chǔ) 5GiB 數(shù)據(jù)??梢孕薷呐渲梦募付ㄗ畲笙拗?。如限制日志最大 50MiB:
/etc/systemd/journald.conf
SystemMaxUse=50M
7u上如何手動(dòng)清理日志?
/var/log/journal 存放著日志, rm 應(yīng)該能工作. 或者使用journalctl,
例如:
清理日志使總大小小于 100M:
# journalctl --vacuum-size=100M清理最早兩周前的日志:
# journalctl --vacuum-time=2weeks
有了systemd-journald,我們是否還需要syslog-ng,rsyslog?
在man手冊里#man journald.conf,有一段關(guān)于systemd-journald和第三方syslog的關(guān)系的描述,如果,我們想步子邁大一些,不使用第三方syslog 當(dāng)然是可以的,如果我們想小步走,可以繼續(xù)使用第三方syslog。man手冊介紹了兩種方式讀日志;
- /run/systemd/journal/syslog + ForwardToSyslog= yes 的方式,傳給syslog-ng,此時(shí),syslog-ng的source必須是
source s_sys {
# Source additional configuration files (.conf extension only)
system();
internal();
};
至于問什么?請(qǐng)參考:wiki.archlinux.org中介紹的Syslog-ng
- in a second method, a syslog daemon behaves like a normal journal client, and reads messages from the journal files, similarly to journalctl(1). In this
method, messages do not have to be read immediately, which allows a logging daemon which is only started late in boot to access all messages since
the start of the system. In addition, full structured meta-data is available to it. This method of course is available only if the messages are
stored in a journal file at all. So it will not work if Storage=none is set. It should be noted that usually the second method is used by syslog
daemons, so the Storage= option, and not the ForwardToSyslog= option, is relevant for them.
7u上的cgroup淺談
其實(shí),網(wǎng)上對(duì)6u的介紹比較多,但是7u的較少一些,最重要的是理解cgroup的內(nèi)部邏輯和機(jī)制,7u上最好的文檔就是man手冊,熟悉man手冊,對(duì)systemd的深入了解至關(guān)重要;基本的概念其實(shí)沒有必要在本文中再重復(fù)重復(fù)重復(fù),而是在腦海里對(duì)其整個(gè)體系結(jié)構(gòu)有一個(gè)框架,帶著疑問去看man手冊;
帶著疑問看man手冊
- 6u上的libconfig(/etc/cgconfig.conf),7u上還有嗎?
參考鏈接
對(duì)6u的cgroup講解的很好Blog(6u): how to use cgroup
與 cgroup 相關(guān)的 systemd man文檔
以下的 manual page 包含 systemd 中統(tǒng)一的 cgroup 層級(jí)基本信息:
- systemd.resource-control(5) —— 描述系統(tǒng)單位共享的資源控制配置選項(xiàng)。
- systemd.unit(5) —— 描述所有單位配置文件的共同選項(xiàng)。
- systemd.slice(5) —— 提供 .slice 單位的基本信息。
- systemd.scope(5) —— 提供 .scope 單位的基本信息。
- systemd.service(5) —— 提供 .service 單位的基本信息。
與 cgroup 相關(guān)的 Systemd 工具幫助頁面
- systemd-run(1) —— 此 manual page 列出了 systemd-run 實(shí)用工具的全部命令列選項(xiàng)。
- systemctl(1) —— systemctl 實(shí)用工具的 manual page 列出了可用選項(xiàng)及指令。
- systemd-cgls(1) —— 此 manual page 列出了 systemd-cgls 實(shí)用工具的全部命令列選項(xiàng)。
- systemd-cgtop(1) —— 此 manual page 包含了 systemd-cgtop 實(shí)用工具的全部命令列選項(xiàng)。
- machinectl(1) —— 此 manual page 列出了 machinectl 實(shí)用工具的全部命令列選項(xiàng)。
- systemd.kill(5) —— 此 manual page 為系統(tǒng)單位提供了終止配置選項(xiàng)的概述。
初感受:user.slice用戶會(huì)話
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#systemd-cgls
..
..
├─user.slice
│ ├─user-122575.slice
│ │ └─session-86733.scope
│ │ ├─15404 sshd: ahao.mah [priv
│ │ ├─15406 sshd: ahao.mah@pts/0
│ │ ├─15407 -bash
│ │ ├─15443 sudo su -c bash
│ │ ├─15450 su -c bash
│ │ ├─15451 bash
│ │ ├─91380 systemd-cgls
│ │ └─91381 less
│ └─user-0.slice
│ └─session-1.scope
│ ├─ 1554 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1556 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1557 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1558 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1560 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1561 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─119532 /home/staragent/bin/staragentd
│ └─119533 staragent-core
..
..
當(dāng)我新打開一個(gè)session,如下,會(huì)產(chǎn)生一樹枝:session-86942.scope
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#systemd-cgls
..
..
├─user.slice
│ ├─user-122575.slice
│ │ ├─session-86942.scope
│ │ │ ├─91454 sshd: ahao.mah [priv
│ │ │ ├─91476 sshd: ahao.mah@pts/1
│ │ │ └─91477 -bash
│ │ └─session-86733.scope
│ │ ├─15404 sshd: ahao.mah [priv
│ │ ├─15406 sshd: ahao.mah@pts/0
│ │ ├─15407 -bash
│ │ ├─15443 sudo su -c bash
│ │ ├─15450 su -c bash
│ │ ├─15451 bash
│ │ ├─91513 systemd-cgls
│ │ └─91514 less
│ └─user-0.slice
│ └─session-1.scope
│ ├─ 1554 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1556 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1557 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1558 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1560 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─ 1561 /usr/alisys/dragoon/bin/DragoonAgent
│ ├─119532 /home/staragent/bin/staragentd
│ └─119533 staragent-core
..
..
看了上面,你應(yīng)該對(duì)user-0.slice產(chǎn)生疑惑才對(duì),為什么在user.slice下,會(huì)存在user-0.slice這個(gè)slice呢??也許猜猜可能知道,這個(gè)是集團(tuán)DragoonAgent 產(chǎn)生的。但是它是怎么產(chǎn)生的呢?留下這個(gè)疑問。
How to use cgroup at 7u
- transient cgroup(臨時(shí) cgroup):請(qǐng)使用 systemd-run 指令啟動(dòng)此服務(wù),如此,可以限制此服務(wù)在運(yùn)行時(shí)所用資源。對(duì) systemd 進(jìn)行 API 調(diào)用,應(yīng)用程序可以動(dòng)態(tài)創(chuàng)建臨時(shí) cgroup。
- persistent cgroup(永久 cgroup),請(qǐng)對(duì)其單位配置文件進(jìn)行編寫。系統(tǒng)重啟后,此項(xiàng)配置會(huì)被保留,所以它可以用于管理自動(dòng)啟動(dòng)的服務(wù)。請(qǐng)注意,scope 單位不能以此方式創(chuàng)建。
7u上創(chuàng)建臨時(shí)cgroup
查看man手冊,獲得systemd-run用法:systemd-run(1) manual page
如下:
用法:
systemd-run --unit=name --scope --slice=slice_name command
解釋:
--unit=toptest 代表您想要此單位被識(shí)別的名稱。如果 --unit 沒有被指定,單位名稱會(huì)自動(dòng)生成。建議選擇一個(gè)描述性的名字,因?yàn)樗鼘⒋?systemctl 輸出中的單位。在單位運(yùn)行時(shí)期間,此名字需為獨(dú)一無二的。
使用可選的 --scope 參數(shù)創(chuàng)建臨時(shí) scope 單位來替代默認(rèn)創(chuàng)建的 service 單位。
--slice 選項(xiàng),讓您新近創(chuàng)建的 service 或 scope 單位可以成為指定 slice 的一部分。用現(xiàn)存 slice(如 systemctl -t slice 輸出所示)的名字替代 slice_name,或者通過傳送一個(gè)獨(dú)有名字來創(chuàng)建新 slice。默認(rèn)情況下,service 和 scope 做為 system.slice 的一部分被創(chuàng)建。
用您希望在 service 單位中運(yùn)行的指令替代 command。將此指令放置于 systemd-run 句法的最末端。這樣,此指令的參數(shù)就不會(huì)與 systemd-run 參數(shù)混淆。
除上述選項(xiàng)外,systemd-run 也有一些其它可用參數(shù)。例如,--description 可以創(chuàng)建對(duì)單位的描述;service 進(jìn)程結(jié)束后,--remain-after-exit 可以收集運(yùn)行時(shí)信息;--machine 選項(xiàng)可以在密閉容器中執(zhí)行指令。更多信息,請(qǐng)參閱 systemd-run(1) manual page。
栗子:
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#systemd-run --unit=toptest --slice=test top -b
Running as unit toptest.service.
現(xiàn)在,toptest.service 名稱可以與 systemctl 指令結(jié)合,以監(jiān)控或修改 cgroup。
slice輸出
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#systemctl -t slice
UNIT LOAD ACTIVE SUB DESCRIPTION
-.slice loaded active active Root Slice
machine.slice loaded active active Virtual Machine and Container Slice
system-getty.slice loaded active active system-getty.slice
system-systemd\x2dfsck.slice loaded active active system-systemd\x2dfsck.slice
system.slice loaded active active System Slice
test.slice loaded active active test.slice
user-0.slice loaded active active user-0.slice
user-122575.slice loaded active active user-122575.slice
user.slice loaded active active User and Session Slice
service輸出
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#systemctl -t service | grep test
toptest.service loaded active running /usr/bin/top -b
配合systemctl
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#systemctl status toptest.service
● toptest.service - /usr/bin/top -b
Loaded: loaded (/run/systemd/system/toptest.service; static; vendor preset: disabled)
Drop-In: /run/systemd/system/toptest.service.d
└─50-Description.conf, 50-ExecStart.conf, 50-Slice.conf
Active: active (running) since Mon 2016-08-15 13:17:30 CST; 5min ago
Main PID: 94889 (top)
CGroup: /test.slice/toptest.service
└─94889 /usr/bin/top -b
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 120293 root 20 0 0 0 0 S 0.0 0.0 0:00.17 kworker/1+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 120935 root 20 0 0 0 0 S 0.0 0.0 0:22.16 kworker/6+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 122864 root 20 0 0 0 0 S 0.0 0.0 0:04.90 kworker/9+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 124341 root 0 -20 0 0 0 S 0.0 0.0 8:56.26 kworker/9+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 125657 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/1+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 126324 root 20 0 0 0 0 S 0.0 0.0 0:02.37 kworker/1+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 127006 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/1+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 128144 root 0 -20 0 0 0 S 0.0 0.0 8:55.18 kworker/1+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 129070 root 0 -20 0 0 0 S 0.0 0.0 0:00.30 kworker/2+
Aug 15 13:23:08 jiangyi01.sqa.zmf top[94889]: 130319 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/1+
7u上創(chuàng)建永久 cgroup
上面,我們已經(jīng)創(chuàng)建了一個(gè)臨時(shí)cgroup,如果想讓它永久,下次開機(jī)還存在,我們只需要使用: systemctl enable 指令,動(dòng)運(yùn)行此指令會(huì)在 /usr/lib/systemd/system/ 目錄中創(chuàng)建單位文件。如要對(duì) cgroup 做出永久改變,請(qǐng)?zhí)砑踊蛐薷钠鋯挝晃募械呐渲脜?shù)
7u上刪除cgroup
兩種方式:
終止一個(gè)unit:
systemctl stop name.service終止多個(gè)unit,pid用逗號(hào)分隔:
systemctl kill name.service --kill-who=PID,... --signal=signal
永久刪除一個(gè)cgroup:systemctl disable name.service
7u上修改crgroup:
兩種方式
- 命令行:臨時(shí)修改cgroup:systemctl set-property
~]# systemctl set-property httpd.service CPUShares=600 MemoryLimit=500M
- 修改文件:所有被 systemd 監(jiān)管的永久單位都在 /usr/lib/systemd/system/ 目錄中有一個(gè)單位配置文件。如要修改 service 單位的參數(shù),請(qǐng)修改此配置文件。
6u上使用cgroup
蝦面這個(gè)系列文章簡單的實(shí)例寫的不錯(cuò)
cgroup中幾個(gè)問題解答
- 6u的cgroup和7u的cgroup有哪些區(qū)別?
- 7u的cgroup中的slice,scope,service的概念區(qū)別是什么?
- 如何在6u上管理cgroup?libcgroup
- 如何在7u上管理cgroup?systemd-run
- 在7u上從臨時(shí)創(chuàng)建cgroup到永久創(chuàng)建cgroup需要做什么?
- cgroup中cpu管理中的CPUShares,cpu.cfs_period_us 和 cpu.cfs_quota_us 你需要清楚
在 cpu 子系統(tǒng)中,cpu.stat 就是用前面那種方法做的資源限制的統(tǒng)計(jì)了。nr_periods、nr_throttled 就是總共經(jīng)過的周期,和其中受限制的周期。throttled_time 就是總共被控制組掐掉的 cpu 使用時(shí)間。
還有個(gè) cpu.shares, 它也是用來限制 cpu 使用的。但是與 cpu.cfs_quota_us、cpu.cfs_period_us 有挺大區(qū)別。cpu.shares 不是限制進(jìn)程能使用的絕對(duì)的 cpu 時(shí)間,而是控制各個(gè)組之間的配額。比如
/cpu/cpu.shares : 1024
/cpu/foo/cpu.shares : 2048
- cgroup中mem管理中的limit_in_bytes參數(shù)你需要清楚
# echo 10485760 >/sys/fs/cgroup/memory/foo/memory.limit_in_bytes
即可限制該組中的進(jìn)程使用的物理內(nèi)存總量不超過 10MB。對(duì) memory.memsw.limit_in_bytes 來說,則是限制虛擬內(nèi)存使用。memory.memsw.limit_in_bytes 必須大于或等于 memory.limit_in_byte。這些值還可以用更方便的 100M,20G 這樣的形式來設(shè)置。要解除限制,就把這個(gè)值設(shè)為 -1 即可。
- cgroup中cpuacct管理中cpuacct.stat統(tǒng)計(jì)使用量,你需要知道
cpuacct 子系統(tǒng)專門用來做 cpu 資源統(tǒng)計(jì)。cpuacct.stat 統(tǒng)計(jì)了該控制組中進(jìn)程用戶態(tài)和內(nèi)核態(tài)的 cpu 使用量,單位是 USER_HZ,也就是 jiffies、cpu 滴答數(shù)。每秒的滴答數(shù)可以用 getconf CLK_TCK 來獲取,通常是 100。將看到的值除以這個(gè)值就可以換算成秒。
cpuacct.usage 和 cpuacct.usage_percpu 是該控制組中進(jìn)程消耗的 cpu 時(shí)間,單位是納秒。后者是分 cpu 統(tǒng)計(jì)的。
- cpu,mem,等等的管理,你需要對(duì)
/sys/fs/cgroup/memory/有大致的了解