kubernetes icon indicating copy to clipboard operation
kubernetes copied to clipboard

Added deployment run script

Open JordyBottelier opened this issue 3 years ago • 6 comments

Hi,

I've been using this plugin for a while now and I love it, but I wanted to add some functionality in regards to deployments. I sometimes want to run a job/script on one of the pods of a deployment (randomly), with the option of retrying the job automatically if it fails.

I tested the functionality both locally and on a Kubernetes cluster and it seems to work fine. I used a lot of code from the pods-run-script.py and even extracted some parts to the common.py file.

If there's any questions or improvements you'd like to see let me know

JordyBottelier avatar Feb 24 '22 16:02 JordyBottelier

I'm facing a similar problem...

Because the pod resource model can have multiple pods per deployment, if I try to run an script across all my deployments, it actually runs across all my pods -- which can be a problem if two pods are doing the same thing at the same time and could deadlock or conflict.

If I had a list of deployments or namespaces, I could use that to run this updated code. But it seems that I still end up running the code once per pod if I derice the list of deployment names from my nodes/pods. I have no way to de-duplicate those nodes so there is only one per deployment.

In Rancher 1.x, which we used before k8s, I updated the plugin to create a counter so container would increment each time it found a container within the service/deployment. It worked, but was a bit of a hack.

Do you have a way to run only one pod per deployment based on the node inventory with your approach here?

kdebisschop avatar May 05 '22 18:05 kdebisschop

I'm facing a similar problem...

Because the pod resource model can have multiple pods per deployment, if I try to run an script across all my deployments, it actually runs across all my pods -- which can be a problem if two pods are doing the same thing at the same time and could deadlock or conflict.

If I had a list of deployments or namespaces, I could use that to run this updated code. But it seems that I still end up running the code once per pod if I derice the list of deployment names from my nodes/pods. I have no way to de-duplicate those nodes so there is only one per deployment.

In Rancher 1.x, which we used before k8s, I updated the plugin to create a counter so container would increment each time it found a container within the service/deployment. It worked, but was a bit of a hack.

Do you have a way to run only one pod per deployment based on the node inventory with your approach here?

I don't fully understand your question or what you are actually trying to accomplish here? The code that I wrote executes a job on exactly 1 pod per deployment, but I do not fully understand your use case

JordyBottelier avatar May 23 '22 11:05 JordyBottelier

You're right -- my comment was a bit unclear. A minor difference is that I want to run a command rather than a script. The main difference is that I want to use the node selection interface in Rundeck -- so I can run across all deployments and have the node set automatically update and run on exactly one pod per deployment. It looks like I would have to add jobs and specify the deployment as I added deployments to my k8s environment.

kdebisschop avatar May 23 '22 12:05 kdebisschop

I have just created a pull request (https://github.com/rundeck-plugins/kubernetes/pull/131) to illustrate the approach I have taken...it may not be as aesthetically correct as defining a node resource based on the ReplicaSet (which is what I think I would need to do to use node selection to identify one node per deployment) but it is effective and is only a few lines of code change that enables me to use all the rest of the code in pods-* python scripts without needing to add new copied code.

kdebisschop avatar May 23 '22 14:05 kdebisschop

@kdebisschop I checked out your code and left a comment

We have a somewhat similar use-case, however, they are still quite different :)

JordyBottelier avatar Jun 03 '22 14:06 JordyBottelier

Hi @JordyBottelier

I think you can do the same using an Orchestrator plugin and a node filter with the deployment label, something like this:

Screen Shot 2022-08-25 at 11 05 50 Screen Shot 2022-08-25 at 11 06 32

ltamaster avatar Aug 25 '22 15:08 ltamaster