kil*_*net 12 spring-boot kubernetes
我需要覆盖在 k8s 中运行的 Spring Boot 应用程序的 application.yml 中定义的一些属性。我怎样才能做到这一点?我发现的唯一方法是挂载整个 application.yml 但我只需要覆盖一个属性。
Man*_*uel 12
可以通过其他方式实现,这就是我再次回答的原因。以简单的方式做到这一点。
在 configmap 中创建 application.yml 并将其作为名为 config 的子目录挂载到 spring boot jar in 所在的同一目录中。
当您的应用程序启动时,Spring Boot 将自动从以下位置查找并加载 application.properties 和 application.yaml 文件:
类路径根
类路径/config包
当前目录
当前目录下的/config子目录
/config 子目录的直接子目录
这意味着我们无需进行任何设置。它应该在子目录 config 中找到配置。
apiVersion: v1
kind: ConfigMap
metadata:
name: spring-application-config
data:
application.yml: |
spring:
application:
name: This is just an example, add as many values as you want.
Run Code Online (Sandbox Code Playgroud)
pod.yaml:
...
volumeMounts:
- name: spring-application-config
mountPath: /app/config
- name: spring-application-config
configMap:
name: spring-application-config
...
Run Code Online (Sandbox Code Playgroud)
假设你的 spring boot jar 文件位于路径/app
Spring Boot 应用程序应该使用环境变量覆盖配置文件中的属性(大多数情况下是在 application.yml 中)。
假设应用程序通过 Pod 部署在 Kubernetes 上(但这并不重要,Deployments、StatefulSets、Jobs 在环境方面都是相同的),您可以通过直接将环境变量分配给 Deployment 本身或使用ConfigMap(或 Secret,如果该属性更安全且被屏蔽)
apiVersion: v1
kind: Pod
metadata:
name: example
spec:
containers:
- name: example-container
image: example-image:latest
env:
- name: THIS_IS_AN_ENV_VARIABLE
value: "Hello from the environment"
- name: spring.persistence.url
value: "The persistence url, for example"
Run Code Online (Sandbox Code Playgroud)
更好的是,您可以将 ConfigMap 的所有内容作为环境变量注入:
apiVersion: v1
kind: ConfigMap
metadata:
name: example-config
data:
spring.persistence.url: "your persistence url"
spring.mail.user: "your mail user"
Run Code Online (Sandbox Code Playgroud)
然后是你的 Pod:
apiVersion: v1
kind: Pod
metadata:
name: example
spec:
containers:
- name: example-container
image: example-image:latest
envFrom:
- configMapRef:
name: example-config
Run Code Online (Sandbox Code Playgroud)
在容器内部,变量将在环境中.. Spring 应该使用它们来覆盖在 application.yml 中定义(或者甚至可能未定义)的同名变量
欲了解更多信息:
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/ https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-信息/ https://docs.spring.io/spring-boot/docs/1.3.3.RELEASE/reference/html/boot-features-external-config.html
| 归档时间: |
|
| 查看次数: |
21970 次 |
| 最近记录: |