我是kubernetes的新手,需要在openshift平台上使用k8s configmap来外部化springboot应用程序的属性文件。我已经将属性文件保存在一个git仓库中"迎宾。message=Spring Boot myapplication。properties已经作为卷挂载到Kubernetes上,并使用"oc create configmap myconfig——from-file=myapplication.properties">命令创建了configmap。使用"oc get configmap myconfig -o yaml">命令也可以看到相同的效果:
data:
myapplication.properties: greeter.message=Spring Boot myapplication.properties has been mounted as volume on Kubernetes!
on Kubernetes!
kind: ConfigMap
metadata:
creationTimestamp: 2021-08-24T04:45:27Z
name: myconfig
namespace: mynamespace
resourceVersion: "53471"
selfLink: /api/v1/namespaces/default/configmaps/myconfig
uid: 73ca674c-8afc-71e1-9a8a-7da609902085
现在我有一个springboot rest控制器作为
@RestController
@Slf4j
public class GreeterController {
@Value("${greeter.message}")
private String greeterMessageFormat;
@GetMapping("/greet/{user}")
public String greet(@PathVariable("user") String user) {
return String.format(greeterMessageFormat);
}
}
最后,我对部署文件进行了更改,以创建和挂载卷
spec:
containers:
volumeMounts:
- name: application-config
mountPath: "/etc/config"
readOnly: true
volumes:
- name: application-config
configMap:
name: myconfig
现在问题出现了,当我尝试启动pod时,springboot应用程序无法启动,显示它找不到${greeter的任何值。@Value("${greeter.message}"),因为我在应用程序src/main/resources/app中没有任何这样的属性。如果我提供了一个属性,那么我的springboot应用程序会从src/main/resources/app中选择该属性。
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'greeter.message' in value "${greeter.message}"
预期值应该取自我创建的configmap。
我参考了这个https://developers.redhat.com/blog/2017/10/03/configuring-spring-boot-kubernetes-configmap#cm-as-files并做了相同的操作。
Thanks in Advance.
Try
envFrom: ##Reference all key-value pairs of the sepcial-config ConfigMap.
- configMapRef:
name: myconfig
引用:
https://www.exoscale.com/syslog/configuration-management-kubernetes-spring-boot/https://www.alibabacloud.com/help/doc-detail/86556.htm