Linux系统DeepSeek安装后的资源配额

云信安装大师
90
AI 质量分
2 5 月, 2025
4 分钟阅读
0 阅读

Linux系统DeepSeek安装后的资源配额配置指南

引言

在Linux服务器上部署DeepSeek等AI应用后,合理配置资源配额是确保系统稳定运行的关键。本文将详细介绍如何在Linux系统中为DeepSeek设置CPU、内存和磁盘I/O等资源限制,防止单个应用占用过多资源影响系统整体性能。

准备工作

在开始之前,请确保:

  1. 已安装DeepSeek并正常运行
  2. 具有root或sudo权限
  3. 系统已安装cgroups相关工具(大多数现代Linux发行版默认已安装)

检查cgroups是否可用:

代码片段
mount | grep cgroup

一、使用cgroups限制资源

1. 创建cgroup控制组

代码片段
# 创建memory和cpu子系统的控制组
sudo mkdir -p /sys/fs/cgroup/memory/deepseek
sudo mkdir -p /sys/fs/cgroup/cpu/deepseek

2. 设置内存限制

限制DeepSeek最大使用16GB内存:

代码片段
# 16GB = 16 * 1024 * 1024 * 1024 = 17179869184 bytes
echo "17179869184" | sudo tee /sys/fs/cgroup/memory/deepseek/memory.limit_in_bytes

# 启用OOM killer(当内存超限时终止进程)
echo "1" | sudo tee /sys/fs/cgroup/memory/deepseek/memory.oom_control

注意事项
– 设置的值应略小于物理内存总量,保留部分给系统和其他进程
– OOM killer会强制终止超限进程,可能导致数据丢失

3. 设置CPU限制

限制DeepSeek最多使用8个CPU核心的80%算力:

代码片段
# CPU核心数(假设为8核)
CORES=8
# CPU份额(100000为100%,这里设置为80000即80%)
echo "80000" | sudo tee /sys/fs/cgroup/cpu/deepseek/cpu.cfs_quota_us
echo "100000" | sudo tee /sys/fs/cgroup/cpu/deepseek/cpu.cfs_period_us

# CPU亲和性(绑定到特定核心)
echo "0-$((CORES-1))" | sudo tee /sys/fs/cgroup/cpu/deepseek/cpuset.cpus

4. 将DeepSeek进程加入控制组

首先获取DeepSeek的主进程ID:

代码片段
DEEPSEEK_PID=$(pgrep -f "deepseek")

然后将进程加入控制组:

代码片段
echo "$DEEPSEEK_PID" | sudo tee /sys/fs/cgroup/memory/deepseek/tasks
echo "$DEEPSEEK_PID" | sudo tee /sys/fs/cgroup/cpu/deepseek/tasks

二、使用systemd服务管理资源

对于使用systemd管理的服务,可以直接在service文件中配置资源限制:

1. 编辑DeepSeek的service文件

代码片段
sudo systemctl edit --full deepseek.service

添加以下内容到[Service]部分:

代码片段
[Service]
MemoryMax=16G         # 最大内存限制
CPUQuota=80%         # CPU使用率上限
CPUShares=1024       # CPU相对权重(默认1024)
IOWeight=500         # I/O权重(100-1000)

2. 重新加载并重启服务

代码片段
sudo systemctl daemon-reload
sudo systemctl restart deepseek.service

三、磁盘I/O限制

使用ioniceioprio_set限制磁盘I/O优先级:

代码片段
# 设置I/O调度类为best-effort(默认),优先级为5(范围0-7,数值越高优先级越低)
ionice -c2 -n5 -p $DEEPSEEK_PID

# c2: best-effort调度类(适合大多数后台进程) 
# n5: I/O优先级(0最高,7最低)

四、持久化配置

为了使cgroups配置在重启后依然有效,可以创建systemd服务:

1. 创建cgroups配置脚本

代码片段
sudo nano /usr/local/bin/setup-deepseek-cgroups.sh

内容如下:

代码片段
#!/bin/bash

# Memory limit (16GB)
MEMORY_LIMIT=$((16 * 1024 * 1024 * 1024))

# Create cgroups if not exist and set limits for DeepSeek app.
mkdir -p /sys/fs/cgroup/memory/deepseek || true 
mkdir -p /sys/fs/cgroup/cpu/deepseek || true 

