0

I am using the following code to kill the container in a pod

        from kubernetes.stream import stream
        . . .
        . . .
        # custom function to read downwardAPI generated metadata file which return ns and pod name
        namespace_name, pod_name = pod_metadata.pod_info() 

        # custom function to create CoreV1Api client
        v1 = k8s_client.CoreV1Api()   

        response = v1.list_namespaced_pod(namespace=namespace_name, field_selector=f"metadata.name={pod_name}")
        command = 'cmd.exe -c "kill 1"'
        command = shlex.split(command)

        for pods in response.items:
            logger.info(f"Pod Name: {pods.metadata.name}")
            for container in pods.status.container_statuses:
                if container.ready:
                    logger.info(f"Shutting down {container.name}")
                    resp = stream(
                        v1.connect_get_namespaced_pod_exec,
                        name=pods.metadata.name,
                        namespace=namespace_name,
                        container=container.name,
                        command=command,
                        stderr=True,
                        stdin=True,
                        stdout=True,
                        tty=False,
                        _preload_content=False,
                    )
                    resp.close()

The code is obtained from the following link :- https://github.com/kubernetes-client/python/issues/1138 The container I have inside POD are windows one. I am not getting any exception and also I dont see container is being killed. I am loading config from kubeconfig.

Batman22
  • 376
  • 4
  • 25
  • there is no kill command on windows. Try taskkill . Something like this ```cmd.exe -c "taskkill /F /PID 1"``` – apoorva kamath Nov 10 '20 at 02:39
  • @apoorvakamath thank you for reply. i am trying to first do it using kubectl. here is my command for kubectl :- kubectl exec helloworld-deployment-8494f859d5-fnkgt -- cmd.exe -c helloworldcontainer -- echo "hello world" > output.txt this is a sample kubectl command which should create output.txt inside container but it does on my local box. what i am doing wrong? – Batman22 Nov 10 '20 at 02:44
  • @apoorvakamath also i tried this using kubectl :- kubectl exec helloworld-deployment-8494f859d5-fnkgt -- cmd.exe -c helloworldcontainer -- taskkill /F /PID 1 it didnt work. is there anything you would like to suggest? – Batman22 Nov 10 '20 at 02:49
  • Can you give this a try. ```kubectl exec helloworld-deployment-8494f859d5-fnkgt -c helloworldcontainer cmd.exe -- "taskkill /F /PID 1" ``` – apoorva kamath Nov 10 '20 at 03:01
  • @apoorvakamath same thing...i think first we should try the simplest one like :- kubectl exec helloworld-deployment-8494f859d5-fnkgt -- cmd.exe -c helloworldcontainer -- echo "hello world" > output.txt is there anything wrong with this? – Batman22 Nov 10 '20 at 03:06
  • @apoorvakamath you can reply to this thread for the hello world file creation issue using kubectl exec https://stackoverflow.com/questions/64761455/execute-command-inside-kubernetes-pod-containing-windows-container – Batman22 Nov 10 '20 at 03:08
  • yes, -c helloworldcontainer is not the argument you want to send to cmd.exe, Its for kubectl to determine your container on which you have to run the cmd.exe. so it needs to be ```kubectl exec helloworld-deployment-8494f859d5-fnkgt -c helloworldcontainer cmd.exe -- echo "hello world" > output.txt``` – apoorva kamath Nov 10 '20 at 03:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224347/discussion-between-batman22-and-apoorva-kamath). – Batman22 Nov 10 '20 at 03:13
  • @apoorvakamath i tried it but same result..since i have single container inside tht pod i even removed the container name but same issue – Batman22 Nov 10 '20 at 03:16
  • @apoorvakamath also output.txt is created on my local machine – Batman22 Nov 10 '20 at 03:17
  • @apoorvakamath kubectl exec helloworld-deployment-8494f859d5-fnkgt -c helloworldcontainer -- ipconfig if i run this it works! – Batman22 Nov 10 '20 at 04:23
  • @apoorvakamath even i changed this in code and that also works – Batman22 Nov 10 '20 at 04:23

0 Answers0