一、Systemd基础架构
1.1 核心概念
plaintext
Systemd核心组件:
组件名称主要功能重要性
systemd 初始化系统和服务管理核心
systemctl 命令行管理工具必需
journald 日志管理重要
networkd 网络管理可选
resolved DNS解析可选
timesyncd 时间同步可选
1.2 Unit类型详解
bash
# 查看所有Unit类型
$ systemctl -t help
# 常见Unit类型
Type作用示例
service 服务单元 nginx.service
socket 套接字单元 sshd.socket
target 目标单元 multi-user.target
mount 挂载单元 home.mount
device 设备单元 dev-sda.device
timer 定时器单元 backup.timer
path 文件路径监控单元 updatedb.path
二、服务配置管理
2.1 Service单元配置
ini
# /etc/systemd/system/myapp.service
[Unit]
Description=MyApplicationService
After=network.target
Wants=redis.service mysql.service
[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/myapp --config /etc/myapp/config.yml
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
2.2 服务依赖管理
bash
# 查看服务依赖关系
$ systemctl list-dependencies nginx.service
# 生成依赖图
$ systemd-analyze dot nginx.service | dot -Tsvg> nginx-deps.svg
2.3 资源限制配置
ini
# 资源限制示例
[Service]
# CPU限制
CPUQuota=200%
CPUWeight=100
# 内存限制
MemoryLimit=2G
MemoryHigh=1.5G
MemoryMax=2G
# IO限制
IOWeight=100
IODeviceWeight=/dev/sda 100
IOReadBandwidthMax=/dev/sda 50M
# 进程数限制
LimitNPROC=1000
三、服务监控与日志
3.1 服务状态监控
python
# 服务监控脚本示例
import subprocess
import json
def check_service_status(service_name):
try:
# 获取服务状态
result = subprocess.run(
['systemctl','show', service_name,'--no-page'],
capture_output=True,
text=True
)
# 解析状态信息
status ={}
for line in result.stdout.split('\n'):
if'='in line:
key, value = line.split('=',1)
status[key]= value
return{
'active_state': status.get('ActiveState'),
'sub_state': status.get('SubState'),
'memory_current': status.get('MemoryCurrent'),
'tasks_current': status.get('TasksCurrent'),
'restart_count': status.get('NRestarts')
}
exceptExceptionas e:
return{'error': str(e)}
def monitor_critical_services():
services =['nginx','mysql','redis']
status ={}
for service in services:
status[service]= check_service_status(f'{service}.service')
return status
3.2 日志配置优化
ini
# /etc/systemd/journald.conf
[Journal]
# 日志存储位置
Storage=persistent
# 日志大小限制
SystemMaxUse=1G
SystemKeepFree=1G
SystemMaxFileSize=100M
# 日志保留时间
MaxRetentionSec=1month
# 日志压缩
Compress=yes
# 转发到syslog
ForwardToSyslog=yes
四、常见问题排查
4.1 服务启动失败分析
bash
# 查看启动失败原因
$ systemctl status myapp.service
$ journalctl -u myapp.service -n 50--no-pager
# 服务启动故障排查流程
1.检查配置文件语法
$ systemd-analyze verify myapp.service
2.检查依赖服务状态
$ systemctl list-dependencies --all myapp.service
3.检查资源限制
$ systemctl show myapp.service | grep -E "Limit|Memory|CPU"
4.尝试手动启动
$ systemctl start --no-block myapp.service
$ journalctl -f -u myapp.service
4.2 性能问题诊断
bash
# 分析启动耗时
$ systemd-analyze blame
# 检查服务资源使用
$ systemd-cgtop
# 查看特定服务的资源限制
$ systemctl show myapp.service -p CPUShares-p MemoryLimit
五、最佳实践案例
5.1 Web服务配置案例
ini
# /etc/systemd/system/webapp.service
[Unit]
Description=WebApplicationService
After=network.target
Requires=mysql.service redis.service
[Service]
Type=notify
NotifyAccess=all
User=webapp
Group=webapp
WorkingDirectory=/opt/webapp
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/node /opt/webapp/server.js
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5
# 健康检查
Type=notify
NotifyAccess=all
WatchdogSec=30s
# 资源限制
MemoryLimit=1G
CPUQuota=200%
LimitNOFILE=65535
# 安全加固
PrivateTmp=true
ProtectSystem=full
ProtectHome=true
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
5.2 定时任务服务
ini
# /etc/systemd/system/backup.timer
[Unit]
Description=DailyBackupTimer
[Timer]
OnCalendar=*-*-*02:00:00
RandomizedDelaySec=1800
Persistent=true
[Install]
WantedBy=timers.target
# /etc/systemd/system/backup.service
[Unit]
Description=SystemBackupService
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7
[Install]
WantedBy=multi-user.target
六、进阶使用技巧
6.1 服务模板化
ini
# /etc/systemd/system/app@.service
[Unit]
Description=ApplicationInstance%i
After=network.target
[Service]
Type=simple
User=app-%i
Environment=PORT=%i
ExecStart=/opt/app/bin/server --port $PORT
Restart=always
[Install]
WantedBy=multi-user.target
# 使用模板创建多个实例
$ systemctl start app@8001.service
$ systemctl start app@8002.service
6.2 动态服务管理
bash
# 使用systemctl edit添加配置
$ systemctl edit nginx.service
# 临时修改服务配置
$ systemctl set-property nginx.service MemoryLimit=2G
# 重置服务配置
$ systemctl revert nginx.service
实用技巧总结
回到开头提到的几个问题,现在我们可以给出完整的解决方案:
僵尸进程问题:
服务访问问题:
检查依赖关系配置
确保网络相关服务已启动
使用systemd的健康检查功能
日志管理问题: