docker(도커) 및 쿠버네티스

(airflow) docker-compose.yaml 파일 분석

데이터왕 2023. 12. 29. 22:43

목적 : airflow setup을 위해 제작된 docker-compose.yaml 파일의 내용을 살펴본다.

 

airflow 설치

  1. 깃허브에서 airflow-setup 파일 받음
    weare@DESKTOP-BE1I4GE:~$ git clone https://github.com/keeyong/airflow-setup.git
    Cloning into 'airflow-setup'...
    remote: Enumerating objects: 65, done.
    remote: Counting objects: 100% (65/65), done.
    remote: Compressing objects: 100% (63/63), done.
    remote: Total 65 (delta 27), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (65/65), 1.32 MiB | 20.85 MiB/s, done.
    Resolving deltas: 100% (27/27), done.

  2. 해당 폴더로 이동
    weare@DESKTOP-BE1I4GE:~$ cd airflow-setup

  3. curl을 사용하여 URL에서 yaml 파일을 다운로드하고 -O 옵션을 사용하여 로컬에 파일을 저장
    weare@DESKTOP-BE1I4GE:~/airflow-setup$ curl -LfO "https://airflow.apache.org/docs/apache-airflow/2.5.1/docker-compose.yaml"
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 10493  100 10493    0     0   134k      0 --:--:-- --:--:-- --:--:--  134k

  4. yaml 파일에 정의된 도커 이미지를 다운로드. 로컬에 없는, postres, redis 등은 docker hub 에서 가져옴.
    weare@DESKTOP-BE1I4GE:~/airflow-setup$ docker-compose -f docker-compose.yaml pull
    WARNING: The AIRFLOW_UID variable is not set. Defaulting to a blank string.
    Pulling postgres          ... done
    Pulling redis             ... done
    Pulling airflow-init      ... done
    Pulling airflow-triggerer ... done
    Pulling airflow-worker    ... done
    Pulling airflow-scheduler ... done
    Pulling airflow-webserver ... done
    + postres, redis 는 외부에서 가져와서 airflow-가 앞에 붙지 않았다.

  5. 서버 실행 (에러발생)
    weare@DESKTOP-BE1I4GE:~/airflow-setup$ docker-compose -f docker-compose.yaml up
    WARNING: The AIRFLOW_UID variable is not set. Defaulting to a blank string.
    Creating airflow-setup_redis_1    ... done
    Creating airflow-setup_postgres_1 ... done
    Creating airflow-setup_airflow-init_1 ... done
    Creating airflow-setup_airflow-scheduler_1 ... done
    Creating airflow-setup_airflow-webserver_1 ... done
    Creating airflow-setup_airflow-triggerer_1 ... done
    Creating airflow-setup_airflow-worker_1    ... done
    Attaching to airflow-setup_redis_1, airflow-setup_postgres_1, airflow-setup_airflow-init_1, airflow-setup_airflow-scheduler_1, airflow-setup_airflow-webserver_1, airflow-setup_airflow-triggerer_1, airflow-setup_airflow-worker_1
    airflow-init_1       |
    airflow-init_1       | WARNING!!!: AIRFLOW_UID not set!
    airflow-init_1       | If you are on Linux, you SHOULD follow the instructions below to set
    airflow-init_1       | AIRFLOW_UID environment variable, otherwise files will be owned by root.
    airflow-init_1       | For other operating systems you can get rid of the warning with manually created .env file:

    AIRFLOW_UID not set! : AIRFLOW_UID가 설정되지 않았다는 내용, 필요한 폴더들을 만들고 환경변수가 저장될 위치가 필요하다.
  6. 에러 해결을 위해 airflow 운용에 필요한 폴더( dags , logs, plugins )들을 만들고 환경변수가 저장될 위치가 필요하다.

    1) DAG 파일, 로그, 플러그인 등이 저장되는 경로를 설정
        `mkdir -p ./dags ./logs ./plugins`:
       - `mkdir`: 디렉토리를 생성
       - `-p`: 부모 디렉토리가 없으면 함께 생성합니다.
       - `./dags`: 현재 작업 디렉토리에 "dags"라는 디렉토리를 생성합니다. Airflow에서 DAG 파일을 저장하는 디렉토리
       - `./logs`: 현재 작업 디렉토리에 "logs"라는 디렉토리를 생성합니다. Airflow 로그 파일이 저장되는 디렉토리입니다.
       - `./plugins`: 현재 작업 디렉토리에 "plugins"라는 디렉토리를 생성합니다. Airflow 플러그인이 위치하는 디렉토리

    2) AIRFLOW_UID를 만들고 저장할 위치를 설정
    `echo -e "AIRFLOW_UID=$(id -u)\nAIRFLOW_GID=0" > .env`:
       - `echo`: 화면에 텍스트를 출력하는 명령어입니다.
       - `-e`: 이 옵션은 이스케이프 시퀀스 (예: `\n` 등)를 해석하도록 합니다.
       - `"AIRFLOW_UID=$(id -u)\nAIRFLOW_GID=0"`: 환경 변수 파일인 `.env`에 저장할 내용으로, 현재 사용자의 UID(사용자 ID)를 `AIRFLOW_UID`로 설정하고, `AIRFLOW_GID`를 0으로 설정합니다. `id -u`는 현재 사용자의 UID를 가져오는 명령어입니다.
       - `> .env`: 앞서 설정한 내용을 현재 디렉토리에 `.env` 파일로 저장합니다.

  7. 다시 서버실행 weare@DESKTOP-BE1I4GE:~/airflow-setup$ docker-compose -f docker-compose.yaml up
    airflow-webserver_1  | 127.0.0.1 - - [29/Dec/2023:13:06:38 +0000] "GET /health HTTP/1.1" 200 141 "-" "curl/7.74.0"
    airflow-webserver_1  | 127.0.0.1 - - [29/Dec/2023:13:06:48 +0000] "GET /health HTTP/1.1" 200 141 "-" "curl/7.74.0"
    airflow-webserver_1  | 127.0.0.1 - - [29/Dec/2023:13:06:58 +0000] "GET /health HTTP/1.1" 200 141 "-" "curl/7.74.0"
    airflow-webserver_1  | 127.0.0.1 - - [29/Dec/2023:13:07:08 +0000] "GET /health HTTP/1.1" 200 141 "-" "curl/7.74.0"
    airflow-webserver_1  | 127.0.0.1 - - [29/Dec/2023:13:07:18 +0000] "GET /health HTTP/1.1" 200 141 "-" "curl/7.74.0"
    airflow-webserver_1  | 127.0.0.1 - - [29/Dec/2023:13:07:28 +0000] "GET /health HTTP/1.1" 200 141 "-" "curl/7.74.0"
    정상동작 됨을 볼 수 있다.

  8. http://localhost:8080/ 접속 (포트 8080과 , id, pw :airflow 이고, 이는 전부 yaml파일에 설정돼있다.)

 

 

