Python: Can't delete pods using Kubernetes API

Created on 26 Jun 2017  路  15Comments  路  Source: kubernetes-client/python

I am using the python client library for the API. I am using the following code :

def delete_pod(name, namespace):
    api_instance = client.CoreV1Api()
    body = client.V1DeleteOptions()
    api_response = api_instance.delete_namespaced_pod(name, namespace, body)
    return api_response
print delete_pod('kargo-3','default')

But I get the following error :

2017-06-26 17:32:19,852 WARNING Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f2e0ce059d0>: Failed to establish a new connection: [Errno 111] Connection refused',)': /api/v1/namespaces/default/pods/kargo-3
2017-06-26 17:32:19,853 WARNING Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f2e0ce05b50>: Failed to establish a new connection: [Errno 111] Connection refused',)': /api/v1/namespaces/default/pods/kargo-3
2017-06-26 17:32:19,853 WARNING Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f2e0ce05c90>: Failed to establish a new connection: [Errno 111] Connection refused',)': /api/v1/namespaces/default/pods/kargo-3
Traceback (most recent call last):
  File "utility_pods.py", line 41, in <module>
    print delete_pod('kargo-3','default')
  File "utility_pods.py", line 37, in delete_pod
    api_response = api_instance.delete_namespaced_pod(name, namespace, body)
  File "/home/piyush/.local/lib/python2.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 9726, in delete_namespaced_pod
    (data) = self.delete_namespaced_pod_with_http_info(name, namespace, body, **kwargs)
  File "/home/piyush/.local/lib/python2.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 9831, in delete_namespaced_pod_with_http_info
    collection_formats=collection_formats)
  File "/home/piyush/.local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 329, in call_api
    _return_http_data_only, collection_formats, _preload_content, _request_timeout)
  File "/home/piyush/.local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 153, in __call_api
    _request_timeout=_request_timeout)
  File "/home/piyush/.local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 406, in request
    body=body)
  File "/home/piyush/.local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 265, in DELETE
    body=body)
  File "/home/piyush/.local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 175, in request
    headers=headers)
  File "/home/piyush/.local/lib/python2.7/site-packages/urllib3/request.py", line 66, in request
    **urlopen_kw)
  File "/home/piyush/.local/lib/python2.7/site-packages/urllib3/request.py", line 87, in request_encode_url
    return self.urlopen(method, url, **extra_kw)
  File "/home/piyush/.local/lib/python2.7/site-packages/urllib3/poolmanager.py", line 321, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/home/piyush/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 678, in urlopen
    **response_kw)
  File "/home/piyush/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 678, in urlopen
    **response_kw)
  File "/home/piyush/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 678, in urlopen
    **response_kw)
  File "/home/piyush/.local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 649, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/piyush/.local/lib/python2.7/site-packages/urllib3/util/retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='localhost', port=443): Max retries exceeded with url: /api/v1/namespaces/default/pods/kargo-3 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f2e0ce0bd90>: Failed to establish a new connection: [Errno 111] Connection refused',))

At first I thought this was because I wasn't providing
kubernetes.client.configuration.api_key['authorization'] = 'YOUR_API_KEY'

But then I am able to use the APIs to get pod list node list, etc. They should also require authorization. Any clue?

lifecyclrotten

Most helpful comment

Good afternoon. I'm new to Python and I've been working on a python script to remove a particular pod using the Kubernetes API, but it is not working. I was wondering if someone could take a look at my script to see what the problem could be. Currently I'm getting a SyntaxError: unexpected EOF while parsing, which means the code is processing all the way through. Below is my script:

from kubernetes import client, config

config.load_kube_config()

def delete_pod(self,name,namespace,body):
core_v1 = client.CoreV1Api()
delete_options = client.V1DeleteOptions()
try:
api_response = core_v1.delete_namespaced_pod(
name='Stang-hi',
namespace='default',
body=delete_options)
print(api_response)

I suspect you have a Python syntax problem in your script unrelated to the kubernetes package, but I'm not sure because the script you've posted isn't formatted correctly. I have edited your script slightly and it seems to work for me.

from kubernetes import client, config

config.load_kube_config()


def delete_pod(name, namespace):
    core_v1 = client.CoreV1Api()
    delete_options = client.V1DeleteOptions()
    api_response = core_v1.delete_namespaced_pod(
        name=name,
        namespace=namespace,
        body=delete_options)
    print(api_response)


if __name__ == '__main__':
    delete_pod(name='Stang-hi', namespace='default')

All 15 comments

For more info here is my kube-config file :

apiVersion: v1
clusters:
- cluster:
    certificate-authority: /home/piyush/.minikube/ca.crt
    server: https://192.168.99.100:8443
  name: minikube
- cluster:
    server: http://192.168.237.115:8080
  name: test
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
- context:
    cluster: test
    user: minikube
  name: test
current-context: test
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /home/piyush/.minikube/apiserver.crt
    client-key: /home/piyush/.minikube/apiserver.key

