blob: fd6b564238c96dd3e632b1efdc0f00f95ef8ce46 [file] [log] [blame]
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kubernetes Executor &mdash; Airflow Documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<script src="_static/js/modernizr.min.js"></script>
<script type="application/javascript">
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga("create", "UA-140539454-1", "auto");
ga("send", "pageview");
</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> Airflow
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="project.html">Project</a></li>
<li class="toctree-l1"><a class="reference internal" href="license.html">License</a></li>
<li class="toctree-l1"><a class="reference internal" href="start.html">Quick Start</a></li>
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="howto/index.html">How-to Guides</a></li>
<li class="toctree-l1"><a class="reference internal" href="ui.html">UI / Screenshots</a></li>
<li class="toctree-l1"><a class="reference internal" href="concepts.html">Concepts</a></li>
<li class="toctree-l1"><a class="reference internal" href="profiling.html">Data Profiling</a></li>
<li class="toctree-l1"><a class="reference internal" href="cli.html">Command Line Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="scheduler.html">Scheduling &amp; Triggers</a></li>
<li class="toctree-l1"><a class="reference internal" href="plugins.html">Plugins</a></li>
<li class="toctree-l1"><a class="reference internal" href="security.html">Security</a></li>
<li class="toctree-l1"><a class="reference internal" href="timezone.html">Time zones</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">Experimental Rest API</a></li>
<li class="toctree-l1"><a class="reference internal" href="integration.html">Integration</a></li>
<li class="toctree-l1"><a class="reference internal" href="lineage.html">Lineage</a></li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="code.html">API Reference</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Airflow</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> &raquo;</li>
<li>Kubernetes Executor</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/kubernetes.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="kubernetes-executor">
<h1>Kubernetes Executor<a class="headerlink" href="#kubernetes-executor" title="Permalink to this headline"></a></h1>
<p>The kubernetes executor is introduced in Apache Airflow 1.10.0. The Kubernetes executor will create a new pod for every task instance.</p>
<p>Example helm charts are available at <cite>scripts/ci/kubernetes/kube/{airflow,volumes,postgres}.yaml</cite> in the source distribution. The volumes are optional and depend on your configuration. There are two volumes available:</p>
<ul class="simple">
<li>Dags: by storing all the dags onto the persistent disks, all the workers can read the dags from there. Another option is using git-sync, before starting the container, a git pull of the dags repository will be performed and used throughout the lifecycle of the pod.</li>
<li>Logs: by storing the logs onto a persistent disk, all the logs will be available for all the workers and the webserver itself. If you don’t configure this, the logs will be lost after the worker pods shuts down. Another option is to use S3/GCS/etc to store the logs.</li>
</ul>
</div>
<div class="section" id="kubernetes-operator">
<h1>Kubernetes Operator<a class="headerlink" href="#kubernetes-operator" title="Permalink to this headline"></a></h1>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">airflow.contrib.operators</span> <span class="k">import</span> <span class="n">KubernetesOperator</span>
<span class="kn">from</span> <span class="nn">airflow.contrib.operators.kubernetes_pod_operator</span> <span class="k">import</span> <span class="n">KubernetesPodOperator</span>
<span class="kn">from</span> <span class="nn">airflow.contrib.kubernetes.secret</span> <span class="k">import</span> <span class="n">Secret</span>
<span class="n">secret_file</span> <span class="o">=</span> <span class="n">Secret</span><span class="p">(</span><span class="s1">&#39;volume&#39;</span><span class="p">,</span> <span class="s1">&#39;/etc/sql_conn&#39;</span><span class="p">,</span> <span class="s1">&#39;airflow-secrets&#39;</span><span class="p">,</span> <span class="s1">&#39;sql_alchemy_conn&#39;</span><span class="p">)</span>
<span class="n">secret_env</span> <span class="o">=</span> <span class="n">Secret</span><span class="p">(</span><span class="s1">&#39;env&#39;</span><span class="p">,</span> <span class="s1">&#39;SQL_CONN&#39;</span><span class="p">,</span> <span class="s1">&#39;airflow-secrets&#39;</span><span class="p">,</span> <span class="s1">&#39;sql_alchemy_conn&#39;</span><span class="p">)</span>
<span class="n">volume_mount</span> <span class="o">=</span> <span class="n">VolumeMount</span><span class="p">(</span><span class="s1">&#39;test-volume&#39;</span><span class="p">,</span>
<span class="n">mount_path</span><span class="o">=</span><span class="s1">&#39;/root/mount_file&#39;</span><span class="p">,</span>
<span class="n">sub_path</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">read_only</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">volume_config</span><span class="o">=</span> <span class="p">{</span>
<span class="s1">&#39;persistentVolumeClaim&#39;</span><span class="p">:</span>
<span class="p">{</span>
<span class="s1">&#39;claimName&#39;</span><span class="p">:</span> <span class="s1">&#39;test-volume&#39;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">volume</span> <span class="o">=</span> <span class="n">Volume</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;test-volume&#39;</span><span class="p">,</span> <span class="n">configs</span><span class="o">=</span><span class="n">volume_config</span><span class="p">)</span>
<span class="n">affinity</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">&#39;nodeAffinity&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s1">&#39;preferredDuringSchedulingIgnoredDuringExecution&#39;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;weight&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="s2">&quot;preference&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;matchExpressions&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="s2">&quot;key&quot;</span><span class="p">:</span> <span class="s2">&quot;disktype&quot;</span><span class="p">,</span>
<span class="s2">&quot;operator&quot;</span><span class="p">:</span> <span class="s2">&quot;In&quot;</span><span class="p">,</span>
<span class="s2">&quot;values&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;ssd&quot;</span><span class="p">]</span>
<span class="p">]</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">},</span>
<span class="s2">&quot;podAffinity&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;requiredDuringSchedulingIgnoredDuringExecution&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;labelSelector&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;matchExpressions&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;key&quot;</span><span class="p">:</span> <span class="s2">&quot;security&quot;</span><span class="p">,</span>
<span class="s2">&quot;operator&quot;</span><span class="p">:</span> <span class="s2">&quot;In&quot;</span><span class="p">,</span>
<span class="s2">&quot;values&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;S1&quot;</span><span class="p">]</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">},</span>
<span class="s2">&quot;topologyKey&quot;</span><span class="p">:</span> <span class="s2">&quot;failure-domain.beta.kubernetes.io/zone&quot;</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">},</span>
<span class="s2">&quot;podAntiAffinity&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;requiredDuringSchedulingIgnoredDuringExecution&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;labelSelector&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;matchExpressions&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;key&quot;</span><span class="p">:</span> <span class="s2">&quot;security&quot;</span><span class="p">,</span>
<span class="s2">&quot;operator&quot;</span><span class="p">:</span> <span class="s2">&quot;In&quot;</span><span class="p">,</span>
<span class="s2">&quot;values&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;S2&quot;</span><span class="p">]</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">},</span>
<span class="s2">&quot;topologyKey&quot;</span><span class="p">:</span> <span class="s2">&quot;kubernetes.io/hostname&quot;</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">tolerations</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s1">&#39;key&#39;</span><span class="p">:</span> <span class="s2">&quot;key&quot;</span><span class="p">,</span>
<span class="s1">&#39;operator&#39;</span><span class="p">:</span> <span class="s1">&#39;Equal&#39;</span><span class="p">,</span>
<span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="s1">&#39;value&#39;</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="n">k</span> <span class="o">=</span> <span class="n">KubernetesPodOperator</span><span class="p">(</span><span class="n">namespace</span><span class="o">=</span><span class="s1">&#39;default&#39;</span><span class="p">,</span>
<span class="n">image</span><span class="o">=</span><span class="s2">&quot;ubuntu:16.04&quot;</span><span class="p">,</span>
<span class="n">cmds</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;bash&quot;</span><span class="p">,</span> <span class="s2">&quot;-cx&quot;</span><span class="p">],</span>
<span class="n">arguments</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;echo&quot;</span><span class="p">,</span> <span class="s2">&quot;10&quot;</span><span class="p">],</span>
<span class="n">labels</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;foo&quot;</span><span class="p">:</span> <span class="s2">&quot;bar&quot;</span><span class="p">},</span>
<span class="n">secrets</span><span class="o">=</span><span class="p">[</span><span class="n">secret_file</span><span class="p">,</span><span class="n">secret_env</span><span class="p">]</span>
<span class="n">volume</span><span class="o">=</span><span class="p">[</span><span class="n">volume</span><span class="p">],</span>
<span class="n">volume_mounts</span><span class="o">=</span><span class="p">[</span><span class="n">volume_mount</span><span class="p">]</span>
<span class="n">name</span><span class="o">=</span><span class="s2">&quot;test&quot;</span><span class="p">,</span>
<span class="n">task_id</span><span class="o">=</span><span class="s2">&quot;task&quot;</span><span class="p">,</span>
<span class="n">affinity</span><span class="o">=</span><span class="n">affinity</span><span class="p">,</span>
<span class="n">is_delete_operator_pod</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">hostnetwork</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="n">tolerations</span><span class="o">=</span><span class="n">tolerations</span>
<span class="p">)</span>
</pre></div>
</div>
<dl class="class">
<dt id="airflow.contrib.operators.kubernetes_pod_operator.KubernetesPodOperator">
<em class="property">class </em><code class="descclassname">airflow.contrib.operators.kubernetes_pod_operator.</code><code class="descname">KubernetesPodOperator</code><span class="sig-paren">(</span><em>namespace</em>, <em>image</em>, <em>name</em>, <em>cmds=None</em>, <em>arguments=None</em>, <em>volume_mounts=None</em>, <em>volumes=None</em>, <em>env_vars=None</em>, <em>secrets=None</em>, <em>in_cluster=False</em>, <em>cluster_context=None</em>, <em>labels=None</em>, <em>startup_timeout_seconds=120</em>, <em>get_logs=True</em>, <em>image_pull_policy='IfNotPresent'</em>, <em>annotations=None</em>, <em>resources=None</em>, <em>affinity=None</em>, <em>config_file=None</em>, <em>xcom_push=False</em>, <em>node_selectors=None</em>, <em>image_pull_secrets=None</em>, <em>service_account_name='default'</em>, <em>is_delete_operator_pod=False</em>, <em>hostnetwork=False</em>, <em>tolerations=None</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/airflow/contrib/operators/kubernetes_pod_operator.html#KubernetesPodOperator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#airflow.contrib.operators.kubernetes_pod_operator.KubernetesPodOperator" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <a class="reference internal" href="code.html#airflow.models.BaseOperator" title="airflow.models.BaseOperator"><code class="xref py py-class docutils literal notranslate"><span class="pre">airflow.models.BaseOperator</span></code></a></p>
<p>Execute a task in a Kubernetes Pod</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>image</strong> (<em>str</em>) – Docker image you wish to launch. Defaults to dockerhub.io,
but fully qualified URLS will point to custom repositories</li>
<li><strong>cmds</strong> (<em>list of str</em>) – entrypoint of the container. (templated)
The docker images’s entrypoint is used if this is not provide.</li>
<li><strong>arguments</strong> (<em>list of str</em>) – arguments of to the entrypoint. (templated)
The docker image’s CMD is used if this is not provided.</li>
<li><strong>volume_mounts</strong> (<em>list of VolumeMount</em>) – volumeMounts for launched pod</li>
<li><strong>volumes</strong> (<em>list of Volume</em>) – volumes for launched pod. Includes ConfigMaps and PersistentVolumes</li>
<li><strong>labels</strong> (<em>dict</em>) – labels to apply to the Pod</li>
<li><strong>startup_timeout_seconds</strong> (<em>int</em>) – timeout in seconds to startup the pod</li>
<li><strong>name</strong> (<em>str</em>) – name of the task you want to run,
will be used to generate a pod id</li>
<li><strong>env_vars</strong> (<em>dict</em>) – Environment variables initialized in the container. (templated)</li>
<li><strong>secrets</strong> (<em>list of Secret</em>) – Kubernetes secrets to inject in the container,
They can be exposed as environment vars or files in a volume.</li>
<li><strong>in_cluster</strong> (<em>bool</em>) – run kubernetes client with in_cluster configuration</li>
<li><strong>cluster_context</strong> (<em>string</em>) – context that points to kubernetes cluster.
Ignored when in_cluster is True. If None, current-context is used.</li>
<li><strong>get_logs</strong> (<em>bool</em>) – get the stdout of the container as logs of the tasks</li>
<li><strong>affinity</strong> (<em>dict</em>) – A dict containing a group of affinity scheduling rules</li>
<li><strong>node_selectors</strong> (<em>dict</em>) – A dict containing a group of scheduling rules</li>
<li><strong>config_file</strong> (<em>str</em>) – The path to the Kubernetes config file</li>
<li><strong>xcom_push</strong> (<em>bool</em>) – If xcom_push is True, the content of the file
/airflow/xcom/return.json in the container will also be pushed to an
XCom when the container completes.</li>
<li><strong>tolerations</strong> – Kubernetes tolerations</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Param:</th><td class="field-body"><p class="first">namespace: the namespace to run within kubernetes</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Type:</th><td class="field-body"><p class="first last">namespace: str</p>
</td>
</tr>
</tbody>
</table>
<p>:type list of tolerations</p>
<dl class="method">
<dt id="airflow.contrib.operators.kubernetes_pod_operator.KubernetesPodOperator.execute">
<code class="descname">execute</code><span class="sig-paren">(</span><em>context</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/airflow/contrib/operators/kubernetes_pod_operator.html#KubernetesPodOperator.execute"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#airflow.contrib.operators.kubernetes_pod_operator.KubernetesPodOperator.execute" title="Permalink to this definition"></a></dt>
<dd><p>This is the main method to derive when creating an operator.
Context is the same dictionary used as when rendering jinja templates.</p>
<p>Refer to get_template_context for more context.</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="airflow.contrib.kubernetes.secret.Secret">
<em class="property">class </em><code class="descclassname">airflow.contrib.kubernetes.secret.</code><code class="descname">Secret</code><span class="sig-paren">(</span><em>deploy_type</em>, <em>deploy_target</em>, <em>secret</em>, <em>key</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/airflow/contrib/kubernetes/secret.html#Secret"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#airflow.contrib.kubernetes.secret.Secret" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>Defines Kubernetes Secret Volume</p>
</dd></dl>
</div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>