资源隔离-systemd

来源:互联网 发布:js实现图片放大缩小 编辑:程序博客网 时间:2024/06/03 06:43

介绍

systemd系统和用户

位置

/usr/lib/systemd/system/usr/lib/systemd/user

unit

systemd管理的基础单元叫unit,分为如下几种类型

Service unit:系统服务Target unit:多个 Unit 构成的一个组Device Unit:硬件设备Mount Unit:文件系统的挂载点Automount Unit:自动挂载点Path Unit:文件或路径Scope Unit:不是由 Systemd 启动的外部进程Slice Unit:进程组Snapshot Unit:Systemd 快照,可以切回某个快照Socket Unit:进程间通信的 socketSwap Unit:swap 文件Timer Unit:定时器

常用命令

# 列出正在运行的 Unit$ systemctl list-units# 列出所有Unit,包括没有找到配置文件的或者启动失败的$ systemctl list-units --all# 列出所有没有运行的 Unit$ systemctl list-units --all --state=inactive# 列出所有加载失败的 Unit$ systemctl list-units --failed# 列出所有正在运行的、类型为 service 的 Unit$ systemctl list-units --type=service# 显示系统状态$ systemctl status# 显示单个 Unit 的状态$ sysystemctl status bluetooth.service# 显示某个 Unit 是否正在运行$ systemctl is-active application.service# 显示某个 Unit 是否处于启动失败状态$ systemctl is-failed application.service# 显示某个 Unit 服务是否建立了启动链接$ systemctl is-enabled application.service# 立即启动一个服务$ sudo systemctl start apache.service# 立即停止一个服务$ sudo systemctl stop apache.service# 重启一个服务$ sudo systemctl restart apache.service# 杀死一个服务的所有子进程$ sudo systemctl kill apache.service# 重新加载一个服务的配置文件$ sudo systemctl reload apache.service# 重载所有修改过的配置文件$ sudo systemctl daemon-reload# 显示某个 Unit 的所有底层参数$ systemctl show httpd.service# 显示某个 Unit 的指定属性的值$ systemctl show -p CPUShares httpd.service# 设置某个 Unit 的指定属性$ sudo systemctl set-property httpd.service CPUShares=500

测试

启动

在slice:test2.slice下,启动一个service:toptest2.service
可以看到,启动后的进程pid是8929

[root@rac2 ~]# systemd-run --unit=toptest2 --slice=test2 top -bRunning as unit toptest2.service.
[root@rac2 ~]# systemctl status toptest2.service● toptest2.service - /usr/bin/top -b   Loaded: loaded (/run/systemd/system/toptest2.service; static; vendor preset: disabled)  Drop-In: /run/systemd/system/toptest2.service.d           └─50-Description.conf, 50-ExecStart.conf, 50-Slice.conf   Active: active (running) since Fri 2017-03-10 14:46:18 CST; 6s ago Main PID: 8928 (top)   CGroup: /test2.slice/toptest2.service           └─8928 /usr/bin/top -bMar 10 14:46:24 rac2 top[8928]: 25230 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/6:0Mar 10 14:46:24 rac2 top[8928]: 26164 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/3:2Mar 10 14:46:24 rac2 top[8928]: 27118 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/8:2Mar 10 14:46:24 rac2 top[8928]: 29597 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/9:2Mar 10 14:46:24 rac2 top[8928]: 30021 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/2:0Mar 10 14:46:24 rac2 top[8928]: 31018 root      20   0  116276   3076   1800 S   0.0  0.0   0:00.75 bashMar 10 14:46:24 rac2 top[8928]: 31112 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/19+Mar 10 14:46:24 rac2 top[8928]: 31827 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/1:2Mar 10 14:46:24 rac2 top[8928]: 32082 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/7:+Mar 10 14:46:24 rac2 top[8928]: 32136 polkitd   20   0  524900  11184   4640 S   0.0  0.0   0:02.24 polkitd
[root@rac2 ~]# ps -ef | grep toproot      8928     1  0 14:46 ?        00:00:00 /usr/bin/top -b

CPU亲和力

CPUAffinity
CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行.
一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码可能会获得更好的性能.

[root@rac2 ~]# ps -ef | grep toproot      8928     1  0 14:46 ?        00:00:00 /usr/bin/top -b

先查看pid是8928的进程当前的cpu亲和力

[root@rac2 ~]# taskset -c -p 8928pid 8928's current affinity list: 0-31

设定仅在cpu2上运行

[root@rac2 ~]# taskset -p -c 2 8928pid 8928's current affinity list: 0-31pid 8928's new affinity list: 2[root@rac2 ~]# taskset -c -p 8928pid 8928's current affinity list: 2

设定仅在cpu3,5,7-10上运行

[root@rac2 ~]#  taskset -p -c 3,5,7-10 8928pid 8928's current affinity list: 2pid 8928's new affinity list: 3,5,7-10[root@rac2 ~]# taskset -c -p 8928pid 8928's current affinity list: 3,5,7-10

service文件方式

下面service写的不太标准,主要是展示参数CPUAffinity

[Unit]Description=t1[Service]Type=simpleExecStart=/usr/bin/top -bExecStop=/bin/kill -WINCH ${MAINPID}KillSignal=SIGCONTPrivateTmp=trueCPUAffinity=3[Install]WantedBy=multi-user.target

限制内存

systemd-run --unit=memorytest1 --slice=test2  /usr/bin/bash /tmp/highmem.shsystemctl set-property memorytest1.service MemoryLimit=2G

通过下面命令可以看进程是否在运行

systemctl status memorytest1.service

通过top可以观察进程消耗内存不会超过限制
譬如128G的服务器,设定为2G,则内存消耗在1.5%上下浮动

限制磁盘读取速度

systemd-run --unit=ioread --slice=test2 dd if=/dev/sdk of=/dev/nulliotop可以看到读取速度是:180M/s
systemctl set-property ioread BlockIOReadBandwidth='/dev/sdk 50M'iotop可以看到读取速度是:50M/s
0 0
原创粉丝点击