echo "$MEMORY_LIMIT" > /sys/fs/cgroup/memory/deepseek/memory.limit_in_bytes 
echo "1" > /sys/fs/cgroup/memory/deepseek/memory.oom_control 

echo "80000" > /sys/fs/cgroup/cpu/deepseek/cpu.cfs_quota_us 
echo "100000" > /sys/fs/cgroup/cpu/deepseek/cpu.cfs_period_us 

CORES=$(nproc)
echo "0-$((CORES-1))" > /sys/fs/cgroup/cpu/deepseek/cpuset.cpus 

DEEPSEEK_PID=$(pgrep -f "deepseek")
if [ ! -z "$DEEPSEEK_PID" ]; then 
    echo "$DEEPSEEK_PID" > /sys/fs/cgroup/memory/deepseek/tasks 
    echo "$DEEPSEEK_PID" > /sys/fs/cgroup/cpu/deepseek/tasks 
fi 

2. 创建systemd服务单元文件

代码片段
sudo nano /etc/systemd/system/setup-deepseek-cgroups.service 

内容如下:

代码片段
[Unit]
Description=Setup cgroups for DeepSeek application 
After=network.target 

[Service]
Type=oneshot 
ExecStart=/usr/local/bin/setup-deepseek-cgroups.sh 

[Install]
WantedBy=multi-user.target 

Enable and start the service:

代码片段
sudo chmod +x /usr/local/bin/setup-deepseel-cgroups.sh  
sudo systemctl daemon-reload  
sudo systemctl enable setup-deepseel-cgroups.service  
sudo systemctl start setup-deepseel-cgroups.service  

Monitoring Resource Usage

To verify the resource limits are working, you can use these commands:

Check memory usage:

代码片段
cat "/proc/$DEEPSEEK_PID/cmdline"; echo ""; cat "/proc/$DEEPSEEK_PID/status | grep -i vmhwm"

Check CPU usage:

代码片段
top -p $DEEPSEEK_PID  
htop  
pidstat -p $DEEPSEEK_PID  1  5  
mpstat -P ALL  1  5  

Check disk I/O:

代码片段
iotop -oP $DEEPSEEK_PID   
iostat -xz  1  5   
pidstat -d  1  5   

View cgroups stats:

For memory:

代码片段
cat "/sys/"$CGROUP_MEMORY"/memory.stat"
cat "/sys/"$CGROUP_MEMORY"/memory.max_usage_in_bytes"

For CPU:

代码片段
cat "/sys/"$CGROUP_CPU"/cpu.stat"
cat "/sys/"$CGROUP_CPU"/cpuacct.stat"

For disk I/O (if using blkio controller):

代码片段
cat "/sys/"$CGROUP_BLKIO"/blkio.throttle.io_service_bytes"
cat "/sys/"$CGROUP_BLKIO"/blkio.throttle.io_serviced"

These commands will help you monitor whether your resource limits are being enforced and how much resources your DeepSeek application is actually using.

Troubleshooting Common Issues

If you encounter issues with the resource limits, here are some common problems and solutions:

Problem: DeepSeek application crashes with OOM (Out of Memory) errors even though memory limit seems sufficient.

Solution: Increase the memory limit slightly or check if there are memory leaks in the application.

Problem: Application performance is significantly degraded after setting CPU limits.

Solution: Increase the CPU quota or adjust the cpuset to allow usage of more cores.

Problem: Disk I/O operations become very slow after setting I/O priorities.

Solution: Adjust the ionice priority to a higher value (lower number) or consider using a different I/O scheduler like deadline or noop for better performance with AI workloads.

Problem: Changes to cgroups don’t persist after reboot.

Solution: Ensure your setup script is properly added to systemd and enabled to run at boot, as shown in the persistence section above.

Conclusion

Setting proper resource quotas for DeepSeek on Linux ensures stable system operation while allowing the AI application to perform optimally. Key takeaways:

  1. cgroups provide fine-grained control over CPU, memory, and I/O resources.
  2. systemd integration offers a more maintainable way to manage resource limits.
  3. Monitoring tools like top, htop, iotop help verify quota enforcement.
  4. Persistent configuration through systemd ensures settings survive reboots.

By following this guide, you can prevent DeepSeek from monopolizing system resources while maintaining good performance for other critical services running on the same machine.

Remember to adjust the specific values (memory limits, CPU shares etc.) according to your actual hardware capabilities and workload requirements!

原创 高质量