4

I am trying to execute the kubectl command using python script but keep getting error. I have requirement to execute the kubectl command to create pod and check the pod log for any failure.

What am I doing wrong here?

import subprocess

command = 'kubectl apply -f deployment.yaml'


check_output= subprocess.check_output(command)
print(check_output)


error

Traceback (most recent call last):
  File "/usr/bin/cma-scripts/kubectl.py", line 6, in <module>
    check_output= subprocess.check_output(command)
  File "/usr/local/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/local/lib/python3.9/subprocess.py", line 505, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/local/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.9/subprocess.py", line 1821, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'kubectl apply -f deployment.yaml'

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
user1591156
  • 1,945
  • 4
  • 18
  • 31
  • 4
    It probably thinks you're not trying to invoke `kubectl` with `apply`, `-f` etc. as arguments, but rather invoke the non-existent command `kubectl apply -f ...` with no arguments. Try running it with `shell=True`, or split the command into a list (`["kubectl", "apply", "-f"...]`) – koorkevani Oct 04 '21 at 19:27
  • @koorkevani consider putting your comment as answer. – P.... Oct 04 '21 at 21:16
  • Can you use a Kubernetes SDK here, instead of trying to invoke `kubectl`? Do you need to dynamically invoke the `kubectl exec` debugging tool, or does your application have an HTTP endpoint you can connect to instead? (Remember that `kubectl exec` will only target a single pod of a replicated deployment, and any changes you make there will be lost when the pod gets deleted.) – David Maze Oct 04 '21 at 21:50

1 Answers1

7

You can execute kubectl commands with Python, but you can also use the Python client for the Kubernetes API.

Below I will give examples for both options.

Executing kubectl commands with Python.

You can use the subprocess module:

$ cat script-1.py
#!/usr/bin/python3.7

import subprocess
subprocess.run(["kubectl", "apply", "-f", "deployment.yaml"])


$ ./script-1.py
deployment.apps/web-app created

You can also use the os module:

$ cat script-1.py
#!/usr/bin/python3.7

import os
os.system("kubectl  apply -f deployment.yaml")

$ ./script-1.py
deployment.apps/web-app created

Using the Python client for the kubernetes API.

As previously mentioned, you can also use a Python client to create a Deployment.

Based on the deployment_create.py example, I've created a script to deploy deployment.yaml in the default Namespace:

$ cat script-2.py
#!/usr/bin/python3.7

from os import path

import yaml

from kubernetes import client, config


def main():
    config.load_kube_config()

    with open(path.join(path.dirname(__file__), "deployment-1.yaml")) as f:
        dep = yaml.safe_load(f)
        k8s_apps_v1 = client.AppsV1Api()
        resp = k8s_apps_v1.create_namespaced_deployment(
            body=dep, namespace="default")
        print("Deployment created. status='%s'" % resp.metadata.name)


if __name__ == '__main__':
    main()

$ ./script-2.py
Deployment created. status='web-app'

$ kubectl get deployment
NAME      READY   UP-TO-DATE   AVAILABLE   
web-app   1/1     1            1   

    
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
matt_j
  • 4,010
  • 1
  • 9
  • 23