Spring Active profile setup for existing application

1/13/2022

Any help much appreciated , I have couple of spring boot application running in aks with default profile , i am trying to change the profile from my deployment.yaml using helm

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "helm-chart.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "helm-chart.name" . }}
    helm.sh/chart: {{ include "helm-chart.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "helm-chart.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "helm-chart.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
             
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          env:
          - name: SPRING_PROFILES_ACTIVE
            value: "dev"

what i end up is my pod is been put to crashloopbackoff state saying

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2022-01-12 12:42:49.054 ERROR 1 --- main o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

The Tomcat connector configured to listen on port 8207 failed to start. The port may already be in use or the connector may be misconfigured.

I tried to delete the existing pod and service for the application and did a fresh deploy i still get the same error ..

methods tried :(in all methods docker file is created , pod is created , application in pod is setup to dev profile but the thing is it not able to start the application with the above error , when i remove the profile setting , every thing is workly perfectly fine expect the fact is the application is set to default profile)

1) in docker file :

option a. CMD "java","-jar","/app.jar", "--spring.profiles.active=dev" option b. CMD "java","-jar","-Dspring.profiles.active=dev","/app.jar"

2) changed in deployment.yml as mentioned above

ps : i dont have property file in my application on src/main/resources , i have only application-(env).yml files there .

The idea is to set the profile first and based on the profile the application_(env).yml has to be selected

output from helm

Release "app" has been upgraded. Happy Helming!
NAME: email-service
LAST DEPLOYED: Thu Jan 13 16:09:46 2022
NAMESPACE: default
STATUS: deployed
REVISION: 19
TEST SUITE: None
USER-SUPPLIED VALUES:
image:
  repository: 957123096554.dkr.ecr.eu-central-1.amazonaws.com/app
service:
  targetPort: 8207

COMPUTED VALUES:
image:
  pullPolicy: Always
  repository: 957123096554.dkr.ecr.eu-central-1.amazonaws.com/app-service
  tag: latest
replicaCount: 1
service:
  port: 80
  targetPort: 8207
  type: ClusterIP

Any help is appreciated , thanks

-- Dilu
amazon-web-services
kubernetes
spring
spring-boot

1 Answer

1/13/2022

First of all, please check what profile the application is using, search for line like this (in log):

The following profiles are active: test

When I tested with Spring Boot v2.2.2.RELEASE, application_test.yml file is not used, it has to be renamed to application-test.yml, for a better highlighting of a difference:

application_test.yml # NOT working
application-test.yml # working as expected

What I like even more (but it is Spring Boot specific), you can use application.yml like this:

foo: 'foo default'
bar: 'bar default'

---
spring:
  profiles:
  - test
bar: 'bar test2'

Why I prefer this? Because you can use multiple profiles then, e.g. profile1,profile2 and it behaves as last wins, I mean it will override the values from profile1 with values from profile2, as it was defined in this order... The same does not work with application-profileName.yml approach.

-- Betlista
Source: StackOverflow