我有springboot2.4.0。我试图通过使用springboot @Value和application.properties来读取K8s秘密中的变量,但是它没有成功。它只能打印localxyz
,而不能打印dXNlcg==
(“用户”)。我做错什么了吗?
我的弹簧保持架
@Component
@Getter
public class PropertyHolder {
@Value("${secret.abc}")
private String abc;
}
Application.properties
secret.abc=localxyz
#---
spring.config.activate.on-profile=dev
secret.abc=${AAA}//Is this right?
secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: secret.abc
data:
abc: dXNlcg==
deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-ing-app
spec:
replicas: 1
selector:
matchLabels:
app: test-ing-app
template:
metadata:
labels:
app: test-ing-app
spec:
containers:
- name: config-demo
image: xxxxx
env:
- name: SPRING_PROFILE
value: dev
- name: SPRING_APPLICATION_JSON
valueFrom:
configMapKeyRef:
name: spring-config
key: dev-config.json
- name: AAA # Is this just a arbitrary name?
valueFrom:
secretKeyRef:
name: secret.abc
key: abc
ports:
- containerPort: 8080
发布于 2021-12-19 04:57:48
AAA不是武断的。您必须在application.properties中使用此名称才能获得如下所示的值
secret.abc=${AAA}
部署:
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-ing-app
spec:
replicas: 1
selector:
matchLabels:
app: test-ing-app
template:
metadata:
labels:
app: test-ing-app
spec:
containers:
- name: config-demo
image: xxxxx
env:
- name: SPRING_PROFILE
value: dev
- name: SPRING_APPLICATION_JSON
valueFrom:
configMapKeyRef:
name: spring-config
key: dev-config.json
- name: AAA # Is this just a arbitrary name?
valueFrom:
secretKeyRef:
name: secretabc
key: abc
ports:
- containerPort: 8080
而且,您的secretKeyRef名称似乎不匹配。更名:在你的deployment.yaml中的秘密。数据库-秘密似乎没有定义。
发布于 2021-12-21 23:39:31
application.properties中#---
后的键值将不会被读取。(可能有办法使其工作,但我不知道)
secret.abc=localxyz
#---
spring.config.activate.on-profile=dev
secret.abc=${AAA}//Is this right?
所以在我的申请中
@Value("${secret.abc}")
private String abc;
只会读secret.abc=localxyz
。但是通过使用kubectl exec pod123 -- printenv
,我确实看到了可用的AAA
。因此,代码需要更改为
@Value("${AAA}")
private String abc;
然后它就能从秘密中读出来。
https://stackoverflow.com/questions/70408630
复制相似问题