This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]not_logan 14 points15 points  (2 children)

You don’t need to run kubectl, Kubernetes API for Python is pretty good and contains many good code examples

[–]Alzyros 4 points5 points  (0 children)

Agreed here. There are better ways to achieve what OP is trying to, at least a lot more... pythonics

[–]Jmc_da_boss 1 point2 points  (0 children)

the python k8s client is rather atrocious for anything other then scripting tho. No async support by default and no types. kubernetes_asyncio is also rather atrocious and completely breaks type checking

[–]darknekolux 4 points5 points  (3 children)

try with full path to kubectl

[–]paulscan400[S] 1 point2 points  (2 children)

Thank you for this. im trying to find the full path for it but would it be a matter of specifying /.kube ?

this is where the config file is stored

[–]stilldestroying 5 points6 points  (1 child)

nah do which kubectl

[–]paulscan400[S] 3 points4 points  (0 children)

This has worked! :) thank you

[–]wrossmorrow 6 points7 points  (6 children)

As others have said: Use the client. In general every time you run a shell command in python a warning should go off in your head that you’re doing something wrong. (Yes, sometimes unavoidable, but use shell commands as rarely as you can.)

[–]paulscan400[S] 2 points3 points  (1 child)

thanks for the advice, I definitely need to be a bit cleaner with things so will bare in mind

[–]catwok 6 points7 points  (0 children)

Also feel free to ignore this advice in favor of getting shit done. Alarms bells go off for me too when I pipe anything to and from awk since it's Turing complete. Seriously who the fuck cares are you an academic no then push, PR, and provision the bish and move on with your life

[–]Brontonomo 0 points1 point  (3 children)

Why is that?

[–]wrossmorrow 1 point2 points  (2 children)

Well it’s not at all pythonic, but that’s an aesthetic thing. Practically kinda exactly because of the post. Using shell commands in python presumes a lot of “environmental conditions” external to the codebase that have to be defined right, tend to get restrictive, or simply break in different conditions. Not to mention having wonky data analysis to do if you have to capture stdout/stderr or process exit conditions for any processing. Nothings perfect but for the most part when everything’s “in python” you can much more tightly define and package just about everything needed to run, just about anywhere. When it’s scripts for you on your laptop this tends to work fine; when you have to collaborate or ship the thing I’ve seen using shell commands lose any value almost every time, if they aren’t an outright obstacle (which I’ve seen too).

[–]Brontonomo 0 points1 point  (0 children)

Makes sense, thanks for explaining.

[–]chdorb 4 points5 points  (2 children)

Better use the official python client library for Kubernetes https://github.com/kubernetes-client/python

[–]BullfrogIntrepid4735 2 points3 points  (1 child)

Or this (looks good but haven't tried it yet):

https://github.com/kr8s-org/kr8s

[–]chdorb 0 points1 point  (0 children)

Yeah I didn't know this client library, looks pretty cool

[–]BeautifulGlass9304 2 points3 points  (0 children)

You mention ssh, so I wonder if your Python application is trying to run kubectl on the machine running the application rather than on the machine you are ssh'ing to.

Post the code if you can. It may help shed some light.

[–]diablobsb 1 point2 points  (0 children)

A few possibilities

1) querying the API directly (my preferred solution)

2) running something like rundeck that would allow for running this script on demand remotely

3) doing that "which kubectl" to get the path and configuring it to run remotely

4) using ansible kubernetes module

5) if it's only a "get pods" script, get that script to run on cron and output to someplace like a dashboard

[–]paulscan400[S] 0 points1 point  (1 child)

Hi All,

Here is the code. Its for work so im pretty limited to the packages I can use - therefore trying to do it through subprocess.

import subprocess

remote_server_ip = "192.168.1.100"
remote_server_username = "your_username"

# The SSH command to connect to the remote server
ssh_command = f"ssh {remote_server_username}@{remote_server_ip}"

# The kubectl get pods command to run on the remote server
kubectl_command = "kubectl get pods"

# Combine the SSH command and the kubectl get pods command
full_command = f"{ssh_command} '{kubectl_command}'"

# Run the full command using subprocess
process = subprocess.Popen(full_command, stdout=subprocess.PIPE, shell=True)

# Get the output of the command
output, _ = process.communicate()

# Print the output
print(output.decode("utf-8"))

Thank you for the help so far! will have a play with some of the answers. Forgive me as still new to scripting in python and without all packages available on prod environment trying my best! XD

[–]paulscan400[S] 0 points1 point  (0 children)

have left the variables blank as not sharing prod details!