LoginSignup
1
1

More than 3 years have passed since last update.

【Airflow on Kubernetes】DockerイメージのbuildとPodのdeployの仕組みについて

Last updated at Posted at 2019-06-27

概要

Airflowでは、Kubernetes用のDockerイメージの作成スクリプトと、Podのdeploy用のスクリプトが用意されている。
GitHUB: https://github.com/apache/airflow

これらのスクリプトが実際にどのような処理を行っているのかを調べた。

Version

目次

関連記事

おおまかな処理の流れ

処理の流れを大きく分けると、以下の2つに分けられる。

  1. Dockerイメージをbuildする
    1. scripts/ci/kubernetes/docker/build.sh
  2. Podをdeployをする
    1. scripts/ci/kubernetes/kube/deploy.sh

以降で、それぞれの詳細な処理について追っていく。

Dockerイメージをbuildする

Dockerイメージをbuildするための、build.shの処理内容を追う。

実際に実行させる場合は、以下の様にする。

$ sudo ./scripts/ci/kubernetes/docker/build.sh

build.shを実行してairflowのDockerイメージを作成する

build.sh#L45compile.shが実行される。

scripts/ci/kubernetes/docker/build.sh#L45
docker run -it --rm -v ${AIRFLOW_ROOT}:/airflow \
    -w /airflow ${PYTHON_DOCKER_IMAGE} ./scripts/ci/kubernetes/docker/compile.sh

build.shの中でcompile.shが実行される

compile.sh#L32でsetup.pyが実行されると、ホストOSに~/airflow/dist/というディレクトリが生成され、その下にapache-airflow-2.0.0.dev0.tar.gzというファイルが生成される。
これが、airflowのソースコードとなるものである。

scripts/ci/kubernetes/docker/compile.sh#L32
# apache-airflow-2.0.0.dev0.tar.gzが生成される
python setup.py compile_assets sdist -q

compile.shでairflowのtarファイルが作成されると、build.sh#L50で、先程生成されたapache-airflow-2.0.0.dev0.tar.gzが、scripts/ci/kubernetes/docker/airflow.tar.gzにリネームしてコピーされる。