docker-compose.yaml 파일 내용분석

  • x-airflow-common

    `x-airflow-common`이라는 용어는 Apache Airflow의 DAG 정의에서 사용되는 확장(`x`) 속성 중 하나일 수 있습니다. DAG(Directed Acyclic Graph)는 Airflow에서 워크플로우를 정의하는 방식입니다.

    `x-airflow-common`은 일반적으로 Airflow DAG 정의에서 공통적으로 사용되는 설정이나 속성을 담기 위한 사용자 정의 확장 속성입니다. 이 속성을 사용하면 여러 DAG에서 공유되는 설정이나 로직을 중복해서 정의하지 않고 한 번에 관리할 수 있습니다.

    예를 들어, 여러 DAG에서 공통적으로 사용되는 설정이 있다면 이를 `x-airflow-common` 속성으로 정의하고, 각 DAG에서 이를 참조하여 사용할 수 있습니다.

    아래는 간단한 DAG 정의에서 `x-airflow-common`이 어떻게 사용될 수 있는지의 예시입니다:

    ```yaml
    # 예시 DAG 정의
    default_args:
      owner: 'airflow'
      start_date: days_ago(1)

    x-airflow-common:
      retries: 3
      retry_delay:
        minutes: 5

    tasks:
      - task_id: task1
        bash_command: 'echo "Hello, World!"'
    ```

    위의 예시에서 `x-airflow-common` 속성은 `retries` 및 `retry_delay`와 같은 일반적으로 여러 곳에서 사용되는 설정을 담고 있습니다. 이렇게 하면 `tasks` 내의 각 태스크에서 이 속성을 참조하여 사용할 수 있습니다.


  • airflow에서 x-airflow-common 의 실제예

     1) &airflow-common 아래 내용을 airflow-common으로 지정
     2) _PIP_ADDITIONAM_REQUIREMENTS : 설치해야할 파이썬 모듈 이름들 지정
     실행할때마다 모듈이 지워지는데 재설치할 수고가 적어짐, 모든 컨테이너에 모듈이 설치 돼서 모듈오류가 줄어듬
     3) {AIRFLOW_PROJ_DIR:-.} : airflow yaml 파일이 있는 폴더의 이름

모듈 추가방식

  • airflow-scheduler 서비스

     1) <<: *airflow-common : 아까 &airflow-common 로 지정했던것 사용
     2) airflow-init ~ : condition 충족하면 실행