Deploy a scalable application on Kubernetes which requires each replica Pod to have different args

8/24/2019

I am trying to understand how to deploy an application on Kubernetes which requires each Pod of the same deployment to have different args used with the starting command.

I have this application which runs spark on Kubernetes and needs to spawn executor Pods on start. The problem is that each Pod of the application needs to spawn its own executors using its own port and spark app name.

I've read of stateful sets and searched the documentation but I didn't found a solution to my problem. Since every Pod needs to use a different port, I need that port to be declared in a service if I understood correctly, and also directly passed as an argument to the pod command in the args.

Is there a way to obtain this without using multiple deployments, one for each pod I need to create? Because this is the only solution i can think of but it can't be scaled after being deployed. I'm using Helm to deploy the application, so I can easily create as many deployments and / or services as needed, but I would like to find a solution which can scale at runtime, if possible.

-- AndD
kubernetes
kubernetes-helm
statefulset

3 Answers

8/28/2019

hey I am not sure if this will exactly match your scenario but I think this is what you can try. Use a sidecar container to run the replica instances, A sidecar is a container which runs along with the main container and also shares the same namespace and can share volumes across each container.

Now to pass the different arguments to each container or sidecar, you will have to tweak the dockerfile or rather tweak the way your container starts.

Create a start.sh script file which accepts the arguments and starts the container with those arguments, the trick here is to accept the argument from environment variables thus allowing you to later configure these from configmaps or pod env.

So here is an example of php/laravel application running the same code and starting with different arguments. And the start.sh the file looks like this.

#!/bin/sh


if [ "${CONTAINER_ROLE}" = "queue" ];
then
    echo "Running the queue..."
    php artisan queue:work --queue=${QUEUENAME}
    echo "Queue Started"
else
   echo "Running Iceberg."
   exec apache2-foreground
fi

So a sample dockerfile looks like this

FROM php:7.1.24-apache


COPY . /srv/myapp
...
...
RUN chown -R www-data:www-data /srv/app \
    && a2enmod remoteip && a2enmod rewrite

WORKDIR /srv/app
RUN chmod +x .docker/start.sh
CMD [ "sh",".docker/start.sh"] 

Let me know how it goes.

-- damitj07
Source: StackOverflow

8/26/2019

I don't think you can have a Deployment which creates PODs from different Specs. You can't have it in Kubernetes and Helm won't help here (since Helm is just a template manager over Kubernetes configurations).

What you can do is to specify each Pod as a separate configuration (if single Pod, you don't necessarily need Deployment) and let Helm manage it.

-- RafaƂ Leszko
Source: StackOverflow

8/29/2019

Posting the solution I used since it could be useful for other people searching around.

In the end I found a great configuration to solve my problem. I used a StatefulSet to declare the deployment of the Spark application. Associated with the StatefulSet, a headless Service which expose each pod on a specific port.

StatefulSet can declare a property spec.serviceName which can have the same name of a headless service to create a unique network name for each Pod. Something like <pod_name>.<service_name>

Additionally, each Pod has a unique and not-changing name which is created using the application name and an ordinal starting from 0 for each replica Pod.

Using a starting script in the docker image and inserting in the environment of each Pod the pod name from the metadata, I was able to use different configurations for each pod since, even with the same deployment, each pod have their own unique metadata name and I can use the StatefulSet service to obtain what I needed.

This way, the StatefulSet is scalable at run time and works as expected.

-- AndD
Source: StackOverflow