k8s的新手。
尝试从基于配置文件的配置映射中读取值。我的configmap存在于默认名称空间中。 但是,Spring Boot不会获取这些值。
配置图如下:
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
但是spring boot日志显示:-
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。我在这里想念东西吗?
有人可以在这里分享一些意见吗?
答案 0 :(得分:3)
spring-cloud-kubernetes 无权访问Kubernetes API,因此无法读取configMap。查看此文档以获取更多详细信息:https://github.com/spring-cloud/spring-cloud-kubernetes/blob/master/docs/src/main/asciidoc/security-service-accounts.adoc。
简而言之,请应用此配置,它将正常工作:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: YOUR-NAME-SPACE
name: namespace-reader
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
verbs: ["get", "list", "watch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: namespace-reader-binding
namespace: YOUR-NAME-SPACE
subjects:
- kind: ServiceAccount
name: default
apiGroup: ""
roleRef:
kind: Role
name: namespace-reader
apiGroup: ""
您可以在这里更详细地了解角色和角色绑定:https://kubernetes.io/docs/reference/access-authn-authz/rbac/。
注意:您不必创建卷和卷挂载。我会说这是另一种选择。如果您希望以这种方式使用它,则必须在Spring Boot应用程序配置中指定spring.cloud.kubernetes.config.paths
(我已将其写入 bootstrap.yaml 资源文件中)。
例如
spring:
cloud:
kubernetes:
config:
paths: /etc/config/application.yaml
然后通过Kubernetes部署配置创建ConfigMap卷并将其安装在该路径上。在我们的示例中,路径为 / etc / config 。
让我知道它是否对您有用:)
答案 1 :(得分:1)
需要更正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
您应该将configmap挂载为卷,以在应用程序中使用配置文件
volumeMounts:
- name: config
mountPath: /config
volumes:
- name: config
configMap:
name: example-configmap-overriding-new-01
答案 2 :(得分:1)
只是在任何人面临与我相同的根本原因时丢弃提示:https_proxy设置。
我们的项目的部署配置中包含http / https_proxy,并且Spring Kubernetes Client对象试图通过IP地址访问主URL以获取配置映射。
虽然noproxy设置无法识别通配符char“ *”(必须为v4 IPAddress),但我必须将spring.cloud.kubernetes.client.masterUrl设置为kubernetes.default.svc,以便可以通过DNS名称对其进行解析。
答案 3 :(得分:0)
您能否也尝试挂载configmap,也许可以帮忙
volumeMounts:
- mountPath: /app/config
name: example-configmap-overriding-new-01
volumes:
- name: example-configmap-overriding-new-01
configMap:
name: example-configmap-overriding-new-01
请告诉我它是否有效。 谢谢
更正的语法错误