您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(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ì)比。
/ | CentOS 5 | CentOS 6 | CentOS 7 | 備注 |
---|---|---|---|---|
sysvinit | ? | ? | ? | 第一代,傳統(tǒng),兼容最多(/etc/init.d/、/etc/rc.X) |
upstart | ? | ? | ? | 第二代,形似systemd雛形(/etc/init) |
systemd | ? | ? | ? | 第三代,配合cgroup,systemd完全接管整個(gè)系統(tǒng)(/usr/lib/systemd) |
動(dòng)作 | sysvinit | upstart | systemd |
---|---|---|---|
查看 | service mytest status | initctl status mytest | systemctl status mytest.service |
啟動(dòng) | service mytest start | initctl start mytest | systemctl start mytest.service |
關(guān)閉 | service mytest stop | initctl stop mytest | systemctl stop mytest.service |
強(qiáng)殺進(jìn)程 | 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 |
開(kāi)機(jī)啟動(dòng) | chkconfig mytest on | /etc/init/mytest.conf里配置start on runlevel [3] | systemctl enable mytest.service |
運(yùn)行級(jí)別 | 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 |
開(kāi)機(jī)生效 | 編輯/etc/inittab | systemctl set-default multi-user.target |
立即切換 | init 5 | systemctl isolate graphical.target |
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等 |
CentOS 6
sysvinit需要自行實(shí)現(xiàn)
nohup &
screen
supervisor
upstart和systemd類似,將程序運(yùn)行在前臺(tái)即可
CentOS 7
由systemd啟動(dòng),將程序運(yùn)行在前臺(tái)即可
sysvinit
cat > /etc/init.d/mytest
upstart
cat > /etc/init/mytest.conf
systemd
cat > /usr/lib/systemd/system/mytest.service
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)一接管
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ù)
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秒
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
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'
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è)資訊頻道,感謝大家的支持。
免責(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)容。