Kubectl command throws error when executed from python script but manual execution works fine

40 Views Asked by At

I'm trying to execute kubectl commands in python

process = subprocess.run(["kubectl", "get", "pods", "-n", "argocd"])

which throws error:

__main__ - INFO - 
E0328 08:18:04.126243   25976 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E0328 08:18:04.126801   25976 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E0328 08:18:04.129854   25976 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E0328 08:18:04.130344   25976 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E0328 08:18:04.131804   25976 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
The connection to the server localhost:8080 was refused - did you specify the right host or port?
__main__ - ERROR - Failed to get po.
NoneType: None

But when I execute the same command manually from the master node I see output without any issues.

Can someone help me figure out what's the issue here?

1

There are 1 best solutions below

0
smadi0x86 On

It seems like this issue is related to authentication with the KubeAPI server. To troubleshoot and resolve this problem follow these steps:

Check Kubernetes API Server Configuration:

Ensure that your Python environment has access to the Kubernetes configuration file (~/.kube/config by default). This file contains the credentials and configuration details required to access the Kubernetes cluster.

Verify Correct Context:

Check if the Kubernetes configuration file specifies the correct context with the necessary permissions to list pods in the argocd namespace.

You can do this by running:

kubectl config current-context

Test API Server Reachability:

Verify that the Kubernetes API server is reachable from your Python environment using:

curl http://localhost:8080/api

Replace localhost:8080 with the actual API server address if different, you can check your api server port by running cat /etc/kubernetes/manifests/kube-apiserver.yaml

Add Authentication:

If authentication is required by your Kubernetes API server and is not configured in your Python environment, set up authentication. This can involve using service account tokens, kubeconfig files with credentials, or other authentication methods based on your Kubernetes cluster setup.

Debugging Output:

Include more verbose output in your Python script to capture any error messages or details that might help diagnose the authentication issue.

Example:

import subprocess

try:
    result = subprocess.run(["kubectl", "get", "pods", "-n", "argocd"], capture_output=True, text=True, check=True)
    print("Output:", result.stdout)
except subprocess.CalledProcessError as e:
    print("Error:", e)
    print("Error Output:", e.stderr)