聊聊 Airflow 2.2.3 容器化安装

开发 前端
安装Docker可参考官方文档[1],纯净系统,就没必要卸载旧版本了,因为是云上平台,为防止配置搞坏环境,你可以先提前进行快照。

上文简单的了解了airflow的概念与使用场景,今天就通过Docker安装一下Airflow,在使用中在深入的了解一下airflow有哪些具体的功能。

1Airflow容器化部署

阿里云的宿主机环境:

  • 操作系统: Ubuntu 20.04.3 LTS
  • 内核版本: Linux 5.4.0-91-generic

安装docker

安装Docker可参考官方文档[1],纯净系统,就没必要卸载旧版本了,因为是云上平台,为防止配置搞坏环境,你可以先提前进行快照。

  1.  # 更新repo 
  2.  sudo apt-get update 
  3.  sudo apt-get install \ 
  4.     ca-certificates \ 
  5.     curl \ 
  6.     gnupg \ 
  7.     lsb-release 
  8.      
  9. # 添加docker gpg key 
  10. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg 
  11.  
  12. # 设置docker stable仓库地址 
  13. echo \ 
  14.   "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ 
  15.   $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null 
  16.    
  17. # 查看可安装的docker-ce版本 
  18. root@bigdata1:~# apt-cache madison docker-ce 
  19.  docker-ce | 5:20.10.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages 
  20.  docker-ce | 5:20.10.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages 
  21.  docker-ce | 5:20.10.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages 
  22.  docker-ce | 5:20.10.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages 
  23.  
  24. # 安装命令格式 
  25. #sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io 
  26. # 安装指定版本 
  27. sudo apt-get install docker-ce=5:20.10.12~3-0~ubuntu-focal docker-ce-cli=5:20.10.12~3-0~ubuntu-focal containerd.io 

优化Docker配置

  1.     "data-root""/var/lib/docker"
  2.     "exec-opts": [ 
  3.         "native.cgroupdriver=systemd" 
  4.     ], 
  5.     "registry-mirrors": [ 
  6.         "https://****.mirror.aliyuncs.com" #此处配置一些加速的地址,比如阿里云的等等... 
  7.     ], 
  8.     "storage-driver""overlay2"
  9.     "storage-opts": [ 
  10.         "overlay2.override_kernel_check=true" 
  11.     ], 
  12.     "log-driver""json-file"
  13.     "log-opts": { 
  14.         "max-size""100m"
  15.         "max-file""3" 
  16.     } 

配置开机自己

  1. systemctl daemon-reload 
  2. systemctl enable --now docker.service 

容器化安装Airflow

数据库选型

