Python: Stop Container in Pod from another Container

Created on 9 Apr 2020  路  10Comments  路  Source: kubernetes-client/python

Stop/Terminated a container from another container with in pod

I have a use case where I need to stop side car containers when service container finish it job. Since a Job can show status as completed only if pod is stopped/terminated. Since sidecar containers continue to run, Job status is "Running" as overall status of pod is running.

Pod Status:

$ kubectl get pods
NAME                READY   STATUS    RESTARTS   AGE
fortio-load-gs4n4   2/3     Running   2          108s

Job Status:

$ kubectl get job
NAME          COMPLETIONS   DURATION   AGE
fortio-load   0/1           81m        81m

Service container has terminated after 30seconds, but due to sidecar in running status, job is stuck at Running status

$ k8 describe job
Name:           fortio-load
Namespace:      mesh-load-fortio-usw2-ppd-dev
Selector:       controller-uid=9bf80f1f-6bde-4e2e-bd50-ea1de1cef0e7
Labels:         app=fortio-load
Annotations:    Parallelism:  3
Completions:    1
Start Time:     Thu, 09 Apr 2020 14:20:57 +0530
Pods Statuses:  1 Running / 0 Succeeded / 0 Failed
Pod Template:
  Labels:       app=fortio-load
                asset=services.mesh.fortioloadrunner
                controller-uid=9bf80f1f-6bde-4e2e-bd50-ea1de1cef0e7
                env=qa
                istio-injected=true
                job-name=fortio-load
                version=v2
  Annotations:  alpha.istio.io/identity: services.mesh.fortioloadrunner
                asset: services.mesh.fortioloadrunner
                iam.amazonaws.com/role: k8s-mesh-load-fortio-usw2-ppd-dev
                policyId: p-xxxxxxxx
                sidecar.istio.io/inject: true
                traffic.sidecar.istio.io/includeInboundPorts: 8080
  Containers:
   fortio-load:
    Image:      fortio/fortio
    Port:       <none>
    Host Port:  <none>
    Command:
      fortio
    Args:
      load
      -t
      30s
      -c
      128
      -qps
      10
      -loglevel
      Debug
      http://services.gateway.meshtestblackhole.mesh/get
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  3m9s  job-controller  Created pod: fortio-load-gs4n4
kindocumentation

Most helpful comment

@kollender

        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 = '/bin/sh -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()

All 10 comments

Hi @yks0000, I think this issue would be more suitable to be filed in kubernetes/website as it is related to the overall behavior of a Job and not tightly related to the python client.

/assign

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

/remove-lifecycle stale

@yks0000 :wave: -- Closing this issue for now, since there was no response.

Please feel free to reopen if you have any unresolved queries.

/close

@palnabarun: Closing this issue.

In response to this:

@yks0000 :wave: -- Closing this issue for now, since there was no response.

Please feel free to reopen if you have any unresolved queries.

/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.

@palnabarun Thanks. I was able to do this using existing api.

@yks0000
Can you share how?

@kollender

        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 = '/bin/sh -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()

for windows container I changed /bin/sh to cmd.exe but still the container is not getting killed. Is there anything else i need to change in your code. I am not even getting exception after running the above piece of code.

Was this page helpful?
0 / 5 - 0 ratings