简体   繁体   中英

Springboot: How to reference a variable from Kubernetes secret

I have springboot2.4.0. I am trying to read a variable in K8s secret by using springboot @Value and application.properties but it doesn't work out. It can only print localxyz instead of dXNlcg== ("user"). Anything I'm doing wrong?

My springboot property holder

@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

AAA is not arbitrary. You will have to use this name in your application.properties to get the value as shown below

secret.abc=${AAA}

Deployment:

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: db-secret
              key: abc
        ports:
        - containerPort: 8080

The key-value in application.properties after the #--- won't be read.(There could be a way to make it work but I don't know)

secret.abc=localxyz
#---
spring.config.activate.on-profile=dev
secret.abc=${AAA}//Is this right?

So in my application

@Value("${secret.abc}")
    private String abc;

will only read secret.abc=localxyz . But by using kubectl exec pod123 -- printenv I do see AAA available. So the code need to be changed to

@Value("${AAA}")
    private String abc;

then it can read from the secret.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM