简体   繁体   中英

How to inject deployment yaml env variable in springboot application yaml

I am trying to read environment variable declared in deployment yaml of Kubernetes into springboot application.yaml

Below is sample in deployment.yaml

    spec:
      containers:
        env:
        - name: SECRET_IN
          value: dev

Below is sample in application.yaml

    innovation:
      in: ${SECRET_IN:demo}

But on localhost when I try to print innovation.in (@Configuration is created correctly) I am not getting "dev" in output, it always prints demo , it appears the link between deployment, application yaml is not happening ,could someone please help.

You can store the whole application.YAML config file into the config map or secret and inject it with the deployment only

For example :

kind: ConfigMap
apiVersion: v1
metadata:
  name: demo
data:
  application.yaml: |-
    pool:
      size:
        core: 1
        max:16

if your application.properties is something like

example:

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/dbname
spring.datasource.username=user
spring.datasource.password=password

You can replace it with

jdbc:mysql://${MYSQL_HOST:localhost}:3306/dbname

Deployment.yaml will be something like

spec:
      containers:
      - name: demowebapp
        image: registry.gitlab.com/unicorn/unicornapp:1.0
        ports:
        - containerPort: 8080
        imagePullPolicy: Always
        env:
          - name: MYSQL_HOST
            value: mysql-prod

You can save more config into the config map & secret also based on the requirement.

Read more at : https://pushbuildtestdeploy.com/spring-boot-application.properties-in-kubernetes/

I think you did everything right, I have a similar working setup, although without a default 'demo'.

A couple of clarification from the spring boot's standpoint that might help.

  1. application.yml can contain placeholders that can be resolved from the environment variables indeed.
  2. Make sure that this application.yml is not "changed" (rewritten, filtered by maven whatever) during the compilation of the spring boot application artifact.
  3. The most important: spring boot knows nothing about the k8s setup . If the environment variable exists - it will pick it. So the same could be checked even locally - define the env. variable on your local machine and run the spring boot application.
  4. The chances are that somehow when the application runs (with the user/group) the environment variables are not accessible - check it by printing the environment variables (or this specific one) right before starting the spring boot application. Or you can do it in java in the main method:
Map<String, String> env  = System.getenv();
env.entrySet().forEach(System.out::println);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM