k8s的新手。
我在配置映射中有多个配置文件,希望我的应用基于spring.profiles.active来获取属性。
案例1:-
我的ConfigMap看起来像
kind: ConfigMap
apiVersion: v1
metadata:
name: example-configmap-overriding-new-02
data:
application.properties: |-
globalkey = global key value
TeamName = Team Name value
Purpose = Purpose value
RootFile = Root file value
Company = company value
Place = Place value
Country = Country value
---
spring.profiles = qa
globalkey = global key qa value
TeamName = Team Name qa value
Purpose = Purpose qa value
RootFile = Root file qa value
---
spring.profiles = prod
globalkey = global key prod value
Company = company prod value
Place = Place prod value
Country = Country prod value
我的部署文件看起来像
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-configmapsingleprofile
spec:
selector:
matchLabels:
app: demo-configmapsingleprofile
replicas: 1
template:
metadata:
labels:
app: demo-configmapsingleprofile
spec:
serviceAccountName: config-reader
containers:
- name: demo-configmapsingleprofile
image: image name
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: example-configmap-overriding-new-02
securityContext:
privileged: false
Spring Boot中的我的配置文件看起来像
@Configuration
public class ConfigConsumerConfig {
@Value(value = "${globalkey}")
private String globalkey;
@Value(value = "${TeamName}")
private String teamName;
@Value(value = "${Purpose}")
private String purpose;
@Value("${RootFile}")
private String rootFile;
@Value("${Company}")
private String company;
@Value("${Place}")
private String place;
@Value("${Country}")
private String country;
//With getters and setters
}
我的application.properties看起来像
spring.profiles.active=prod
spring.application.name=example-configmap-overriding-new-02
spring.cloud.kubernetes.config.name=example-configmap-overriding-new-02
spring.cloud.kubernetes.config.namespace=default
spring.cloud.kubernetes.config.sources[0].name=example-configmap-overriding-new-02
spring.cloud.kubernetes.config.enabled=true
应用运行正常,并从配置映射中获取了值。但这是从错误的配置文件中挑选的。从“质量检查”配置文件中提取了一些值。
我得到的最终结果是:-
{"globalkey":"global key prod value","teamName":"Team Name qa value","purpose":"Purpose qa value","rootFile":"Root file qa value","company":"company prod value","place":"Place prod value","country":"Country prod value"}
案例2:-
但是,当我将配置映射与yaml一起使用时,如下所示,
kind: ConfigMap
apiVersion: v1
metadata:
name: example-configmap-overriding-new-02
data:
application.yml: |-
globalkey : global key value
TeamName : Team Name value
Purpose : Purpose value
RootFile : Root file value
Company : company value
Place : Place value
Country : Country value
---
spring:
profiles: qa
globalkey : global key qa value
TeamName : Team Name qa value
Purpose : Purpose qa value
RootFile : Root file qa value
---
spring:
profiles: prod
globalkey : global key prod value
Company : company prod value
Place : Place prod value
Country : Country prod value
我得到了预期的结果。它按预期从产品配置文件中选取。
{"globalkey":"global key prod value","teamName":"Team Name value","purpose":"Purpose value","rootFile":"Root file value","company":"company prod value","place":"Place prod value","country":"Country prod value"}
第2种情况按预期运行,但第一种情况未达到预期效果。
我对配置映射有任何错误/误解吗?可以在这里提出一些建议吗?
谢谢。
答案 0 :(得分:1)
鉴于尚无其他答案,请让我尝试帮助您。
YAML文件中的---
分隔符可分隔多个YAML文档。在属性文件中不是这样。不确定如何使用这些---
来加载属性文件,但是现在不知道它的结构方式会导致重复的密钥,其中一个密钥会覆盖之前的相同密钥。
使用属性文件时,创建多个键/值对-每个配置文件一个。像这样:
kind: ConfigMap
apiVersion: v1
metadata:
name: example-configmap-overriding-new-02
data:
application.properties: |-
globalkey = global key value
TeamName = Team Name value
Purpose = Purpose value
RootFile = Root file value
Company = company value
Place = Place value
Country = Country value
application-qa.properties: |-
spring.profiles = qa
globalkey = global key qa value
TeamName = Team Name qa value
Purpose = Purpose qa value
RootFile = Root file qa value
application-prod.properties: |-
spring.profiles = prod
globalkey = global key prod value
Company = company prod value
Place = Place prod value
Country = Country prod value
它们将在正在运行的容器中“实现”为单个文件,位于安装位置。
答案 1 :(得分:0)