英文:
How to link SpringBoot application.properties file to Kubernetes ConfigMaps and Secrets
问题
我必须将数据库URL、数据库用户名和密码变量注入到Java Spring Boot应用程序中。我知道这些属性位于res/文件夹下的application.properties文件中。
现在,作为设计更改,我希望采用K8s的ConfigMaps和Secrets来替代在application.properties文件中的硬编码。
我该如何将K8s的ConfigMaps和Secrets链接到应用程序中?我应该如何在application.properties文件中设置占位符?
在应用程序的deployment.yaml中定义ConfigMap和Secret的依赖是否足够?
英文:
I have to inject DB URL, DB Username and Password variables into a Java Spring Boot Application. I know that these properties reside in the application.properties file under res/ folder.
Now as a design change I want to adopt K8s ConfigMaps and Secrets to be used in place of hardcoding in the the application.properties file.
How can I link the CMs and Secrets from K8s to the application. How should I set placeholders in application.properties file?
Does defining the CM and Secret dependency in application deployment.yaml does everything?
答案1
得分: 2
你有两种选择:一种可以在不需要额外依赖的情况下实现,另一种需要使用Spring Boot Cloud。
普通方式
您可以在您的application.yml
中定义环境变量占位符:
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
username: ${DB_USER}
password: ${DB_PASSWORD}
然后,在您的Kubernetes部署中定义环境变量:
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: your-config-map
key: dbHost
- name: DB_PORT
valueFrom:
configMapKeyRef:
name: your-config-map
key: dbPort
- name: DB_NAME
valueFrom:
configMapKeyRef:
name: your-config-map
key: dbName
- name: DB_USER
valueFrom:
secretKeyRef:
name: your-secret
key: dbUser
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: your-secret
key: dbPassword
有关为容器定义环境变量的更多信息,请参阅k8s文档。
Spring Boot Cloud Kubernetes
在参考文档中有一个完整的部分,称为使用ConfigMap PropertySource和Secrets PropertySource。建议您去那里查找详细信息。
英文:
You have two options: one can be achieved without extra dependencies and one with Spring Boot Cloud.
Plain way
You define enviroment variable placeholders in your application.yml
:
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
username: ${DB_USER}
password: ${DB_PASSWORD}
You then define the environment variables in your k8s deployment:
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: your-config-map
key: dbHost
- name: DB_PORT
valueFrom:
configMapKeyRef:
name: your-config-map
key: dbPort
- name: DB_NAME
valueFrom:
configMapKeyRef:
name: your-config-map
key: dbName
- name: DB_USER
valueFrom:
secretKeyRef:
name: your-secret
key: dbUser
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: your-secret
key: dbPassword
More on defining environment variables for containers can be found in the k8s documentation.
Spring Boot Cloud Kubernetes
There is a whole section in the reference called Using a ConfigMap PropertySource and Secrets PropertySource. I suggest you go and look it up there.
答案2
得分: 1
First you need to enable spring.cloud.kubernetes.secrets.enabled
then you can use Kubernetes Secrets via SecretsPropertySource
.
You can use secret name via: -Dspring.cloud.kubernetes.secrets.name=my-secret
where my-sercret
is name of a secret which is defined in kubernetes.
or you can access it in multiple other methods described in the Documentation.
英文:
First you need to enable spring.cloud.kubernetes.secrets.enabled
then you can use Kubernetes Secrets via SecretsPropertySource
.
You can use secret name via: -Dspring.cloud.kubernetes.secrets.name=my-secret
where my-sercret
is name of a secret which is defined in kubernetes.
or you can access it in multiple other methods described in the Documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论