尝试使用systemd在ubuntu服务器上运行apache airflow

Mr.*_*ent 11 apache ubuntu systemd airflow

我正在尝试使用systemd在ubuntu服务器上运行气流.我已经按照快速入门指南和气流文档中的教程进行了操作,并设法安装了气流并使用以下命令成功运行它:

airflow webserver -p 8080
Run Code Online (Sandbox Code Playgroud)

在使用配置文件安装systemd和大量试验和错误后,我设法使用该命令运行气流

sudo systemctl start airflow
Run Code Online (Sandbox Code Playgroud)

气流持续运行一周,直到今天我用命令重新启动它

sudo systemctl restart airflow
Run Code Online (Sandbox Code Playgroud)

sudo systemctl status airflow现在运行给我以下两条消息之一:

? airflow.service - Airflow webserver daemon
 Loaded: loaded (/lib/systemd/system/airflow.service; enabled; vendor preset: enabled)
 Active: activating (auto-restart) (Result: exit-code) since Wed 2018-09-12 09:23:01 UTC; 1s ago
Process: 3115 ExecStart=/opt/miniconda3/bin/airflow webserver -p 8080 --pid /home/user/airflow/airflow-webserver.pid --daemon (code=exited, status=1/FAILURE)
Main PID: 3115 (code=exited, status=1/FAILURE)

Sep 12 09:23:01 server-service systemd[1]: airflow.service: Main process exited, code=exited, status=1/FAILURE
Sep 12 09:23:01 server-service systemd[1]: airflow.service: Unit entered failed state.
Sep 12 09:23:01 server-service systemd[1]: airflow.service: Failed with result 'exit-code'.
Run Code Online (Sandbox Code Playgroud)

要么

? airflow.service - Airflow webserver daemon
 Loaded: loaded (/lib/systemd/system/airflow.service; enabled; vendor preset: enabled)
 Active: active (running) since Wed 2018-09-12 09:23:54 UTC; 1s ago
Main PID: 3399 (airflow)
  Tasks: 1
 Memory: 56.1M
    CPU: 1.203s
 CGroup: /system.slice/airflow.service
         ??3399 /opt/miniconda3/bin/python /opt/miniconda3/bin/airflow webserver -p 8080 --pid /home/user/airflow/airflow-webserver.pid --daemon

Sep 12 09:23:54 server-service systemd[1]: Stopped Airflow webserver daemon.
Sep 12 09:23:54 server-service systemd[1]: Started Airflow webserver daemon.
Sep 12 09:23:54 server-service airflow[3399]: [2018-09-12 09:23:54,372] {__init__.py:57} INFO - Using executor SequentialExecutor
Sep 12 09:23:55 server-service airflow[3399]:   ____________       _____________
Sep 12 09:23:55 server-service airflow[3399]:  ____    |__( )_________  __/__  /________      __
Sep 12 09:23:55 server-service airflow[3399]: ____  /| |_  /__  ___/_  /_ __  /_  __ \_ | /| / /
Sep 12 09:23:55 server-service airflow[3399]: ___  ___ |  / _  /   _  __/ _  / / /_/ /_ |/ |/ /
Sep 12 09:23:55 server-service airflow[3399]:  _/_/  |_/_/  /_/    /_/    /_/  \____/____/|__/
Sep 12 09:23:55 server-service airflow[3399]:  
Sep 12 09:23:55 server-service airflow[3399]: [2018-09-12 09:23:55,124] [3399] {models.py:167} INFO - Filling up the DagBag from /root/airflow/dags
Run Code Online (Sandbox Code Playgroud)

我认为当systemd无法启动气流时会返回第一条消息,而当systemd仍处于启动气流的过程中时会返回第二条消息.

由于第一个错误消息包含airflow.service: Service hold-off time over, scheduling restart.我认为我可能有这个问题,但运行sudo systemctl enable airflow.service并没有解决问题(我认为airflow.service仍然启用,如此处所示:)Loaded: loaded (/lib/systemd/system/airflow.service; enabled; vendor preset: enabled).

在试图解决问题时,我发现了一些我不理解的奇怪事情:

  • 根据气流快速启动页面,手动运行气流将创建一个airflow-webserver.pid在气流回家中调用的文件,而使用systemd运行气流将创建一个webserver.pid/run/airflow目录中调用的文件.最初,当我尝试使用systemd运行气流时,我注意到/run/airflow/webserver.pid没有创建.设置PIDFile=/home/user/airflow/airflow-webserver.pid解决了问题; 系统使用airflow-webserver.pid文件中提供的worker pid运行气流.但是现在我已经运行sudo systemctl restart airflow了它不再起作用了; 运行airflow webserver -p 8080不会创建airflow-webserver.pid我指向的那个.

  • 由于行驶风不再自动创建/run/airflow/webserver.pid/home/user/airflow/airflow-webserver.pid我试着在所需目录中手动创建文件.但是如果我在创建/run/airflow/webserver.pid文件后使用systemd运行airflow ,它将被删除(而不是替换),如果我airflow webserver -p 8080在创建/run/airflow/webserver.pid文件后手动运行气流,那么文件将被删除.

