k8s 新手。
尝试从基于配置文件的配置映射中读取值。我的 configmap 存在于默认命名空间中。但是,弹簧靴没有拾取价值。
配置图如下所示:
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap-overriding-new-01
data:
application.properties: |-
globalkey = global key value
application-qa.properties: |-
globalkey = global key qa value
application-prod.properties: |-
globalkey = global key prod value
配置映射也在默认命名空间中创建。
kubectl get configmap -n default
NAME DATA AGE
example-configmap-overriding-new-01 3 8d
我的部署文件看起来像
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-configmapk8testing
spec:
selector:
matchLabels:
app: demo-configmapk8testing
replicas: 1
template:
metadata:
labels:
app: demo-configmapk8testing
spec:
containers:
- name: demo-configmapk8testing
image: Path to image
ports:
- containerPort: 8080
args: [
"--spring.profiles.active=prod",
"--spring.application.name=example-configmap-overriding-new-01",
"--spring.cloud.kubernetes.config.name=example-configmap-
overriding-new-01",
"--spring.cloud.kubernetes.config.namespace=default",
"--spring.cloud.kubernetes.config.enabled=true"]
envFrom:
- configMapRef:
name: example-configmap-overriding-new-01
但春季启动日志说: -
2019-07-02 22:10:38.092 WARN 1 --- [ main]
o.s.c.k.config.ConfigMapPropertySource : Can't read configMap with name:
[example-configmap-overriding-new-01] in namespace:[default]. Ignoring
2019-07-02 22:10:38.331 INFO 1 --- [ main]
b.c.PropertySourceBootstrapConfiguration : Located property source:
CompositePropertySource {name='composite-configmap', propertySources=
[ConfigMapPropertySource {name='configmap.example-configmap-overriding-new-
01.default'}]}
2019-07-02 22:10:38.420 INFO 1 --- [ main]
b.c.PropertySourceBootstrapConfiguration : Located property source:
SecretsPropertySource {name='secrets.example-configmap-overriding-new-
01.default'}
2019-07-02 22:10:38.692 INFO 1 --- [ main]
c.e.c.ConfigconsumerApplication : **The following profiles are
active: prod**
--some logs--
Injection of autowired dependencies failed; nested exception is
java.lang.IllegalArgumentException: **Could not resolve placeholder
'globalkey' in value "${globalkey}"**
我的 Spring Boot 配置文件看起来像
@Configuration
public class ConfigConsumerConfig {
@Value(value = "${globalkey}")
private String globalkey;
// with getter and setters
}
我的 pom.xml 也有以下依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
我在本地机器上运行 minikube。我在这里错过了什么吗?
有人可以在这里分享一些输入。