溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行CentOS 6與CentOS 7的服務(wù)管理對(duì)比

發(fā)布時(shí)間:2022-01-24 10:48:31 來(lái)源:億速云 閱讀:159 作者:柒染 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)如何進(jìn)行CentOS 6與CentOS 7的服務(wù)管理對(duì)比,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

centos7與centos6從初始化技術(shù),服務(wù)啟動(dòng),開(kāi)機(jī)啟動(dòng)文件等都有差別,下面和大家分享一下centos7與centos6的服務(wù)管理對(duì)比。

1. sysvinit、upstart、systemd簡(jiǎn)介

/CentOS 5CentOS 6CentOS 7備注
sysvinit???第一代,傳統(tǒng),兼容最多(/etc/init.d/、/etc/rc.X)
upstart???第二代,形似systemd雛形(/etc/init)
systemd???第三代,配合cgroup,systemd完全接管整個(gè)系統(tǒng)(/usr/lib/systemd)

2. sysvinit、upstart、systemd常用命令

動(dòng)作sysvinitupstartsystemd
查看service mytest statusinitctl status mytestsystemctl status mytest.service
啟動(dòng)service mytest startinitctl start mytestsystemctl start mytest.service
關(guān)閉service mytest stopinitctl stop mytestsystemctl stop mytest.service
強(qiáng)殺進(jìn)程kill -9 PIDkill -9 PIDsystemctl kill mytest.service –signal=9
重啟service mytest restartinitctl restart mytestsystemctl restart mytest.service
重載service mytest reloadinitctl reload mytestsystemctl reload mytest.service
開(kāi)機(jī)啟動(dòng)chkconfig mytest on/etc/init/mytest.conf里配置start on runlevel [3]systemctl enable mytest.service

3. runlevel運(yùn)行級(jí)別

運(yùn)行級(jí)別CentOS 6CentOS 7
0haltrunlevel0.target -> poweroff.target
1Single user moderunlevel1.target -> rescue.target
2Multiuser, without NFSrunlevel2.target -> multi-user.target
3Full multiuser moderunlevel3.target -> multi-user.target
4unusedrunlevel4.target -> multi-user.target
5X11runlevel5.target -> graphical.target
6rebootrunlevel6.target -> reboot.target
查看cat /etc/inittabsystemctl get-default
開(kāi)機(jī)生效編輯/etc/inittabsystemctl set-default multi-user.target
立即切換init 5systemctl isolate graphical.target

4. 日志查詢

CentOS 6: 手工在/var/log/messages、/var/log/dmesg、/var/log/secure中g(shù)rep,麻煩且效率低

CentOS 7: 統(tǒng)一使用journalctl,可以使用多個(gè)因素匹配,比如時(shí)間段、服務(wù)名、日志級(jí)別等等。另外,systemd日志默認(rèn)經(jīng)過(guò)壓縮,是二進(jìn)制文件,無(wú)法直接查看

journalctl常用命令作用CentOS 6比
journalctl所有日志,包含系統(tǒng)、內(nèi)核等等手動(dòng)在對(duì)應(yīng)日志文件中g(shù)rep
journalctl –dmesg查看當(dāng)前開(kāi)機(jī)后的內(nèi)核日志dmesg
journalctl –boot查看當(dāng)前開(kāi)機(jī)后的日志先查當(dāng)前開(kāi)機(jī)啟動(dòng)時(shí)間,然后cat /var/log/…
journalctl –boot=-1查看上一次啟動(dòng)的日志查詢上次開(kāi)機(jī)到當(dāng)前開(kāi)機(jī)之間時(shí)間,然后cat /var/log/…
journalctl –since=”2018-08-01 12:00:00″查看從指定時(shí)間開(kāi)始到當(dāng)前的日志手動(dòng)在日志里grep
journalctl –since=yesterday –until=today查看昨天0-24點(diǎn)的日志手動(dòng)在日志里grep
journalctl -n 20查看最后10行tail -n 20
journalctl -f實(shí)時(shí)滾動(dòng)顯示最新日志tail -f
journalctl -e直接翻到最后tail
journalctl -u mytest.service查看指定服務(wù)日志先查詢?nèi)罩颈4媛窂?,然后再cat查看
journalctl -p 0查看指定日志級(jí)別的日志,日志級(jí)別從0到7通過(guò)syslog將不同級(jí)別的日志放到不同文件中
journalctl -u mytest.service -o json-pretty或-o verbose查看每條日志詳細(xì)信息(包含元信息)無(wú)
journalctl –disk-usage查看日志所在的磁盤(pán)空間du -shx /var/log/messages等

