Categraf 托管与自升级

本文介绍 Categraf 在 Linux 下如何通过 systemd 或 SysVinit 托管服务,并使用 install、remove、start、stop、status、update 等命令完成启停、状态查看和自升级。

作者 孔飞@快猫星云

Categraf 支持多种部署和托管方式,社区里管理 Categraf 的方法也很多,大家按自己的环境选择即可。之前我们认为,通过 Ansible 之类的工具批量下发和更新就能完成大部分任务。最近很多用户咨询 Categraf 有没有更方便的升级方式,这促使我们思考如何让服务管理与升级更简单。

这两天我们发布了一个新版本,本文介绍 Linux 下推荐的托管与自升级方式,也欢迎大家提 ISSUE 和 PR。

核心要点摘要

  • Linux 下推荐使用 systemd 托管 Categraf;CentOS 6 等老系统也支持 SysVinit。
  • Categraf 提供 installremovestartstopstatusupdate 等命令,减少手写 service 文件和手工替换二进制的步骤。
  • install 会创建 categraf.service,设置工作目录和配置目录,并执行 systemctl daemon-reload
  • update 需要指定 tar.gz 升级包 URL,包内必须包含 categraf 可执行文件。
  • 自升级会替换当前执行命令对应的二进制文件,升级成功后日志会输出 I! update categraf success

适用场景与命令总览

操作 命令 等价或结果
安装托管服务 sudo /opt/categraf/categraf --install 创建 categraf.service 并 reload systemd
删除托管服务 sudo /opt/categraf/categraf --remove 停止服务并删除 service 文件
启动服务 sudo /opt/categraf/categraf --start 等价于 systemctl start categraf
停止服务 sudo /opt/categraf/categraf --stop 等价于 systemctl stop categraf
查看状态 sudo /opt/categraf/categraf --status 等价于 systemctl status categraf
执行升级 sudo /opt/categraf/categraf --update --update_url <tar.gz URL> 下载、解压、替换二进制并重启服务

systemd

systemd 是一个 Linux 系统下的系统和服务管理工具,它是 Linux 启动过程的第一个进程(PID=1),它用于管理整个系统的服务状态,包括进程控制、日志记录、系统状态跟踪等等。它可以在系统启动时,并行启动所有系统服务,可以很好地管理守护进程、网络、时间、安全、日志和其他系统设置,实现了快速启动,同时也提高了系统资源的利用率和效率。

与传统的 SysVinit 不同,systemd 采用了各种新的技术来提高系统的可靠性和性能,例如 socket 激活器、并行启动、单位控制等等。更重要的是,systemd 提供了一个强大的系统统计功能,能够帮助管理员更好地了解和监控系统状态。这些统计信息包括 CPU、内存、磁盘、网络资源使用情况、特定服务的状态等等。

目前,systemd 已经成为 Linux 系统下最常见的服务管理工具之一,我们推荐使用 systemd 进行 Categraf 托管。

注:CentOS 6 等老系统 SysVinit 也支持。

Categraf 与 systemd

为了使用 systemd 更方便地进行托管,我们设计了上面的几类命令。下面分别介绍每个参数及使用方法。

install

举例,我将 https://download.flashcat.cloud/categraf-v0.3.36-linux-amd64.tar.gz 内容解压到了 /opt/categraf 目录下,执行 sudo /opt/categraf/categraf --install 命令,会在 /etc/systemd/system/ 目录下创建 categraf.service 文件,文件内容如下:

# /etc/systemd/system/categraf.service
[Unit]
Description=Opensource telemetry collector
ConditionFileIsExecutable=/opt/categraf/categraf

After=network-online.target
Wants=network-online.target

[Service]
StandardOutput=journal+console
StandardError=journal+console
StartLimitInterval=3600
StartLimitBurst=10
ExecStart=/opt/categraf/categraf "-configs" "/opt/categraf/conf"

WorkingDirectory=/opt/categraf

Restart=on-failure

RestartSec=120
EnvironmentFile=-/etc/sysconfig/categraf
KillMode=process
[Install]
WantedBy=multi-user.target

install 会做以下几件事情:

  1. 创建 categraf.service 文件;
  2. 指定 WorkingDirectory 为 Categraf 所在目录;
  3. 通过 -configs Categraf所在目录的conf 指定配置目录;
  4. 执行 systemctl daemon-reload

remove

使用示例:

sudo /opt/categraf/categraf --remove

remove 动作会执行两个操作:

  1. 停止 Categraf,等价于 systemctl stop categraf
  2. 删除 categraf.service 文件。

start

使用示例:

sudo /opt/categraf/categraf --start

start 动作等价于 systemctl start categraf

stop

使用示例:

sudo /opt/categraf/categraf --stop

stop 动作等价于 systemctl stop categraf

status

使用示例:

sudo /opt/categraf/categraf --status

status 动作等价于 systemctl status categraf

update

update 动作用于升级 Categraf,需要指定升级包的 URL。

使用示例:

sudo /opt/categraf/categraf --update --update_url https://download.flashcat.cloud/categraf-v0.3.36-linux-amd64.tar.gz

这里有如下前提

  • linux系统,systemd(或SysVinit)托管
  • update_url 指定的包是 tar.gz 格式 ,包里包含一个categraf 可执行文件

下载包之后,Categraf 会进行解压,替换二进制文件,清理临时目录和文件,并重启服务。

替换动作会使用压缩包里的 categraf 替换执行命令的二进制。示例中执行的是 /opt/categraf/categraf,因此被替换的是 /opt/categraf/categraf

如果你执行的是:

/home/flashcat/categraf --update --update_url https://download.flashcat.cloud/categraf-v0.3.36-linux-amd64.tar.gz

那么被替换的就是 /home/flashcat/categraf

二进制升级成功之后,日志会输出 I! update categraf success

自升级流程

可以把 update 理解为一个固定流程:

  1. 当前 Categraf 进程接收 --update--update_url 参数;
  2. 下载 update_url 指向的 tar.gz 升级包;
  3. 解压升级包,并确认包内包含 categraf 可执行文件;
  4. 用压缩包中的二进制替换当前执行命令对应的二进制;
  5. 清理临时目录和文件;
  6. 重启 Categraf 服务;
  7. 日志输出 I! update categraf success 表示升级成功。

常见问题

Q1:Categraf 自升级是否必须依赖 systemd?

A:Linux 下推荐使用 systemd 托管;CentOS 6 等老系统也支持 SysVinit。update 的前提是服务已经被 systemd 或 SysVinit 托管。

Q2:update_url 可以指向任意压缩包吗?

A:不可以。文中说明的前提是升级包为 tar.gz 格式,并且包里包含一个 categraf 可执行文件。

Q3:升级时会替换哪个 Categraf 二进制?

A:替换当前执行命令对应的二进制。执行 /opt/categraf/categraf --update 就替换 /opt/categraf/categraf;执行 /home/flashcat/categraf --update 就替换 /home/flashcat/categraf

Q4:如何确认升级成功?

A:二进制升级成功后,日志会输出 I! update categraf success。也可以再执行 sudo /opt/categraf/categraf --status 查看服务状态。

小结

Categraf 的托管与自升级命令解决的是两个实际问题:服务生命周期交给 systemd 或 SysVinit 管理,二进制升级由 Categraf 自身完成下载、替换、清理和重启。对需要批量管理 Agent 的环境来说,这比手工编写 service 文件和逐台替换二进制更稳定,也更容易标准化。

联系我们交流

延伸路径

继续看解决方案和产品对比

如果你正在做监控、可观测性或故障定位相关选型,建议从解决方案和产品对比继续往下看。

快猫星云 联系方式 快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云