【发布时间】:2022-01-21 07:00:21
【问题描述】:
我有springboot2.4.0。我正在尝试使用 springboot @Value 和 application.properties 读取 K8s 机密中的变量,但它不起作用。它只能打印localxyz 而不是dXNlcg==("user")。有什么我做错了吗?
我的 springboot 属性持有者
@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
【问题讨论】:
-
这个环境变量在 pod 中是否可用?您可以
kubectl exec到 pod 并检查它。只是为了排除 Kubernetes 本身。
标签: spring-boot kubernetes kubernetes-secrets