根据官网的说明,数据库建议使用MySQL8+和postgresql 9.6+,在官方的docker-compose脚本[2]中使用是PostgreSQL,因此我们需要调整一下docker-compose.yml的内容

  1. --- 
  2. version: '3' 
  3. x-airflow-common: 
  4.   &airflow-common 
  5.   # In order to add custom dependencies or upgrade provider packages you can use your extended image. 
  6.   # Comment the image line, place your Dockerfile in the directory where you placed the docker-compose.yaml 
  7.   # and uncomment the "build" line below, Then run `docker-compose build` to build the images. 
  8.   image: ${AIRFLOW_IMAGE_NAME:-apache/airflow:2.2.3} 
  9.   # build: . 
  10.   environment: 
  11.     &airflow-common-env 
  12.     AIRFLOW__CORE__EXECUTOR: CeleryExecutor 
  13.     AIRFLOW__CORE__SQL_ALCHEMY_CONN: mysql+mysqldb://airflow:aaaa@mysql/airflow # 此处替换为mysql连接方式 
  14.     AIRFLOW__CELERY__RESULT_BACKEND: db+mysql://airflow:aaaa@mysql/airflow # 此处替换为mysql连接方式 
  15.     AIRFLOW__CELERY__BROKER_URL: redis://:xxxx@redis:6379/0 # 为保证安全,我们对redis开启了认证,因此将此处xxxx替换为redis密码 
  16.     AIRFLOW__CORE__FERNET_KEY: '' 
  17.     AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: 'true' 
  18.     AIRFLOW__CORE__LOAD_EXAMPLES: 'true' 
  19.     AIRFLOW__API__AUTH_BACKEND: 'airflow.api.auth.backend.basic_auth' 
  20.     _PIP_ADDITIONAL_REQUIREMENTS: ${_PIP_ADDITIONAL_REQUIREMENTS:-} 
  21.   volumes: 
  22.     - ./dags:/opt/airflow/dags 
  23.     - ./logs:/opt/airflow/logs 
  24.     - ./plugins:/opt/airflow/plugins 
  25.   user"${AIRFLOW_UID:-50000}:0" 
  26.   depends_on: 
  27.     &airflow-common-depends-on 
  28.     redis: 
  29.       condition: service_healthy 
  30.     mysql: # 此处修改为mysql service名称 
  31.       condition: service_healthy 
  32.  
  33. services: 
  34.   mysql: 
  35.     image: mysql:8.0.27 # 修改为mysql最新版镜像 
  36.     environment: 
  37.       MYSQL_ROOT_PASSWORD: bbbb # MySQL root账号密码 
  38.       MYSQL_USER: airflow 
  39.       MYSQL_PASSWORD: aaaa # airflow用户的密码 
  40.       MYSQL_DATABASE: airflow 
  41.     command: 
  42.       --default-authentication-plugin=mysql_native_password # 指定默认的认证插件 
  43.       --collation-server=utf8mb4_general_ci # 依据官方指定字符集 
  44.       --character-set-server=utf8mb4 # 依据官方指定字符编码 
  45.     volumes: 
  46.       - /apps/airflow/mysqldata8:/var/lib/mysql # 持久化MySQL数据 
  47.       - /apps/airflow/my.cnf:/etc/my.cnf # 持久化MySQL配置文件 
  48.     healthcheck: 
  49.       test:  mysql --user=$$MYSQL_USER --password=$$MYSQL_PASSWORD -e 'SHOW DATABASES;' # healthcheck command 
  50.       interval: 5s 
  51.       retries: 5 
  52.     restart: always 
  53.  
  54.   redis: 
  55.     image: redis:6.2 
  56.     expose: 
  57.       - 6379 
  58.     command: redis-server --requirepass xxxx # redis-server开启密码认证 
  59.     healthcheck: 
  60.       test: ["CMD""redis-cli","-a","xxxx","ping"] # redis使用密码进行healthcheck 
  61.       interval: 5s 
  62.       timeout: 30s 
  63.       retries: 50 
  64.     restart: always 
  65.  
  66.   airflow-webserver: 
  67.     <<: *airflow-common 
  68.     command: webserver 
  69.     ports: 
  70.       - 8080:8080 
  71.     healthcheck: 
  72.       test: ["CMD""curl""--fail""http://localhost:8080/health"
  73.       interval: 10s 
  74.       timeout: 10s 
  75.       retries: 5 
  76.     restart: always 
  77.     depends_on: 
  78.       <<: *airflow-common-depends-on 
  79.       airflow-init: 
  80.         condition: service_completed_successfully 
  81.  
  82.   airflow-scheduler: 
  83.     <<: *airflow-common 
  84.     command: scheduler 
  85.     healthcheck: 
  86.       test: ["CMD-SHELL"'airflow jobs check --job-type SchedulerJob --hostname "$${HOSTNAME}"'
  87.       interval: 10s 
  88.       timeout: 10s 
  89.       retries: 5 
  90.     restart: always 
  91.     depends_on: 
  92.       <<: *airflow-common-depends-on 
  93.       airflow-init: 
  94.         condition: service_completed_successfully 
  95.  
  96.   airflow-worker: 
  97.     <<: *airflow-common 
  98.     command: celery worker 
  99.     healthcheck: 
  100.       test: 
  101.         - "CMD-SHELL" 
  102.         - 'celery --app airflow.executors.celery_executor.app inspect ping -d "celery@$${HOSTNAME}"' 
  103.       interval: 10s 
  104.       timeout: 10s 
  105.       retries: 5 
  106.     environment: 
  107.       <<: *airflow-common-env 
  108.       # Required to handle warm shutdown of the celery workers properly 
  109.       # See https://airflow.apache.org/docs/docker-stack/entrypoint.html#signal-propagation 
  110.       DUMB_INIT_SETSID: "0" 
  111.     restart: always 
  112.     depends_on: 
  113.       <<: *airflow-common-depends-on 
  114.       airflow-init: 
  115.         condition: service_completed_successfully 
  116.  
  117.   airflow-triggerer: 
  118.     <<: *airflow-common 
  119.     command: triggerer 
  120.     healthcheck: 
  121.       test: ["CMD-SHELL"'airflow jobs check --job-type TriggererJob --hostname "$${HOSTNAME}"'
  122.       interval: 10s 
  123.       timeout: 10s 
  124.       retries: 5 
  125.     restart: always 
  126.     depends_on: 
  127.       <<: *airflow-common-depends-on 
  128.       airflow-init: 
  129.         condition: service_completed_successfully 
  130.  
  131.   airflow-init: 
  132.     <<: *airflow-common 
  133.     entrypoint: /bin/bash 
  134.     # yamllint disable rule:line-length 
  135.     command: 
  136.       - -c 
  137.       - | 
  138.         function ver() { 
  139.           printf "%04d%04d%04d%04d" $${1//./ } 
  140.         } 
  141.         airflow_version=$$(gosu airflow airflow version) 
  142.         airflow_version_comparable=$$(ver $${airflow_version}) 
  143.         min_airflow_version=2.2.0 
  144.         min_airflow_version_comparable=$$(ver $${min_airflow_version}) 
  145.         if (( airflow_version_comparable < min_airflow_version_comparable )); then 
  146.           echo 
  147.           echo -e "\033[1;31mERROR!!!: Too old Airflow version $${airflow_version}!\e[0m" 
  148.           echo "The minimum Airflow version supported: $${min_airflow_version}. Only use this or higher!" 
  149.           echo 
  150.           exit 1 
  151.         fi 
  152.         if [[ -z "${AIRFLOW_UID}" ]]; then 
  153.           echo 
  154.           echo -e "\033[1;33mWARNING!!!: AIRFLOW_UID not set!\e[0m" 
  155.           echo "If you are on Linux, you SHOULD follow the instructions below to set " 
  156.           echo "AIRFLOW_UID environment variable, otherwise files will be owned by root." 
  157.           echo "For other operating systems you can get rid of the warning with manually created .env file:" 
  158.           echo "    See: https://airflow.apache.org/docs/apache-airflow/stable/start/docker.html#setting-the-right-airflow-user" 
  159.           echo 
  160.         fi 
  161.         one_meg=1048576 
  162.         mem_available=$$(($$(getconf _PHYS_PAGES) * $$(getconf PAGE_SIZE) / one_meg)) 
  163.         cpus_available=$$(grep -cE 'cpu[0-9]+' /proc/stat) 
  164.         disk_available=$$(df / | tail -1 | awk '{print $$4}'
  165.         warning_resources="false" 
  166.         if (( mem_available < 4000 )) ; then 
  167.           echo 
  168.           echo -e "\033[1;33mWARNING!!!: Not enough memory available for Docker.\e[0m" 
  169.           echo "At least 4GB of memory required. You have $$(numfmt --to iec $$((mem_available * one_meg)))" 
  170.           echo 
  171.           warning_resources="true" 
  172.         fi 
  173.         if (( cpus_available < 2 )); then 
  174.           echo 
  175.           echo -e "\033[1;33mWARNING!!!: Not enough CPUS available for Docker.\e[0m" 
  176.           echo "At least 2 CPUs recommended. You have $${cpus_available}" 
  177.           echo 
  178.           warning_resources="true" 
  179.         fi 
  180.         if (( disk_available < one_meg * 10 )); then 
  181.           echo 
  182.           echo -e "\033[1;33mWARNING!!!: Not enough Disk space available for Docker.\e[0m" 
  183.           echo "At least 10 GBs recommended. You have $$(numfmt --to iec $$((disk_available * 1024 )))" 
  184.           echo 
  185.           warning_resources="true" 
  186.         fi 
  187.         if [[ $${warning_resources} == "true" ]]; then 
  188.           echo 
  189.           echo -e "\033[1;33mWARNING!!!: You have not enough resources to run Airflow (see above)!\e[0m" 
  190.           echo "Please follow the instructions to increase amount of resources available:" 
  191.           echo "   https://airflow.apache.org/docs/apache-airflow/stable/start/docker.html#before-you-begin" 
  192.           echo 
  193.         fi 
  194.         mkdir -p /sources/logs /sources/dags /sources/plugins 
  195.         chown -R "${AIRFLOW_UID}:0" /sources/{logs,dags,plugins} 
  196.         exec /entrypoint airflow version 
  197.     # yamllint enable rule:line-length 
  198.     environment: 
  199.       <<: *airflow-common-env 
  200.       _AIRFLOW_DB_UPGRADE: 'true' 
  201.       _AIRFLOW_WWW_USER_CREATE: 'true' 
  202.       _AIRFLOW_WWW_USER_USERNAME: ${_AIRFLOW_WWW_USER_USERNAME:-airflow} 
  203.       _AIRFLOW_WWW_USER_PASSWORD: ${_AIRFLOW_WWW_USER_PASSWORD:-airflow} 
  204.     user"0:0" 
  205.     volumes: 
  206.       - .:/sources 
  207.  
  208.   airflow-cli: 
  209.     <<: *airflow-common 
  210.     profiles: 
  211.       - debug 
  212.     environment: 
  213.       <<: *airflow-common-env 
  214.       CONNECTION_CHECK_MAX_COUNT: "0" 
  215.     # Workaround for entrypoint issue. See: https://github.com/apache/airflow/issues/16252 
  216.     command: 
  217.       - bash 
  218.       - -c 
  219.       - airflow 
  220.  
  221.   flower: 
  222.     <<: *airflow-common 
  223.     command: celery flower 
  224.     ports: 
  225.       - 5555:5555 
  226.     healthcheck: 
  227.       test: ["CMD""curl""--fail""http://localhost:5555/"
  228.       interval: 10s 
  229.       timeout: 10s 
  230.       retries: 5 
  231.     restart: always 
  232.     depends_on: 
  233.       <<: *airflow-common-depends-on 
  234.       airflow-init: 
  235.         condition: service_completed_successfully 

在官方docker-compose.yaml基础上只修改了x-airflow-common,MySQL,Redis相关配置,接下来就应该启动容器了,在启动之前,需要创建几个持久化目录:

  1. mkdir -p ./dags ./logs ./plugins 
  2. echo -e "AIRFLOW_UID=$(id -u)" > .env # 注意,此处一定要保证AIRFLOW_UID是普通用户的UID,且保证此用户有创建这些持久化目录的权限 

如果不是普通用户,在运行容器的时候,会报错,找不到airflow模块

  1. docker-compose up airflow-init #初始化数据库,以及创建表 
  2. docker-compose up -d #创建airflow容器 

当出现容器的状态为unhealthy的时候,要通过docker inspect $container_name查看报错的原因,至此airflow的安装就已经完成了。

参考资料

[1]Install Docker Engine on Ubuntu: https://docs.docker.com/engine/install/ubuntu/

[2]官方docker-compose.yaml: https://airflow.apache.org/docs/apache-airflow/2.2.3/docker-compose.yaml

 

责任编辑:武晓燕 来源: 云原生生态圈
相关推荐

2020-06-10 08:55:36

Docker容器工具

2022-01-05 19:34:18

AirflowCeleryMYSQL

2023-04-28 08:43:46

2020-07-14 07:27:48

容器IoCSpring

2023-07-03 09:59:00

并发编程并发容器

2022-03-04 08:45:11

Docker开源Linux

2023-11-28 07:55:05

Calico容器网络

2020-06-10 08:28:51

Kata容器I

2021-08-11 10:50:35

AirFlow MaxCompute阿里云

2017-07-04 13:37:57

调度工具Airflow开源

2023-03-27 08:49:51

2023-09-11 07:25:52

2021-09-14 13:25:23

容器pod僵尸进程

2019-01-09 13:20:28

GPU虚拟化应用

2022-01-03 23:59:15

任务调度框架

2018-04-24 09:05:09

容器存储接口

2022-01-19 08:01:13

Linuxdocker容器

2022-05-09 08:17:37

InstantJava字符

2024-01-30 09:14:35

容器资源管理

2023-09-07 20:04:06

前后端趋势Node.js
点赞
收藏

51CTO技术栈公众号