本主題將從3個角度進行對比
本文爲第二部分:服務管理的對比
1. sysvinit、upstart、systemd簡介
/ | CentOS 5 | CentOS 6 | CentOS 7 | 備註 |
---|---|---|---|---|
sysvinit | ✅ | ✅ | ✅ | 第一代,傳統,兼容最多(/etc/init.d/、/etc/rc.X) |
upstart | ❌ | ✅ | ❌ | 第二代,形似systemd雛形(/etc/init) |
systemd | ❌ | ❌ | ✅ | 第三代,配合cgroup,systemd完全接管整個系統(/usr/lib/systemd) |
2. sysvinit、upstart、systemd常用命令
動作 | sysvinit | upstart | systemd |
---|---|---|---|
查看 | service mytest status | initctl status mytest | systemctl status mytest.service |
啓動 | service mytest start | initctl start mytest | systemctl start mytest.service |
關閉 | service mytest stop | initctl stop mytest | systemctl stop mytest.service |
強殺進程 | kill -9 PID | kill -9 PID | systemctl kill mytest.service --signal=9 |
重啓 | service mytest restart | initctl restart mytest | systemctl restart mytest.service |
重載 | service mytest reload | initctl reload mytest | systemctl reload mytest.service |
開機啓動 | chkconfig mytest on | /etc/init/mytest.conf裏配置start on runlevel [3] | systemctl enable mytest.service |
3. runlevel運行級別
運行級別 | CentOS 6 | CentOS 7 |
---|---|---|
0 | halt | runlevel0.target -> poweroff.target |
1 | Single user mode | runlevel1.target -> rescue.target |
2 | Multiuser, without NFS | runlevel2.target -> multi-user.target |
3 | Full multiuser mode | runlevel3.target -> multi-user.target |
4 | unused | runlevel4.target -> multi-user.target |
5 | X11 | runlevel5.target -> graphical.target |
6 | reboot | runlevel6.target -> reboot.target |
查看 | cat /etc/inittab | systemctl get-default |
開機生效 | 編輯/etc/inittab | systemctl set-default multi-user.target |
立即切換 | init 5 | systemctl isolate graphical.target |
4. 日誌查詢
CentOS 6: 手工在/var/log/messages、/var/log/dmesg、/var/log/secure中grep,麻煩且效率低
CentOS 7: 統一使用journalctl,可以使用多個因素匹配,比如時間段、服務名、日誌級別等等。另外,systemd日誌默認經過壓縮,是二進制文件,無法直接查看
journalctl常用命令 | 作用 | CentOS 6比 |
---|---|---|
journalctl | 所有日誌,包含系統、內核等等 | 手動在對應日誌文件中grep |
journalctl --dmesg | 查看當前開機後的內核日誌 | dmesg |
journalctl --boot | 查看當前開機後的日誌 | 先查當前開機啓動時間,然後cat /var/log/... |
journalctl --boot=-1 | 查看上一次啓動的日誌 | 查詢上次開機到當前開機之間時間,然後cat /var/log/... |
journalctl --since="2018-08-01 12:00:00" | 查看從指定時間開始到當前的日誌 | 手動在日誌裏grep |
journalctl --since=yesterday --until=today | 查看昨天0-24點的日誌 | 手動在日誌裏grep |
journalctl -n 20 | 查看最後10行 | tail -n 20 |
journalctl -f | 實時滾動顯示最新日誌 | tail -f |
journalctl -e | 直接翻到最後 | tail |
journalctl -u mytest.service | 查看指定服務日誌 | 先查詢日誌保存路徑,然後再cat查看 |
journalctl -p 0 | 查看指定日誌級別的日誌,日誌級別從0到7 | 通過syslog將不同級別的日誌放到不同文件中 |
journalctl -u mytest.service -o json-pretty或-o verbose | 查看每條日誌詳細信息(包含元信息) | 無 |
journalctl --disk-usage | 查看日誌所在的磁盤空間 | du -shx /var/log/messages等 |
5. 實現守護進程
CentOS 6
- sysvinit需要自行實現
- nohup &
- screen
- supervisor
- upstart和systemd類似,將程序運行在前臺即可
CentOS 7
- 由systemd啓動,將程序運行在前臺即可
6. sysvinit、upstart、systemd例子
sysvinit
cat > /etc/init.d/mytest <<EOF
. /etc/rc.d/init.d/functions
start() { … }
stop() { … }
restart() { … }
reload() { … }
status() { … }
case "$1" in
start)
start
;;
stop)
stop
;;
…
esac
exit $RETVAL
EOF
chmod +x /etc/init.d/mytest
service mytest start
upstart
cat > /etc/init/mytest.conf <<EOF
start on runlevel [3]
description “mytest"
exec /root/mytest.sh
EOF
initctl start mytest
systemd
cat > /usr/lib/systemd/system/mytest.service <<EOF
[Unit]
Description=mytest
[Service]
Type=simple
ExecStart=/root/mytest.sh
[Install]
WantedBy=multi-user.target
EOF
systemctl start mytest
7. PID管理
- sysvinit: 需要生成PID文件,用於後期關閉、重啓等使用
- upstart: 無需PID文件,upstart會記錄主進程ID,子進程ID沒有記錄
- systemd: 無需PID文件,所有進程ID由cgroup統一接管
8. 內置的資源限制
CentOS 6: 除了ulimit,沒有其他限制進程資源的簡便方法
CentOS 7: 除了ulimit,還支持部分cgroup限制,可對進程做內存限制和cpu資源限制等
[Service]
ExecStart=...
MemoryLimit=500M
CPUShares=100
另外,CentOS 7可以通過systemd-cgtop
命令查看cgroup裏的性能數據
9. 服務異常自動重啓
upstart
start on runlevel [3]
description "mytest"
exec /root/mytest.sh
post-stop exec sleep 5
respawn
respawn limit unlimited
systemd
[Unit]
Description=mytest
[Service]
Type=simple
ExecStart=/root/mytest.sh
Restart=always
RestartSec=5
StartLimitInterval=0
[Install]
WantedBy=multi-user.target
上面2種方式均表示,無限次自動重啓,每次重啓前等待5秒
10. 寫日誌方式
CentOS 6: 自行輸出到文件中,或通過syslog記錄(如logger命令)
CentOS 7: 只要程序由systemd啓動,只需將輸出日誌到標準輸出或標準錯誤
- 建議centos7只將應用程序的一些元信息輸出到標準輸出或標準錯誤,比如啓動成功、啓動失敗等等
- 不建議將業務日誌輸出到journal。因爲journal中所有日誌都存在一個文件中,會導致2個問題:
- 如果沒有做日誌持久化,則默認存在內存中,會導致最多一半的內存被佔用
- 存儲量很大,會導致查詢其他日誌很耗時
- 解決辦法:輸出到syslog,[Service]支持StandardOutput=syslog
11. 指定每條日誌級別
CentOS 6: 通過syslog將不同級別的日誌輸出到不同文件
CentOS 7: 只需在輸出的每一行開頭加,比如
echo '<0>hello, emerg'
echo '<1>hello, alert'
echo '<2>hello, crit'
echo '<3>hello, err'
echo '<4>hello, warning'
echo '<5>hello, notice'
echo '<6>hello, info'
echo '<7>hello, debug'
12. systemd日誌永久保存
systemd日誌默認保存在內存中,因此當服務器重啓後,就無法通過journalctl來查看之前的日誌,解決方法:
mkdir -p /var/log/journal
systemctl restart systemd-journald