在 new Amazon Linux 2 上的 AWS Elastic Beanstalk 中创建 systemd 服务

Create systemd service in AWS Elastic Beanstalk on new Amazon Linux 2

我目前正在尝试在 AWS Elastic Beanstalk 上创建一个 worker,它从特定的 SQS 队列中提取消息(在 Symfony messenger 的帮助下)。我不想为此任务使用专用的工作实例。经过一些研究,我发现 systemd 在这里可以提供帮助,它在新的 Amazon Linux 2 个实例上默认启用。

但是,我无法创建 运行 systemd 服务。这是我的 .ebextensions/03_workers.config 文件:

files:
    /etc/systemd/system/my_worker.service:
        mode: "000755"
        owner: root
        group: root
        content: |
            [Unit]
            Description=My worker

            [Service]
            User=nginx
            Group=nginx
            Restart=always
            ExecStart=/usr/bin/nohup /usr/bin/php /var/app/current/bin/console messenger:consume integration_incoming --time-limit=60

            [Install]
            WantedBy=multi-user.target

services:
    systemd:
        my_worker:
            enabled: "true"
            ensureRunning: "true"

我看不到我的服务 运行 如果我是 运行 这个命令:

systemctl | grep my_worker

我做错了什么? :)

systemdServices 中不受支持。唯一正确的是 sysvinit:

services:
  sysvinit:
    my_worker:
      enabled: "true"
      ensureRunning: "true"

但我认为它甚至都行不通,因为这是针对亚马逊的 Linux 1,不适用于亚马逊 Linux 2.

在 Amazon Linux 2 中,您甚至不应该使用很多 .ebextensions。 AWSdocs具体写:

On Amazon Linux 2 platforms, instead of providing files and commands in .ebextensions configuration files, we highly recommend that you use Buildfile. Procfile, and platform hooks whenever possible to configure and run custom code on your environment instances during instance provisioning.

因此,您应该考虑使用 Procfile,它基本上可以实现您想要实现的目标:

Use a Procfile for long-running application processes that shouldn't exit. Elastic Beanstalk expects processes run from the Procfile to run continuously. Elastic Beanstalk monitors these processes and restarts any process that terminates. For short-running processes, use a Buildfile.

备选

因为您已经为 systemd 创建了单元文件 /etc/systemd/system/my_worker.service,您可以自己 enablestart 创建它。

为此container_commands.ebextensions中可以使用。例如:

container_commands:
   10_enable_worker:
     command: systemctl enable worker.service
   20_start_worker:
     command: systemctl start worker.service

不是officially documented,但是你可以使用亚马逊的systemd服务Linux 2.

像下面这样的块应该可以工作:

services: 
    systemd:
        __SERVICE_NAME__:
            enabled: true
            ensureRunning: true

内部包 /usr/lib/python3.7/site-packages//construction.py 提供对“systemd”服务的支持,其中列出了可识别的服务类型:sysvinitwindowssystemd

class CloudFormationCarpenter(object):
    _serviceTools = {"sysvinit": SysVInitTool, "windows": WindowsServiceTool, "systemd": SystemDTool}

请注意,systemd 服务必须支持 chkconfig,特别是您在 /etc/init.d/__SERVICE_NAME__ 的启动脚本必须包含类似于以下内容的“chkconfig”和“描述”行:

# chkconfig: 2345 70 60
# description: Continuously logs Nginx status.

如果您不正确支持 chkconfig,则 chkconfig --list __SERVICE_NAME__ 将打印错误,并且尝试部署到 Elastic Beanstalk 将在尝试启动服务时在 /var/log/cfn-init.log 中记录更详细的错误.