美文网首页
systemd的新特性及unit常见类型分析

systemd的新特性及unit常见类型分析

作者: li_zw | 来源:发表于2018-05-04 16:50 被阅读0次

systemd是Linux下的中央系统及设定管理程式(init),包括有守护进程,程序库跟应用程序。开发目标是提供更优秀的框架以表示系统服务间的依赖关系,并以此实现系统初始化时服务的并行启动,同时达到降低shell的系统开销,最终替代现在常用的system v与BSD风格init程序。

systemd组件  与System V风格init相比,
Systemd特点:
• 平行处理所有服务:
systemd可以并行启动不相依的服务。
• on-deamon启动方式
systemd依靠systemctl命令就可以控制systemd,无需其他命令。常驻内存,因此任何要求 (on-demand) 都可以立即处理后续的 daemon 启动的任务
• 服务相依的自我检查:
例如:B服务是架构在A服务上,你手动启动B服务systemd会自动启动A服务。
• 功能分类
system所管理的服务非常之多,它定义了一个服务就是一个unit,相同的unit分为同一类型,systemd 将服务单位 (unit) 区分为 service, socket, target, path,snapshot, timer 等多种不同的类型(type), 方便管理员的分类与记忆。
• 将多个 daemons 集合成为一个群组
如同 systemV 的 init 里头有个 runlevel 的特色,systemd 亦将许多的功能集合成为一个所谓的 target 项目,这个项目主要在设计操作环境的创建, 所以是集合了许多的 daemons,亦即是执行某个 target 就是执行好多个daemon 的意思
• 向下兼容Init服务脚本
基本上, systemd 是可以相容于 init 的启动脚本的,因此,旧的 init 启动脚本也能够通过 systemd 来管理,只是更进阶的 systemd 功能就没有办法支持

Systemd的核心概念是:unit,unit由其相关配置文件进行标识,识别和配置,文件中主要包含了系统服务、监听的socket、保存的快照以及其它与init相关的信息; 这些配置文件主要保存在:
/usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system

    unit的常见类型:
        Service unit:文件扩展名为.service,用于定义系统服务;
        Target unit:文件扩展为.target,用于模拟实现“运行级别”;
        Device unit: .device,用于定义内核识别的设备;
        Mount unit: .mount,定义文件系统挂载点;
        Socket unit: .socket,用于标识进程间通信用到的socket文件;
        Snapshot unit: .snapshot, 管理系统快照;
        Swap unit: .swap, 用于标识swap设备;
        Automount unit: .automount,文件系统自动点设备;
         Path unit: .path, 用于定义文件系统中的一文件或目录;

target
system所管理的服务非常之多,它定义了一个服务就是一个unit,相同的unit分为同一类型,systemd 将服务单位 (unit) 区分为 service, socket, target, path,snapshot, timer 等多种不同的类型(type), 为了方便管理员的分类、记忆与管理,把多个unit 集合在一起,组成一个target,执行这个target即可执行对应下的多个unit。
systemd将过去的运行级别划分成对应的target,如下:
运行级别:
0 ==> runlevel0.target, poweroff.target
1 ==> runlevel1.target, rescue.target
2 ==> runlevel2.tartet, multi-user.target
3 ==> runlevel3.tartet, multi-user.target
4 ==> runlevel4.tartet, multi-user.target
5 ==> runlevel5.target, graphical.target
6 ==> runlevel6.target, reboot.target

unit文件贯穿systemd,理解unit文件对于理解systemd很重要。
systemd 将过去所谓的 daemon 执行脚本通通称为一个服务单位 (unit),而每种服务单位依据功能来区分时,就分类为不同的类型 (type)。 基本的类型有包括系统服务、数据监听与交换的插槽档服务 (socket)、储存系统状态的快照类型、提供不同类似执行等级分类的操作环境 (target) 等等

下面主要来说说type为service的unit

service unit file:
通常由三部分组成:
[Unit]:定义与Unit类型相关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等;
[Service]:与特定类型相关的专用选项;此处为Service类型;
[Install]:定义由“systemctl enable”以及"systemctl disable“命令在实现服务启用或禁用时用到的一些选项;

Unit段的常用选项:

Description:描述信息; 意义性描述;
After:定义unit的启动次序;表示当前unit应该晚于哪些unit启动;其功能与Before相反;
Requies:依赖到的其它units;强依赖,被依赖的units无法激活时,当前unit即无法激活;
Wants:依赖到的其它units;弱依赖;
Conflicts:定义units间的冲突关系;

Service段的常用选项:

Type:用于定义影响ExecStart及相关参数的功能的unit进程启动类型;

