Python scripts executes Kubernetes command: kubectl apply -f config_file.yaml

7/26/2018

I have generated a python script that opens a deployment config_file.yaml, modifies some parameters and saves it again, using pyyaml. This python script will be executed in the master node of a Kubernetes cluster.

Once is generated the new file, my intention is to execute

kubectl apply -f config_file.yaml

in the python script to apply the modifications to the deployment.

I have been reading how to do it using kubernetes python client, but it seems it is not prepare to execute kubectl apply.

So the other option is to create a bash script and execute it from python script.

Bash scripts:

#!/bin/bash
sudo kubectl apply -f config_file.yaml

I give it permissions chmod +x shell_scipt.sh

Python script:

import subprocess
subprocess.call(['./shell_script.sh'])

But an error appears: File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child raise child_exception OSError: [Errno 13] Permission denied

I don't know how to resolve this error, I have tested givin permissions to the bash script, but nothing worked.

-- user3278790
bash
kubectl
kubernetes
python

2 Answers

9/6/2018

I do not know anything about Kubernetes but I think I might help.

I am basically suggesting that you run the command directly from Python script, not having Python running a bash script which runs a command.

import os

command = 'kubectl apply -f config_file.yaml'
password = 'yourpassword'
p = os.system('echo %s|sudo -S %s' % (passs, command))
-- Teoman YĆ¼ksel
Source: StackOverflow

9/6/2018

If I understand correctly you are using python to dynamically modify static yaml files. If this is the case I would recommend using helm which if perfect for making static yaml file dynamic :-)

-- pb100
Source: StackOverflow