Matelutex
Matelutex

Reputation: 2210

Spring Boot - read container environment variables in properties file

I use: Spring Boot Microservices (containerized) Docker Kubernetes

My case is as follows:

I have to generate link: https://dev-myapp.com OR https://qa-myapp.com depending on the environment in which my service is running (DEV, QA) Depending on the environment (DEV, QA). I have one Spring profile BUT under this profile my app can run in kubernetes on 2 types of environment: DEV or QA. I want to generate proper link - read it from my properties file:

@Value("${email.body}")
  private String emailBody;

application.yaml:

email:
  body: Click on the following URL: ${ENVIRONMENT_URL:}/edge/invitation?code={0}&email={1}

DEVOPS(Kubernetes):

Manifest in workloads folder (DEV branch, the same for qa branch nut this time with https://qa-myapp.com):

apiVersion: v1
kind: Service
...
...
apiVersion: apps/v1
kind: Deployment
...
...
containers:
env:
 - name: ENVIRONMENT_URL
          value: https://dev-myapp.com

So is i possible to read that value from kubernetes container in my Spring properties file? I want to get email.body property depending on the container my service is running on.

Upvotes: 0

Views: 1727

Answers (1)

nischay goyal
nischay goyal

Reputation: 3480

Yes this is possible and have corrected the syntax of the yaml

apiVersion: v1
kind: Service
...
...
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        command: ["/bin/sh", "-c", "env | grep ENVIRONMENT_URL"]
        env:
        - name: ENVIRONMENT_URL
          value: https://myapp.com. #Indedntation Changed
        ports:
        - containerPort: 80

Upvotes: 3

Related Questions