scripts/ci/kubernetes/docker/build.sh#L50
cp $AIRFLOW_ROOT/dist/*.tar.gz ${DIRNAME}/airflow.tar.gz

build.shの中でairflowのDockerイメージをbuildする

build.sh#L51で、docker buildが実行される。

build.sh#L51
cd $DIRNAME && docker build --pull $DIRNAME --tag=${IMAGE}:${TAG}

このときに、このDockerfileが読み込まれる。

Dockerfileの処理

Dockerfile#L45で、先程生成されたホストOSのscripts/ci/kubernetes/docker/airflow.tar.gzがコンテナ内にCOPYされる。

Dockerfile#L45
# コンテナ内にCOPY
COPY airflow.tar.gz /tmp/airflow.tar.gz

Dockerfileの中でairflow-test-env-init.shが実行される

次にDockerfile#L48で、airflow-test-env-init.shが実行される。

Dockerfile#L48
COPY airflow-test-env-init.sh /tmp/airflow-test-env-init.sh

すると、airflow-test-env-init.sh#L23で、コンテナ内の/usr/local/lib/python3.6/site-packages/airflow/example_dags/を、PersistentVolumeのmount先である/root/airflow/dags/にコピーする。

airflow-test-env-init.sh#L23
# example_dags/とcontrib/example_dags/をPersistentVolumeにコピー
cd /usr/local/lib/python3.6/site-packages/airflow && \
cp -R example_dags/* /root/airflow/dags/ && \
cp -R contrib/example_dags/example_kubernetes_*.py /root/airflow/dags/ && \
cp -a contrib/example_dags/libs /root/airflow/dags/ && \

build.shの処理はこれで完了。

Deploy

Podをdeployするための、deploy.shの処理内容を追う。

実際に実行させる場合は、以下の様にする。
-dオプションで、dags_folderの追加方法をpersistent_modegit_modeのどちらかから選ぶ。

  usage: ./scripts/ci/kubernetes/kube/deploy.sh options
  OPTIONS:
    -d Use PersistentVolume or GitSync for dags_folder. Available options are "persistent_mode" or "git_mode"

実行

$ sudo ./scripts/ci/kubernetes/kube/deploy.sh -d {persistent_mode,git_mode}

deploy.shを実行

deploy.shを実行して、Podをdeployする。

実行されると、scripts/ci/kubernetes/kube/templates以下のmanifestのテンプレートファイルの必要箇所が置換されて、scripts/ci/kubernetes/kube/buildの下に生成される。
生成されたmanifestファイルを使ってkubectl applyが実行される。

$ ls -1 scripts/ci/kubernetes/kube/templates/
airflow.template.yaml
configmaps.template.yaml
init_git_sync.template.yaml

InitContainer

ci/kubernetes/kube/templates/airflow.template.yamlを元にして生成された、scripts/ci/kubernetes/kube/build/airflow.yamlをapplyすると、airflow.yaml#L44で定義されているInitContainerが実行され、deployの際にInitContainerでもairflow-test-env-init.shが実行される。

ここで、コンテナ内の/usr/local/lib/python3.6/site-packages/airflow/example_dags/が、PodのPersistentVolumeのmount先である/root/airflow/dags/にコピーされる。

airflow.template.yaml#L44

airflow.template.yaml#一部抜粋
    spec:
      initContainers:
      - name: "init"
        image: {{AIRFLOW_IMAGE}}:{{AIRFLOW_TAG}}
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: airflow-configmap
          mountPath: /root/airflow/airflow.cfg
          subPath: airflow.cfg
        - name: {{INIT_DAGS_VOLUME_NAME}}
          mountPath: /root/airflow/dags
        - name: test-volume
          mountPath: /root/test_volume
        env:
        - name: SQL_ALCHEMY_CONN
          valueFrom:
            secretKeyRef:
              name: airflow-secrets
              key: sql_alchemy_conn
        command:
          - "bash"
        args:
          - "-cx"
          - "./tmp/airflow-test-env-init.sh"

deploy.shの処理はこれで完了。

dagsの確認

Pod内を確認。/root/airflow/dags/だけでなく、/usr/local/lib/python3.6/site-packages/airflow/example_dags/にもファイルがあることがわかる。

Pod内に入って確認してみる。

$ sudo kubectl exec -it airflow-xxxxxxxxxx-xxxxx /bin/bash

Pod内の/usr/local/lib/python3.6/site-packages/airflow/example_dags/を確認。

root@airflow-xxxxxxxxxx-xxxxx:/# ls -1 /usr/local/lib/python3.6/site-packages/airflow/example_dags/
__init__.py
__pycache__
docker_copy_data.py
example_bash_operator.py
example_branch_operator.py
example_branch_python_dop_operator_3.py
example_docker_operator.py
example_http_operator.py
example_latest_only.py
example_latest_only_with_trigger.py
example_passing_params_via_test_command.py
example_pig_operator.py
example_python_operator.py
example_short_circuit_operator.py
example_skip_dag.py
example_subdag_operator.py
example_trigger_controller_dag.py
example_trigger_target_dag.py
example_xcom.py
subdags
test_utils.py
tutorial.py

Pod内の/root/airflow/dags/以下を確認。

root@airflow-xxxxxxxxxx-xxxxx:/# ls -1 /root/airflow/dags/
__init__.py
__pycache__
docker_copy_data.py
example_bash_operator.py
example_branch_operator.py
example_branch_python_dop_operator_3.py
example_docker_operator.py
example_http_operator.py
example_kubernetes_executor.py
example_kubernetes_executor_config.py
example_kubernetes_operator.py
example_latest_only.py
example_latest_only_with_trigger.py
example_passing_params_via_test_command.py
example_pig_operator.py
example_python_operator.py
example_short_circuit_operator.py
example_skip_dag.py
example_subdag_operator.py
example_trigger_controller_dag.py
example_trigger_target_dag.py
example_xcom.py
libs
subdags
test_utils.py
tutorial.py

どちらにもdagファイルがあるが、/root/airflow/dags/のほうがファイルが多い。
これはairflow-test-env-init.shを見ると分かるが、example_dags/だけでなく、contrib/example_dags/以下のファイルもコピーされているためである。

airflow-test-env-init.sh#L23
cp -R example_dags/* /root/airflow/dags/ && \
cp -R contrib/example_dags/example_kubernetes_*.py /root/airflow/dags/ && \
cp -a contrib/example_dags/libs /root/airflow/dags/ && \

Podのlogを確認する方法

Pod名とコンテナ名を指定する。

$ sudo kubectl logs -f pod/airflow--xxxxxxxxxx-xxxxx -c {webserver,scheduler}

Hackする

同期されるdagファイルを変更する場合

airflow-test-env-init.sh#L23
の以下の箇所を変更することで、同期されるdagファイルを変更することができる。

airflow-test-env-init.sh#L23
cp -R example_dags/* /root/airflow/dags/ && \
cp -R contrib/example_dags/example_kubernetes_*.py /root/airflow/dags/ && \
cp -a contrib/example_dags/libs /root/airflow/dags/ && \

PersistentVolumeのmount先を変更する方法

以下2つのvolumeの定義を変更する。

  • airflowのファイル群のmount先
    • scripts/ci/kubernetes/kube/volumes.yaml
  • postgresのデータのmount先
    • scripts/ci/kubernetes/kube/postgres.yaml

メタデータDBをデフォルトのPostgreSQLからMySQLに変更する

【Airflow on Kubernetes】メタデータDBをデフォルトのPostgreSQLからMySQLに変更する

ログの確認方法

deploy時に問題が起きた際は、以下のように各種コンテナのログ確認する。

$ sudo kubectl logs -f airflow-xxxxxxxxxx-xxxxx init
$ sudo kubectl logs -f airflow-xxxxxxxxxx-xxxxx webserver
$ sudo kubectl logs -f airflow-xxxxxxxxxx-xxxxx scheduler

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1