Helm kubernetes. Is it possible to inject what replica number the specific replica is?

4/8/2020

So I have a spring boot app which runs with two replicas. I want to be able to inject whether the app is replica 1 or 2. I want to this as i want my application to run a proccess on startup, however I only want one of the replicas to run the start up proccess

My test.values.yaml.template

spring-boot:

  application:
      spring:
        datasource:
          url: url
          username: username
          password: password
        profiles:
          active: prod, agent
-- Daniel Haughton
kubernetes
kubernetes-helm

3 Answers

4/8/2020

TL;DR:

  • No, you cannot inject job operations directly on a ReplicaSet.

It's not a helm issue, it's a core Kubernetes concept:

From ReplicaSet’s Documentation:

A ReplicaSet purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a specified number of identical Pods. This actually means that you may never need to manipulate ReplicaSet objects: use a Deployment instead, and define your application in the spec section.

  • The purpose of a ReplicaSet is to replicate the pods (usually described in a deployment) and ensure the desired number of replicas is always available.

I want to be able to inject whether the app is replica 1 or 2. I want to this as i want my application to run a proccess on startup, however I only want one of the replicas to run the start up proccess

  • Pods are separate hosts, It's not like two instances of a app running inside the same computer, hence if you need a startup job to make them work, this job needs to be run in each one of them.

A Pod represents a unit of deployment: a single instance of an application in Kubernetes, which might consist of either a single container or a small number of containers that are tightly coupled and that share resources.

For that you can use a InitContainer:

Init containers are exactly like regular containers, except:

  • Init containers always run to completion.
  • Each init container must complete successfully before the next one starts

If you have any question let me know in the comments.

-- willrof
Source: StackOverflow

4/8/2020

In general if for any reason you need to make your application replicas distinguishable from each other, then you should use StatefulSet instead of Deployment. Then you can inject the POD name into your container as env variable and use it in your application.

-- Rafał Leszko
Source: StackOverflow

4/8/2020

If you have any startup process, one of the best option is to make use of init container. Please see more details here

-- Bimal
Source: StackOverflow