最详细的CentOS 6与7对比(二):服务管理对比

主题将从3个角度进行对比

  1. 常见设置(CentOS 6 vs CentOS 7
  2. 服务管理(Sysvinit vs Upstart vs Systemd)
  3. 性能测试(cpu/mem/io/oltp)

本文为第二部分:服务管理的对比


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

最详细的CentOS 6与7对比(二):服务管理对比

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 最详细的CentOS 6与7对比(二):服务管理对比 https://www.lizi.tw/soft/16741.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

最详细的CentOS 6与7对比(二):服务管理对比-海报

分享本文封面