【问题标题】:airflow systemd fails due to gunicorn由于 gunicorn,气流 systemd 失败
【发布时间】:2018-12-23 11:31:38
【问题描述】:

我无法使用 systemd 启动气流网络服务器,即使它在 systemd 之外启动并正常运行,如下所示:

export AIRFLOW_HOME=/path/to/my/airflow/home ; airflow webserver -p 8080

systemd 日志让我相信问题来自 gunicorn,即使当我运行上述命令时 gunicorn 启动时没有问题(即它只是 systemd 中的问题)。我已经根据气流文档(运行 Ubuntu 16)配置了以下 systemd 文件。

/etc/default/气流

AIRFLOW_HOME=/path/to/my/airflow/home
SCHEDULER_RUNS=5

/lib/systemd/system/airflow-webserver.service

[Unit]
Description=Airflow webserver daemon   
After=network.target

[Service]
EnvironmentFile=/etc/default/airflow
User=ubuntu
Group=ubuntu
Type=simple
ExecStart=/bin/bash -c "export AIRFLOW_HOME=/path/to/my/airflow/home ; airflow webserver -p 8080 "

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

[Install]
WantedBy=multi-user.target

/etc/tmpfiles.d/airflow.conf

D /run/airflow 0755 airflow airflow

当我使用 systemctl 启动服务时,这会导致以下错误。

systemctl start airflow-webserver.service

Jul 15 22:41:27 ip-172-31-19-64 systemd[1]: Started Airflow webserver daemon.
Jul 15 22:41:27 ip-172-31-19-64 bash[31494]: [2018-07-15 22:41:27,555] {driver.py:120} INFO - Generating grammar tables from /usr/lib/python3.5/lib2to3/Grammar.txt
Jul 15 22:41:27 ip-172-31-19-64 bash[31494]: [2018-07-15 22:41:27,592] {driver.py:120} INFO - Generating grammar tables from /usr/lib/python3.5/lib2to3/PatternGrammar.txt
Jul 15 22:41:27 ip-172-31-19-64 bash[31494]: [2018-07-15 22:41:27,729] {__init__.py:45} INFO - Using executor SequentialExecutor
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:   ____________       _____________
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:  ____    |__( )_________  __/__  /________      __
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: ____  /| |_  /__  ___/_  /_ __  /_  __ \_ | /| / /
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: ___  ___ |  / _  /   _  __/ _  / / /_/ /_ |/ |/ /
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:  _/_/  |_/_/  /_/    /_/    /_/  \____/____/|__/
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: [2018-07-15 22:41:28,042] {models.py:189} INFO - Filling up the DagBag from /path/to/my/airflow/home/dags
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: /home/ubuntu/.local/lib/python3.5/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.cache is deprecated, use flask_cach
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:   .format(x=modname), ExtDeprecationWarning
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: Running the Gunicorn Server with:
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: Workers: 4 sync
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: Host: 0.0.0.0:8080
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: Timeout: 120
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: Logfiles: - -
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: =================================================================
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: Traceback (most recent call last):
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:   File "/usr/local/bin/airflow", line 27, in <module>
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:     args.func(args)
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:   File "/usr/local/lib/python3.5/dist-packages/airflow/bin/cli.py", line 788, in webserver
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:     gunicorn_master_proc = subprocess.Popen(run_args)
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:   File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:     restore_signals, start_new_session)
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:   File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]:     raise child_exception_type(errno_num, err_msg)
Jul 15 22:41:28 ip-172-31-19-64 bash[31494]: FileNotFoundError: [Errno 2] No such file or directory: 'gunicorn'
Jul 15 22:41:28 ip-172-31-19-64 systemd[1]: airflow-webserver.service: Main process exited, code=exited, status=1/FAILURE

是否需要进行一些配置才能使 gunicorn 与 systemd 兼容?

编辑: 根据有关这是一个权限问题的建议,我通过:sudo apt-get install gunicorn 安装了 gunicorn,并在重新运行 systemctl 时收到以下错误Error: No module named airflow.www.gunicorn_config。我认为这是由于我刚刚安装的 gunicorn 和我的 ubuntu 用户用来运行气流的 gunicorn 之间的不一致,所以我将 /usr/bin/ 中的 gunicorn 替换为前者。此修补程序可能不是修复的最佳方法,但之后我成功地能够通过 systemd 运行气流。

【问题讨论】:

  • 原因是因为使用了不同的python环境。 Gunicorn 可供您手动执行命令的用户使用,并且 gunicorn 包在根 python 中不可用。在根 python 中安装 gunicorn 并再次启动气流。也发布输出。
  • @AmalGJose 成功!我 sudo apt-get install'd gunicorn 并收到一个不同的模块未找到错误。因此,我将我的 ubuntu 用户正在运行的 gunicorn 可执行文件复制到 /usr/bin/ 中,并且它起作用了。我猜这是一种不好的做法,所以我很好奇您是否认为有更好的解决方案,但很高兴我让它工作了。谢谢!
  • @tobi6,成功! ^ 上面的其他详细信息
  • 是的,我建议的方法只是一种变通方法。实际的解决方案是在环境变量配置中配置正确的 PYTHONPATH,以便 systemd 将使用相同的 PYTHONPATH,其中存在气流和依赖包。请参考文档中的环境部分0pointer.de/public/systemd-man/systemd.exec.html#Environment=
  • @AmalGJose 谢谢我去看看

标签: gunicorn systemd airflow


【解决方案1】:

我在安装在 /srv/airflow 下的虚拟环境中的 Ubuntu 18.04 LTS 和 Apache Airflow 版本 1.10.1 上遇到了同样的问题。经过大量的试验和错误,我最终得到了这个可行的解决方案。

我的airflow-webserver.service 文件:

[Unit]
Description=Airflow webserver daemon
After=network.target

[Service]
Environment="PATH=/srv/airflow/bin"
Environment="AIRFLOW_HOME=/srv/airflow"
User=airflow
Group=airflow
Type=simple
ExecStart=/srv/airflow/bin/airflow webserver --pid /srv/airflow/webserver.pid
Restart=on-failure
RestartSec=5s
PrivateTmp=true

[Install]
WantedBy=multi-user.target

我这样做是为了安装服务:

sudo cp airflow-webserver.service /lib/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable airflow-webserver.service
sudo systemctl start airflow-webserver.service

【讨论】:

    【解决方案2】:

    是的,ExecStart=..airflow/bin/airflow 需要 ..airflow/bin/python 但 systemd 找到了系统 python。气流总是需要Environment="PATH=..airflow/bin"

    【讨论】:

      【解决方案3】:

      在 Ubuntu Bionic 中,我发现 sudo apt-get install python3-gunicorn 首先是 sudo apt-get install gunicorn 而在根 python 环境中解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-29
        • 1970-01-01
        • 1970-01-01
        • 2018-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多