类型:
simple:
forking:
oneshot:
dbus:
notify:
idle:
EnvironmentFile:环境配置文件;
ExecStart:指明启动unit要运行命令或脚本;
ExecStartPre, ExecStartPost
ExecStop:指明停止unit要运行的命令或脚本;
Restart:

Install段的常用选项:

Alias:
RequiredBy:被哪些units所依赖;
WantedBy:被哪些units所依赖;

练习:为当前系统的httpd服务提供一个unit文件

实验过程:在Centos7上使用源码安装httpd服务,然后自己写一个unit,实现可以用systemd对服务进行管理

过程:
写unit文件可以使用yum 安装http服务来参考写


源码安装http

[root@test1 ~]# wget http://mirrors.hust.edu.cn/apache//apr/apr-1.6.3.tar.gz
[root@test1 ~]# tar apr-1.6.3.tar.gz
[root@test1 ~]# cd apr-1.6.3
[root@test1 apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@test1 apr-1.6.3]# make && make install  #先按照apr

[root@test1 ~]# tar -xvf apr-util-1.6.1.tar.gz 
[root@test1 ~]# cd apr-util-1.6.1
[root@test1 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr/ && make && make install  #安装apr-util

[root@test1 ~]# wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.33.tar.gz  
[root@test1 ~]# tar xvf httpd-2.4.33.tar.gz   //下载源码包并解压
[root@test1 ~]# cd httpd-2.4.33
[root@test1 httpd-2.4.33]# ./configure --prefix=/usr/local/apache/ --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/
[root@test1 httpd-2.4.33]# make && make install #安装完成


为httpd编写unit文件

[Unit]
Description=The Apache HTTP Server

[Service]
Type=forking
EnvironmentFile=/usr/local/apache/conf/httpd.conf
ExecStart=/usr/local/apache/bin/apachectl start
ExecReload=/usr/local/apache/bin/apachectl graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

经我测试,如果type设置为:simple,启动程序直接由execstart后面的命令直接运行,并且直接运行在后台中,命令执行起来非常快,但是httpd服务没那么快起来,简单来说,type设置为simple,命令执行虽然快,但因为主进程需要启动子程序,所以直接使用浏览器浏览是看不到效果的;如果设置为forking,命令执行起来很慢,会中断一段时间,但是执行完成后,可以马上浏览看到效果,并且使用simple有个弊端,因为httpd开辟了很多子程序提供服务,主进程的作用就是启动子进程,当启动完成以后,主进程会结束自身,所以再次使用systemctl status httpd查看,就会显示inactive状态,但其实已经启动了。如果把type设置为:forking,则不会出现这个问题

service的type详细说明:

simple:默认值,这个daemon主要由execstart后面所写的字符串来启动,启动后常驻内存。

forking:由 ExecStart 启动的程序通过 spawns 延伸出其他子程序来作为此 daemon 的主要服务。原生的父程序在启动结束后就会终止运行

传统的 unit 服务大多属于这种项目,例如 httpd 这个 WWW 服务,当 httpd 的程序因为运行过久因此即将终结了,则 systemd 会再重新生出另一个子程序持续运行后, 再将父程序删除。据说这样的性能比较好!

oneshot:与simple类似的启动方式类似,但是工作完就结束,不常驻内存

dbus:与simple类似的启动方式类似,但要设置DbusName=XX,daemon程序才会运行。

idle:与simple类似,要执行这个daemon 必须要所有的工作都顺利执行完毕后才会执行。这类的daemon 通常是开机到最后才执行即可的服务

接下来可以使用systemctl命令来管理httpd了

[root@test2 httpd-2.4.33]# systemctl start httpd #启动httpd
[root@test2 httpd-2.4.33]# systemctl stop httpd #停止httpd
[root@test2 httpd-2.4.33]# systemctl restart httpd #重启httpd
[root@test2 system]# systemctl enable httpd #加入开机启动
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

[root@test2 system]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 五 2018-05-04 16:23:41 CST; 7s ago
 Main PID: 35837 (apachectl)
   CGroup: /system.slice/httpd.service
           ├─35837 /bin/sh /usr/local/apache/bin/apachectl start
           └─35839 /usr/local/apache/bin/httpd -k start

5月 04 16:23:41 test2 systemd[1]: Started The Apache HTTP Server.
5月 04 16:23:41 test2 systemd[1]: Starting The Apache HTTP Server...

至此,源码安装http和编写 unit文件并且实现systemd对其管理的实验成功~~~(^-^

相关文章

网友评论

      本文标题:systemd的新特性及unit常见类型分析

      本文链接:https://www.haomeiwen.com/subject/wzhdrftx.html