我的airflow.service文件看起来像这样:

[Unit]
Description=Airflow webserver daemon
After=network.target postgresql.service mysql.service redis.service rabbitmq-server.service

[Service]
EnvironmentFile=/etc/sysconfig/airflow
PIDFile=/home/user/airflow/airflow-webserver.pid
User=%i
Group=%i
Type=simple
ExecStart=/opt/miniconda3/bin/airflow webserver -p 8080 --pid /home/user/airflow/airflow-webserver.pid --daemon

Restart=on-failure
RestartSec=5s
PrivateTmp=true

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

问题:如何解决这些问题,以便我可以使用systemd运行气流?

编辑:再次重新启动systemd守护进程后,我设法让气流运行(或者至少看起来如此).运行systemctl status airflow回报:

? airflow.service - Airflow webserver daemon
   Loaded: loaded (/lib/systemd/system/airflow.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-09-12 10:49:17 UTC; 6min ago
 Main PID: 30054
    Tasks: 0
   Memory: 388.0K
      CPU: 2.987s
   CGroup: /system.slice/airflow.service

Sep 12 10:49:22 server-service airflow[30031]:   File "/opt/miniconda3/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
Sep 12 10:49:22 server-service airflow[30031]:     reraise(type(exception), exception, tb=exc_tb, cause=cause)
Sep 12 10:49:22 server-service airflow[30031]:   File "/opt/miniconda3/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
Sep 12 10:49:22 server-service airflow[30031]:     raise value.with_traceback(tb)
Sep 12 10:49:22 server-service airflow[30031]:   File "/opt/miniconda3/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
Sep 12 10:49:22 server-service airflow[30031]:     context)
Sep 12 10:49:22 server-service airflow[30031]:   File "/opt/miniconda3/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
Sep 12 10:49:22 server-service airflow[30031]:     cursor.execute(statement, parameters)
Sep 12 10:49:22 server-service airflow[30031]: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: connection [SQL: 'SELECT connection.conn_id AS connection_conn_id \nFROM connection G
Sep 12 10:49:23 server-service systemd[1]: airflow.service: Supervising process 30054 which is not our child. We'll most likely not notice when it exits.
lines 1-19/19 (END)
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法在浏览器中访问气流.此外,使用systemd或手动启动气流不会产生所需的文件/run/airflow/webserver.pid/home/user/airflow/airflow-webserver.pid.我试图检查它们是否存在于其他地方,sudo find ~/ -type f -name "webserver.pid"但这并没有返回任何东西.

我认为该消息Supervising process 30054 which is not our child. We'll most likely not notice when it exits.与我的问题有关,因为在过去使用systemd成功运行气流时它没有收到此消息.是否systemctl status airflow表明气流已经运行了6分钟,因为systemd没有注意到带有pid 30054的工人不再有效?

编辑2:我发现了为什么airflow-webserver.pid"气流不会产生".当您运行airflow webserver -p 8080气流时确实会创建.pid文件,但是当您停止Web服务器时,系统会再次删除.pid文件(如果气流本身不会这样做).这解释了为什么airflow-webserver.pid不存在,但它没有解释为什么webserver.pid不在/run/airflow目录中.

小智 6

我知道我正在挖掘一个有点过时的帖子,但我也试图弄清楚为什么我无法在服务器运行时让调度程序自动运行。

我确实找到了一个在 Ubuntu 18.04 和 18.10 上对我有用的解决方案,所以希望这会有所帮助。

我在此处的链接上提供了有关如何在后端安装 Airflow 和 PostgreSQL 的完整文章。

**来自我文章的后半部分 本质上它归结为对气流调度器.系统文件进行特定更改。

这是在 Ubuntu 上实现的“陷阱”之一。创建 Airflow 的开发团队将其设计为在不同的 Linux 发行版上运行,因此需要进行一些小的(但关键的)更改,以便 Airflow 在服务器开启时自动运行。默认的 systemd 服务文件最初如下所示:

[Unit]
Description=Airflow scheduler daemon
After=network.target postgresql.service mysql.service redis.service rabbitmq-server.service
Wants=postgresql.service mysql.service redis.service rabbitmq-server.service

[Service]
EnvironmentFile=/etc/sysconfig/airflow
User=airflow
Group=airflow
Type=simple
ExecStart=/bin/airflow scheduler
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

但是,这将不起作用,因为“环境文件”协议在 Ubuntu 18 上不起作用。相反,注释掉该行并添加:

Environment="PATH=/home/ubuntu/anaconda3/envs/airflow/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Run Code Online (Sandbox Code Playgroud)

如果您希望 UI 也自动启动,您可能希望至少为 Airflow Scheduler 创建一个 systemd 服务文件,也可能为 Webserver 创建一个系统服务文件。事实上,我们在这个实现中确实想要这两个文件,所以我们将创建两个文件,airflow-scheduler.service 和airflow-webserver.service。两者都将复制到 /etc/systemd/system 文件夹中。这些如下:


气流调度程序服务

[Unit]
Description=Airflow scheduler daemon
After=network.target postgresql.service mysql.service redis.service rabbitmq-server.service
Wants=postgresql.service mysql.service redis.service rabbitmq-server.service

[Service]
#EnvironmentFile=/etc/default/airflow
Environment="PATH=/home/ubuntu/anaconda3/envs/airflow/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
User=airflow
Group=airflow
Type=simple
ExecStart=/home/ubuntu/anaconda3/envs/airflow/bin/airflow scheduler
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target
#airflow-webserver.service
Run Code Online (Sandbox Code Playgroud)

气流-webserver.service

[Unit]
Description=Airflow webserver daemon
After=network.target postgresql.service mysql.service redis.service rabbitmq-server.service
Wants=postgresql.service mysql.service redis.service rabbitmq-server.service

[Service]
#EnvironmentFile=/etc/default/airflow
Environment="PATH=/home/ubuntu/anaconda3/envs/airflow/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
User=airflow
Group=airflow
Type=simple
ExecStart=/home/ubuntu/anaconda3/envs/airflow/bin/airflow webserver -p 8085 --pid /home/ubuntu/airflow/airflow-webserver.pid
Restart=on-failure
RestartSec=5s
PrivateTmp=true

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

最后,通过超级用户复制命令 sudo cp 将这两个文件复制到 /etc/systemd/systemd 文件夹,是时候点火了:

sudo systemctl 启用气流调度程序 sudo systemctl 启动气流调度程序 sudo systemctl 启用气流网络服务器 sudo systemctl 启动气流网络服务器

  • 请注意,您可能必须将“ExecStart”更改为正确的文件夹才能使其正常工作,例如,如果您通过pip安装了airflow,则将“airflow-scheduler.service”更改为“ExecStart=/usr/local/bin/airflow Scheduler”之类的文件夹。此外,“User”和“Group”也可能不同。对于新的默认 ubuntu EC2 实例,不需要 `User=ubuntu` 和 `Group` (2认同)

vil*_*asv 0

该错误sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: connection表明您的 Airflow 进程无法访问已初始化的数据库。您确定airflow initdb在尝试设置 Airflow 网络服务器之前运行过吗?

我一直在我的AWS Airflow Stack中的 systemd 下运行 Airflow ,您可以在其中找到配置参数。为了完整起见,我将在此处转录我的配置文件,但仅通过查看您的配置不起作用的原因我无法找出答案。

我的配置是定制的,以使其在机器ec2-user内的用户下工作Amazon Linux 2,但我相信它也应该适用于 Ubuntu。请注意,因为我正在其他计算机上运行数据库、redis 和其他所有内容,所以我将它们从该After部分中删除了。

        /usr/bin/turbine:
            #!/bin/sh
            exec airflow scheduler

        /etc/sysconfig/airflow:
            AIRFLOW_HOME=/efs/airflow
            AIRFLOW__CELERY__DEFAULT_QUEUE=${queue}
            ... your environment configs
            AWS_DEFAULT_REGION=${AWS::Region}

        /usr/lib/systemd/system/airflow.service:
            [Unit]
            Description=Airflow daemon
            After=network.target
            [Service]
            EnvironmentFile=/etc/sysconfig/airflow
            User=ec2-user
            Group=ec2-user
            Type=simple
            ExecStart=/usr/bin/turbine
            Restart=always
            RestartSec=5s
            [Install]
            WantedBy=multi-user.target

        /usr/lib/tmpfiles.d/airflow.conf:
            D /run/airflow 0755 ec2-user ec2-user
Run Code Online (Sandbox Code Playgroud)

除此之外,我还设置了一个观察程序服务,以确保我们始终在 systemd 中使用最新的环境文件:

        /usr/lib/systemd/system/watcher.service:
            [Unit]
            Description=Airflow configuration watcher
            After=network.target
            [Service]
            Type=oneshot
            ExecStartPre=/usr/bin/systemctl daemon-reload
            ExecStart=/usr/bin/systemctl restart airflow
            [Install]
            WantedBy=multi-user.target

        /usr/lib/systemd/system/watcher.path:
            [Path]
            PathModified=/etc/sysconfig/airflow
            [Install]
            WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

一切都设置好了

systemctl enable airflow.service
systemctl enable watcher.path
systemctl start airflow.service
systemctl start watcher.path
Run Code Online (Sandbox Code Playgroud)