<p>I'm new to Prefect and would like to deploy a Kubernetes Job at an Azure Kubernetes Service using Prefect.
I've set up a Linux Container including Prefect, kubectl. I could establish a connection to the AKS cluster and by using kubectl, I could deploy a Kubernetes Job at the AKS.
But how does it work with Prefect?
At the Container, I've stored the .py file and a YAML file, which defines the Kubernetes Job.</p>
<p><a href="https://i.stack.imgur.com/TqwIR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TqwIR.png" alt="enter image description here" /></a></p>
<p>--&gt; kubectl apply -f deploytestcontainer.yaml --&gt; works</p>
<p>Running attached example Prefect code also works (but the Job is not getting deployed at the AKS)</p>
<p><a href="https://i.stack.imgur.com/GSAB3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GSAB3.png" alt="enter image description here" /></a></p>
<p>That's the content of &quot;firstk8sjob.py&quot;</p>
<pre><code>import prefect
from prefect import task, Flow
from prefect.run_configs import KubernetesRun
 
@task
def hello_task():
    flow.run_config = KubernetesRun(job_template_path=&quot;deploytestcontainer.yaml&quot;)
    logger = prefect.context.get(&quot;logger&quot;)
    logger.info(&quot;Hello world!&quot;)
 
with Flow(&quot;hello-flow&quot;) as flow:
    hello_task()
 
flow.run()
</code></pre>
<p>Thank you a lot in advance for your advices!</p>

bianconero
<p>There are two steps to deploy your flow to Azure AKS:</p>
<ol>
<li>Deploy your Kubernetes agent to this cluster</li>
<li>Register your flow</li>
</ol>
<p>First, you need to deploy a Kubernetes agent. You can generate a manifest file using:</p>
<pre><code>prefect agent kubernetes install --rbac --key YOUR_API_KEY --label YOUR_LABEL &gt; agent.yaml
</code></pre>
<p>Then you can check the file, and modify it as needed (e.g. change image version to match your desired Python and Prefect version, add environment variables when needed, etc).</p>
<p>Note that the API key is for Prefect Cloud - are you a Prefect Cloud or Prefect Server user? If you're on Prefect Server, you need additionally <a href="https://discourse.prefect.io/t/how-to-set-server-endpoint-when-deploying-a-kubernetes-agent/181" rel="nofollow noreferrer">those env variables</a>.</p>
<p>Once your manifest is ready, you can apply it to your AKS cluster:</p>
<pre><code>kubectl apply -f agent.yaml # optionally set: -n yournamespace 
</code></pre>
<p>Then, once the agent is running, you can deploy your flow simply by specifying the label on your <code>KubernetesRun</code>.</p>
<p>Note that you were setting the run config within a task which is not recommended and may be the cause of your issue. You should attach the run config to your flow object as follows:</p>
<pre class="lang-py prettyprint-override"><code>import prefect
from prefect import task, Flow
from prefect.run_configs import KubernetesRun


@task
def hello_task():
    logger = prefect.context.get(&quot;logger&quot;)
    logger.info(&quot;Hello world!&quot;)


with Flow(&quot;hello-flow&quot;, run_config=KubernetesRun(labels=[&quot;YOUR_LABEL&quot;])) as flow:
    hello_task()

if __name__ == &quot;__main__&quot;:
    flow.register(&quot;YOUR_PROJECT_NAME&quot;)
</code></pre>
<p>You can also register within your CLI and get rid of this <code>&quot;__main__&quot;</code> block:</p>
<pre><code>prefect register --project YOUR_PROJECT_NAME -p path/to/flow.py
</code></pre>
<p>Once the flow is registered, you can trigger a run on AKS using:</p>
<pre><code>prefect run --name &quot;hello-flow&quot; --project YOUR_PROJECT_NAME --watch
</code></pre>
