i am very new to Spring Boot and the application.properties. I have the problem, that i need to be very flexible with my database port, because i have two different databases. Therefore i want to read the port from a environment variable. I tried the following:
spring.data.mongodb.uri = mongodb://project1:${db-password}@abc:12345/projectThis code works fine, if my Database has the port 12345. But if i now try to read the port from an environment variable there is a problem. I tried this:
spring.data.mongodb.uri = mongodb://project1:${db-password}@abc:${port}/projectThe problem is the following: I am using k8 and Jenkins. The environment variable "port" is given to my program in my k8 and this works fine for "db-password", but not for the port. My Jenkins says: "The connection string contains an invalid host 'abd:${port}'. The port '${port}' is not a valid, it must be an integer between 0 and 65535"
So now to my question: How can i read a port as an environment variable, without getting this error?
Thank you in advance!
To inject environment variable to the pods you can do the following:
You can create ConfigMap and configure your pods to use it.
Steps required:
ConfigMapI provided simple ConfigMap below to store your variables:
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config
data:
  port: "12345"To apply it and be able to use it invoke following command:
$ kubectl create -f example-configmap.yaml
The ConfigMap above will create the environment variable port with value of 12345.
Check if ConfigMap was created successfully:
$ kubectl get configmap
Output should be like this:
NAME             DATA   AGE
example-config   1      21mTo get the detailed information you can check it with command:
$ kubectl describe configmap example-config
With output:
Name:         example-config
Namespace:    default
Labels:       <none>
Annotations:  <none>
Data
====
port:
----
12345
Events:  <none>
I provided simple deployment with ConfigMap included:
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        envFrom:
          - configMapRef:
              name: example-config
        ports:
        - containerPort: 80Configuration responsible for using ConfigMap:
        envFrom:
          - configMapRef:
              name: example-configAfter that you need to run your deployment with command:
$ kubectl create -f configmap-test.yaml
And check if it's working:
$ kubectl get pods
With output:
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-84d6f58895-b4zvz   1/1     Running   0          23m
nginx-deployment-84d6f58895-dp4c7   1/1     Running   0          23mTo test if environment variable is working you need to get inside the pod and check for yourself.
To do that invoke the command:
$ kubectl exec -it NAME_OF_POD -- /bin/bash
Please provide the variable NAME_OF_POD with appropriate one for your case.
After successfully getting into container run:
$ echo $port
It should show:
root@nginx-deployment-84d6f58895-b4zvz:/# echo $port
12345Now you can use your environment variables inside pods.