blob: 789391d9b653421eabbcfbb67c74bba6d479c409 [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 &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" />
<link rel="next" title="Lineage" href="lineage.html" />
<link rel="prev" title="Metrics" href="metrics.html" />
<script src="_static/js/modernizr.min.js"></script>
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
_paq.push(['disableCookies']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '13']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo -->
<link rel="canonical" href="https://airflow.apache.org/docs/apache-airflow/stable/kubernetes.html" />
</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 class="version">
1.10.2
</div>
<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 class="current">
<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="metrics.html">Metrics</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Kubernetes</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#kubernetes-executor">Kubernetes Executor</a></li>
<li class="toctree-l2"><a class="reference internal" href="#kubernetes-operator">Kubernetes Operator</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="lineage.html">Lineage</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</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</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">
<h1>Kubernetes<a class="headerlink" href="#kubernetes" title="Permalink to this headline"></a></h1>
<div class="section" id="kubernetes-executor">
<h2>Kubernetes Executor<a class="headerlink" href="#kubernetes-executor" title="Permalink to this headline"></a></h2>
<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">
<h2>Kubernetes Operator<a class="headerlink" href="#kubernetes-operator" title="Permalink to this headline"></a></h2>
<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>**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>image_pull_policy</strong> (<em>str</em>) – Specify a policy to cache or always pull an image</li>
<li><strong>image_pull_secrets</strong> (<em>str</em>) – Any image pull secrets to be given to the pod.
If more than one secret is required, provide a
comma separated list: secret_a,secret_b</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>hostnetwork</strong> (<em>bool</em>) – If True enable host networking on the pod</li>
<li><strong>tolerations</strong> (<em>list tolerations</em>) – A list of 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>
</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>Defines Kubernetes Secret Volume</p>
</dd></dl>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="lineage.html" class="btn btn-neutral float-right" title="Lineage" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="metrics.html" class="btn btn-neutral" title="Metrics" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<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/language_data.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>