5. 實(shí)現(xiàn)守護(hù)進(jìn)程

CentOS 6

  • sysvinit需要自行實(shí)現(xiàn)

    • nohup &

    • screen

    • supervisor

  • upstart和systemd類似,將程序運(yùn)行在前臺(tái)即可

CentOS 7

  • 由systemd啟動(dòng),將程序運(yùn)行在前臺(tái)即可

6. sysvinit、upstart、systemd例子

sysvinit

cat > /etc/init.d/mytest

upstart

cat > /etc/init/mytest.conf

systemd

cat > /usr/lib/systemd/system/mytest.service

7. PID管理

  • sysvinit: 需要生成PID文件,用于后期關(guān)閉、重啟等使用

  • upstart: 無(wú)需PID文件,upstart會(huì)記錄主進(jìn)程ID,子進(jìn)程ID沒(méi)有記錄

  • systemd: 無(wú)需PID文件,所有進(jìn)程ID由cgroup統(tǒng)一接管

8. 內(nèi)置的資源限制

CentOS 6: 除了ulimit,沒(méi)有其他限制進(jìn)程資源的簡(jiǎn)便方法
CentOS 7: 除了ulimit,還支持部分cgroup限制,可對(duì)進(jìn)程做內(nèi)存限制和cpu資源限制等

[Service]
ExecStart=...
MemoryLimit=500M
CPUShares=100

另外,CentOS 7可以通過(guò)systemd-cgtop命令查看cgroup里的性能數(shù)據(jù)

9. 服務(wù)異常自動(dòng)重啟

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種方式均表示,無(wú)限次自動(dòng)重啟,每次重啟前等待5秒

10. 寫(xiě)日志方式

CentOS 6: 自行輸出到文件中,或通過(guò)syslog記錄(如logger命令)

CentOS 7: 只要程序由systemd啟動(dòng),只需將輸出日志到標(biāo)準(zhǔn)輸出或標(biāo)準(zhǔn)錯(cuò)誤

  • 建議centos7只將應(yīng)用程序的一些元信息輸出到標(biāo)準(zhǔn)輸出或標(biāo)準(zhǔn)錯(cuò)誤,比如啟動(dòng)成功、啟動(dòng)失敗等等

  • 不建議將業(yè)務(wù)日志輸出到j(luò)ournal。因?yàn)閖ournal中所有日志都存在一個(gè)文件中,會(huì)導(dǎo)致2個(gè)問(wèn)題:

    • 如果沒(méi)有做日志持久化,則默認(rèn)存在內(nèi)存中,會(huì)導(dǎo)致最多一半的內(nèi)存被占用

    • 存儲(chǔ)量很大,會(huì)導(dǎo)致查詢其他日志很耗時(shí)

  • 解決辦法:輸出到syslog,[Service]支持StandardOutput=syslog

11. 指定每條日志級(jí)別

CentOS 6: 通過(guò)syslog將不同級(jí)別的日志輸出到不同文件

CentOS 7: 只需在輸出的每一行開(kāi)頭加,比如

echo 'hello, emerg'
echo 'hello, alert'
echo 'hello, crit'
echo 'hello, err'
echo 'hello, warning'
echo 'hello, notice'
echo 'hello, info'
echo 'hello, debug'

12. systemd日志永久保存

systemd日志默認(rèn)保存在內(nèi)存中,因此當(dāng)服務(wù)器重啟后,就無(wú)法通過(guò)journalctl來(lái)查看之前的日志,解決方法:

mkdir -p /var/log/journal
systemctl restart systemd-journald

看完上述內(nèi)容,你們對(duì)如何進(jìn)行CentOS 6與CentOS 7的服務(wù)管理對(duì)比有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI