【问题标题】:Springboot: How to reference a variable from Kubernetes secretSpring Boot:如何从 Kubernetes 机密中引用变量
【发布时间】: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


【解决方案1】:

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 中更改名称:secretabc。 db-secret 似乎未定义

【讨论】:

  • 感谢您的回复。对不起,这是错字。我确实使用了secret.abc=${AAA},但该应用程序在部署时只能读取localxyz。你有什么发现我做错了吗?
  • 您的 secretKeyRef 名称似乎不匹配。在您的 deployment.yaml 中更改名称:secretabc。 db-secret 似乎未定义
【解决方案2】:

#--- 之后的 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;

然后它就可以从秘密中读取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2019-05-23
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多