How do I set the environment for spring.datasource on a spring-boot container using a Kubernetes YAML file ? Is it the same as Docker YAML?

1/24/2020

So the other day I just learned about Docker and I could use in my Docker-Compose YAML file something like:

environment:
        - spring.datasource.url=jdbc:postgresql://192.168.100.100/my_database
        - spring.datasource.username=my_username
        - spring.datasource.password=my_password!@#$

I like to implement this on my Kubernetes YAML file. How can I do this?

-- Audi Pratama
docker
kubernetes
kubernetes-pod
spring-boot
yaml

2 Answers

1/24/2020

You can declare environment vars on YAML as on Docker Files, just with different syntax.

Here's the example you requested:

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
spec:
  containers:
  - name: envar-demo
    image: busybox
    args:
    - sleep
    - "86400"
    env:
    - name: spring.datasource.url
      value: "jdbc:postgresql://192.168.100.100/my_database"
    - name: spring.datasource.username
      value: "my_username"
    - name: spring.datasource.password
      value: "my_password!@#
quot;

Now I'll create a simple busybox container to show in runtime the variables activated:

user@minikube:~$ kubectl apply -f envar-pod.yaml
pod/envar-demo created

user@minikube:~$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
envar-demo   1/1     Running   0          8s

user@minikube:~$ kubectl exec -it envar-demo -- /bin/sh
/ # printenv
HOSTNAME=envar-demo
SHLVL=1
HOME=/root
TERM=xterm
spring.datasource.password=my_password!@#$
spring.datasource.url=jdbc:postgresql://192.168.100.100/my_database
spring.datasource.username=my_username
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
/ #

For more information refer to Environment Variable Expose

-- willrof
Source: StackOverflow

1/24/2020

No. Kubernetes yaml files and docker-compose files are differrent. Check Kompose (https://github.com/kubernetes/kompose) tool to convert your docker-compose to kubernetes files.

-- GintsGints
Source: StackOverflow