k8soper_1.py 1.8 KB
Newer Older
one_jh's avatar
one_jh committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from datetime import datetime, timedelta

from kubernetes.client import models as k8s
from airflow.models import DAG, Variable
from airflow.operators.dummy_operator import DummyOperator
from airflow.kubernetes.secret import Secret
from airflow.kubernetes.pod import Resources
from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import (
    KubernetesPodOperator,
)

dag_id = 'kubernetes-dag'

task_default_args = {
    'owner': 'jhwon',
    'retries': 3,
    'retry_delay': timedelta(minutes=5),
    'start_date': datetime(2022, 5, 11),
    'depends_on_past': False,
    'email': ['jh_won@lotte.net'],
    'email_on_retry': False,
one_jh's avatar
one_jh committed
22
    'email_on_failure': False,
one_jh's avatar
one_jh committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    'execution_timeout': timedelta(hours=1)
}

dag = DAG(
    dag_id=dag_id,
    description='kubernetes pod operator',
    default_args=task_default_args,
    schedule_interval='5 16 * * *',
    max_active_runs=1
)

env = Secret(
    'env',
    'TEST',
one_jh's avatar
one_jh committed
37
    'test-env',
one_jh's avatar
one_jh committed
38
39
40
41
42
43
44
45
46
47
48
    'TEST',
)

pod_resources = Resources()
pod_resources.request_cpu = '1'
pod_resources.request_memory = '200m'
pod_resources.limit_cpu = '2'
pod_resources.limit_memory = '400m'


configmaps = [
one_jh's avatar
one_jh committed
49
    k8s.V1EnvFromSource(config_map_ref=k8s.V1ConfigMapEnvSource(name='airflow-airflow-config')),
one_jh's avatar
one_jh committed
50
51
52
53
54
55
]

start = DummyOperator(task_id='start', dag=dag)

run = KubernetesPodOperator(
    task_id='kubernetespodoperator',
one_jh's avatar
one_jh committed
56
57
    namespace='airflow',
    # image='curlimages/curl@sha256:c05146450820bb943c400bf2173a2423096c586a3984b989619d9165d61f1a24',
one_jh's avatar
one_jh committed
58
    image='curlimages/curl:latest',
one_jh's avatar
one_jh committed
59
    cmds=["curl", "-v","http://localhost:30042"],
one_jh's avatar
one_jh committed
60

one_jh's avatar
one_jh committed
61
62
63
    # secrets=[
    #     env
    # ],
one_jh's avatar
one_jh committed
64
    image_pull_secrets=[k8s.V1LocalObjectReference('image_credential')],
one_jh's avatar
one_jh committed
65
    name='curl-job',
one_jh's avatar
one_jh committed
66
    is_delete_operator_pod=False,
one_jh's avatar
one_jh committed
67
    get_logs=True,
one_jh's avatar
one_jh committed
68
    # resources=pod_resources,
one_jh's avatar
one_jh committed
69
70
71
72
73
    env_from=configmaps,
    dag=dag,
)

start >> run