<p>Kubernetes has a very nice official Python API client. The API client assumes that you will be creating individual resources (such as pods, or services) and assumes that you will be using Python objects to compose and create API requests. </p>

<p>However, I'd like to run arbitrary kubernetes YAML files (containing one or more k8s resources) via a Python interface. I was wondering if the Python kubernetes client can be leveraged to apply arbitrary YAML files? </p>

<p>I'm basically looking for the Python equivalent of:</p>

<pre><code>kubectl apply -f some-file-containing-multiple-resources.yaml
</code></pre>

<p>I'm looking for something where I can basically load the kubeconfig and apply the yaml via Python in a fairly Pythonic way.</p>

<p>I know I can probably wrap the kubectl command with a Python subprocess call, but I was hoping for something more Pythonic than that and hoped that the core K8s Python client could do something like that. Or, if there is another Python package that does something similar. </p>

<p>Can the Python kubernetes client call arbitrary k8s yaml files and if not, is there something that can? </p>

<p>Thanks for reading - I appreciate any advice you have to offer. </p>

Joe J
<p>You can try using <code>create_from_yaml</code> provided by kubernetes.utils in the following way.
This is the multi-resource-definition file</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
    - name: ngnx-container
      image: nginx:latest
      ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
   - port: 8080
     targetPort: 80
</code></pre>

<p>Now you can try running the below code and check whether that works out for you or not.</p>

<pre><code>from kubernetes import client, config, utils
config.load_kube_config()
k8s_client = client.ApiClient()
yaml_file = '&lt;location to your multi-resource file&gt;'
utils.create_from_yaml(k8s_client, yaml_file)
</code></pre>
