Catálogo
-
下载系统服务配置模板
1
2
3wget https://raw.githubusercontent.com/apache/airflow/master/scripts/systemd/airflow
wget https://raw.githubusercontent.com/apache/airflow/master/scripts/systemd/airflow-webserver.service
wget https://raw.githubusercontent.com/apache/airflow/master/scripts/systemd/airflow-scheduler.service -
第一个文件配置了在启动系统服务时候需要设置的环境变量,即
AIRFLOW_HOME
,AIRFLOW_CONFIG
,设置成和安装篇里面一样的值即可。而后将其拷贝到适当位置,推荐为/etc/sysconfig/airflow
,并将所有权修改到运行Airflow的用户下。 -
然后配置
airflow-scheduler.service
如下:airflow-scheduler.service
:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[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=<2中的环境变量配置文件>
User=<airflow user>
Group=<airflow user group>
Type=simple
ExecStart=<path to airflow>/airflow scheduler'
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target -
再配置
airflow-webserver.service
如下
airflow-webserver.service
:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17[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=<2中的环境变量配置文件>
User=<airflow user>
Group=<airflow user group>
Type=simple
ExecStart=<path to airflow>/airflow webserver --pid /run/airflow/webserver.pid
Restart=on-failure
RestartSec=5s
PrivateTmp=true
[Install]
WantedBy=multi-user.target这里我们发现需要一个
pid
文件1
2mkdir /run/airflow
chown <airflow user>:<airflow user group> /run/airflow -
然后启动服务
1
2
3sudo systemctl daemon-reload # 让更改生效
sudo systemctl start airflow-scheduler.service
sudo systemctl start airflow-webserver.service -
但这个时候发现很奇怪的问题,
airflow-scheduler
服务总是时断时续,报错“找不到airflow”。
研究后发现,虽然我们通过指定<path to airflow>/airflow
保证了webserver
和scheduler
本身是通过Anaconda环境运行的,但是scheduler
在运行任务时候,是运行airflow task run xxx ...
,这个时候由于systemctl
并不会自动激活conda环境,所以就无法找到airflow
这个命令。所以需要在
ExecStart
里面加入激活环境的相关命令,airflow-scheduler.service
须变更如下:1
ExecStart=/bin/bash -c 'source /home/booksword/anaconda3/etc/profile.d/conda.sh && conda activate && airflow scheduler'
airflow-scheduler.service
变更如下:1
ExecStart=/bin/bash -c 'source /home/booksword/anaconda3/etc/profile.d/conda.sh && conda activate && airflow webserver --pid /run/airflow/webserver.pid'