๐Ÿ“’ Docker(6)

Kimdongkiยท2024๋…„ 6์›” 4์ผ

DB

๋ชฉ๋ก ๋ณด๊ธฐ
25/33

๐Ÿ“Œ Airflow API ํ™œ์„ฑํ™”

API

  • airflow.cfg์˜ api ์„น์…˜์—์„œ auth_backend์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ
[api]
auth_backend = airflow.api.auth.backend.basic_auth
  • docker-compose.yaml์—๋Š” ์ด๋ฏธ ์„ค์ •์ด ๋˜์–ด ์žˆ์Œ (environments)
    AIRFLOWAPIAUTH_BACKENDS: 'airflow.api.auth.backend.basic_auth,airflow.api.auth.backend.session'

  • ์•„๋ž˜ ๋ช…๋ น์œผ๋กœ ํ™•์ธํ•ด๋ณด๊ธฐ
    $ docker exec -it learn-airflow-airflow-scheduler-1 airflow config get-value api auth_backend
    airflow.api.auth.backend.basic_auth,airflow.api.auth.backend.session

  • Airflow Web UI์—์„œ ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ (API ์‚ฌ์šฉ์ž)

    • Security -> List Users -> +
    • ์ดํ›„ ํ™”๋ฉด์—์„œ ์ƒˆ ์‚ฌ์šฉ์ž ์ •๋ณด ์ถ”๊ฐ€ (monitor:MonitorUser1)

๐Ÿ“Œ Health API ํ˜ธ์ถœ

  • health API ํ˜ธ์ถœ
    curl -X GET --user "monitor:MonitorUser1" http://localhost:8080/health
  • ์ •์ƒ ๊ฒฝ์šฐ ์‘๋‹ต:
{
 "metadatabase": {
 "status": "healthy"
 },
 "scheduler": {
 "status": "healthy",
 "latest_scheduler_heartbeat": "2022-03-12T06:02:38.067178+00:00"
 }
}

๐Ÿ“Œ API ์‚ฌ์šฉ์˜ˆ

curl -X POST --user "airflow:airflow" -H 'Content-Type: application/json' -d
'{"execution_date":"2023-05-24T00:00:00Z"}'
"http://localhost:8080/api/v1/dags/HelloWorld/dagRuns"
{
 "detail": "DAGRun with DAG ID: 'HelloWorld' and DAGRun logical date: '2023-05-24 00:00:00+00:00'
already exists",
 "status": 409,
 "title": "Conflict",
 "type":
"https://airflow.apache.org/docs/apache-airflow/2.5.1/stable-rest-api-ref.html#section/Errors/AlreadyExists"
}
curl -X GET --user "airflow:airflow" http://localhost:8080/api/v1/dags
curl -X GET --user "airflow:airflow" http://localhost:8080/api/v1/variables
{
 "total_entries": 7,
 "variables": [
 {
 "description": null,
 "key": "api_token",
 "value": "12345667"
 },
 {
 "description": null,
 "key": "csv_url",
 "value": "https://s3-geospatial.s3-us-west-2.amazonaws.com/name_gender.csv"
 },
curl -X GET --user "airflow:airflow" http://localhost:8080/api/v1/config
{
 "detail": "Your Airflow administrator chose not to expose the configuration, most likely for security
reasons.",
 "status": 403,
 "title": "Forbidden",
 "type":
"https://airflow.apache.org/docs/apache-airflow/2.5.1/stable-rest-api-ref.html#section/Errors/PermissionDe
nied"
}

๐Ÿ“Œ API์™€ Config ํ™œ์šฉ

1. ํ™œ์„ฑํ™”๋œ Dags๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” Python ์ฝ”๋“œ

import requests

def get_active_dags():
    url = "http://localhost:8080/api/v1/dags"
    
    username, password = "airflow", "airflow"

    try:
        response = requests.get(url, auth=(username, password))
        response_json = response.json()

        for dag in response_json["dags"]:
            if dag["is_paused"] is False:
                print(f"Active DAG: {dag['dag_id']}")

    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")

get_active_dags()

2. config.cfg ํŒŒ์ผ์—์„œ๋Š” webserver ์„น์…˜์—์„œ expose_config๋ฅผ False -> True๋กœ ๋ฐ”๊พธ์–ด ์ฃผ๋ฉด ๋œ๋‹ค.

dockr-compose.yml ํŒŒ์ผ์—์„œ๋Š” x-airflow-common -> environment ์„น์…˜์—์„œ AIRFLOW_WEBSERVER_EXPOSE_CONFIG: 'true'๋กœ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

3. connections API์˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์ง€์ •๋œ ๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฐ€?

export
Connections successfully exported to connections.json.
import
[2024-06-04 09:46:03,316] {crypto.py:83} WARNING - empty cryptography key - values will not be stored encrypted.
Could not import connection aws_conn_id: connection already exists.
Could not import connection redshift_dev_db: connection already exists.

connections API๋„ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์ง€์ •๋œ ๊ฒƒ์€ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€