My current-context is : test

have you tried loading config using config.load_kube_config? See examples.

I am having the similar issue. I have called config.load_kube_config. I am just using the sample code in README.

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

This is my config file.

apiVersion: v1
clusters:
- cluster:
    certificate-authority: /Users/slin/.minikube/ca.crt
    server: https://192.168.99.100:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /Users/slin/.minikube/client.crt
    client-key: /Users/slin/.minikube/client.key

@linshu do you get the same error?

Also, can you both try kubectl and make sure it is working with the same config?

if this didn't fix it, please report a system snapshot here (like all python packages and their version, platform, python version, etc.)

I got a similar error and was able to address this by using the following code to explicitly set api_client.

Here's the code that works. This works when running inside a Docker container on my local machine talking to a remote k8s cluster.

k8s_config.load_kube_config()
api_client = k8s_client.ApiClient()
v1 = k8s_client.CoreV1Api(api_client)
print(v1.api_client.host)
print(v1.list_pod_for_all_namespaces())

Here's the code that doesn't work

k8s_config.load_kube_config()
v1 = k8s_client.CoreV1Api()
print(v1.api_client.host)
print(v1.list_pod_for_all_namespaces())

which produces output

https://localhost
...
MaxRetryError: HTTPSConnectionPool(host='localhost', port=443): Max retries exceeded with url: /api/v1/pods (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f21a4a11fd0>: Failed to establish a new connection: [Errno 111] Connection refused',))

So the problem appears to be that api_client ends up using host 'localhost' for the K8s master which is incorrect; my k8s master is remote and has an actual ip address.

Interestingly I only hit this problem when running the above snippet inside a container on my local machine. However, I don't hit this problem when running on my host machine. However, there's no guarantee that my python environments are equivalent.

Good afternoon. I'm new to Python and I've been working on a python script to remove a particular pod using the Kubernetes API, but it is not working. I was wondering if someone could take a look at my script to see what the problem could be. Currently I'm getting a SyntaxError: unexpected EOF while parsing, which means the code is processing all the way through. Below is my script:

from kubernetes import client, config

config.load_kube_config()

def delete_pod(self,name,namespace,body):
core_v1 = client.CoreV1Api()
delete_options = client.V1DeleteOptions()
try:
api_response = core_v1.delete_namespaced_pod(
name='Stang-hi',
namespace='default',
body=delete_options)
print(api_response)

Good afternoon. I'm new to Python and I've been working on a python script to remove a particular pod using the Kubernetes API, but it is not working. I was wondering if someone could take a look at my script to see what the problem could be. Currently I'm getting a SyntaxError: unexpected EOF while parsing, which means the code is processing all the way through. Below is my script:

from kubernetes import client, config

config.load_kube_config()

def delete_pod(self,name,namespace,body):
core_v1 = client.CoreV1Api()
delete_options = client.V1DeleteOptions()
try:
api_response = core_v1.delete_namespaced_pod(
name='Stang-hi',
namespace='default',
body=delete_options)
print(api_response)

I suspect you have a Python syntax problem in your script unrelated to the kubernetes package, but I'm not sure because the script you've posted isn't formatted correctly. I have edited your script slightly and it seems to work for me.

from kubernetes import client, config

config.load_kube_config()


def delete_pod(name, namespace):
    core_v1 = client.CoreV1Api()
    delete_options = client.V1DeleteOptions()
    api_response = core_v1.delete_namespaced_pod(
        name=name,
        namespace=namespace,
        body=delete_options)
    print(api_response)


if __name__ == '__main__':
    delete_pod(name='Stang-hi', namespace='default')

@hendersonkw I would suggest you to check if you have any unclosed brackets / quotes / etc. Also check for "hidden" characters if you copied parts of the code from an online source.

Thank you Jonathan for the assistance. You were correct is was an issue with the formatting. Once I typed everything again from scratch just like the one you posted it worked with no issues. I wanted to know if there is a way for me to add a variable so when I run the script it ask me for the pod I want to delete and once I input the pod name it deletes that pod? If so can you assist me with that?

@JonathanRRogers Thank you Jonathan for the assistance. You were correct is was an issue with the formatting. Once I typed everything again from scratch just like the one you posted it worked with no issues. I wanted to know if there is a way for me to add a variable so when I run the script it ask me for the pod I want to delete and once I input the pod name it deletes that pod? If so can you assist me with that?

@hendersonkw First, I'd suggest you use an editor or other tool that checks your Python code and shows problems with it before you attempt to run it. I use Flake8 via flycheck in Emacs. Other editors support flake8 or you can run the "flake8" command directly.
Your "delete_pod" function already takes a pod name. If you want to add a command-line parameter for pod name, use argparse from the Python standard library: https://docs.python.org/3/library/argparse.html?highlight=argparse#module-argparse

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Was this page helpful?
0 / 5 - 0 ratings