无法读取名称为 [xx] in namespace ['default'] 的 configmap 忽略 [英] Cannot read configmap with name: [xx] in namespace ['default'] Ignoring

查看:69
本文介绍了无法读取名称为 [xx] in namespace ['default'] 的 configmap 忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

k8s 新手.

尝试从基于配置文件的配置映射中读取值.我的 configmap 存在于默认命名空间中.但是,spring boot 没有获取值.

配置图如下所示:

apiVersion: v1种类:ConfigMap元数据:名称:example-configmap-overriding-new-01数据:application.properties: |-globalkey = 全局键值应用程序-qa.properties: |-globalkey = 全局密钥 qa 值应用程序-prod.properties: |-globalkey = 全局密钥产品值

配置映射也在默认命名空间中创建.

kubectl get configmap -n default姓名数据时代example-configmap-overriding-new-01 3 8d

我的部署文件看起来像

apiVersion:apps/v1种类:部署元数据:名称:demo-configmapk8testing规格:选择器:匹配标签:应用程序:demo-configmapk8testing复制品:1模板:元数据:标签:应用程序:demo-configmapk8testing规格:容器:- 名称:demo-configmapk8testing图像:图像的路径端口:- 容器端口:8080参数: ["--spring.profiles.active=prod","--spring.application.name=example-configmap-overriding-new-01","--spring.cloud.kubernetes.config.name=example-configmap-覆盖-new-01","--spring.cloud.kubernetes.config.namespace=default",--spring.cloud.kubernetes.config.enabled=true"]envFrom:- configMapRef:名称:example-configmap-overriding-new-01

但是 spring boot 日志说:-

2019-07-02 22:10:38.092 WARN 1 --- [主要]o.s.c.k.config.ConfigMapPropertySource :无法读取具有名称的 configMap:命名空间中的 [example-configmap-overriding-new-01]:[default].无视2019-07-02 22:10:38.331 信息 1 --- [主要]b.c.PropertySourceBootstrapConfiguration :位于属性源:CompositePropertySource {name='composite-configmap', propertySources=[ConfigMapPropertySource {name='configmap.example-configmap-overriding-new-01.default'}]}2019-07-02 22:10:38.420 信息 1 --- [主要]b.c.PropertySourceBootstrapConfiguration :位于属性源:SecretsPropertySource {name='secrets.example-configmap-overriding-new-01.默认'}2019-07-02 22:10:38.692 信息 1 --- [主要]c.e.c.ConfigconsumerApplication : **以下配置文件是活动:生产**——一些日志——自动装配依赖项的注入失败;嵌套异常是java.lang.IllegalArgumentException: **无法解析占位符'globalkey' 值 "${globalkey}"**

我的 spring boot 配置文件看起来像

@Configuration公共类 ConfigConsumerConfig {@Value(value = "${globalkey}")私人字符串全局密钥;//使用 getter 和 setter}

我的 pom.xml 也有以下依赖项.

 <依赖><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-kubernetes-config</artifactId><version>1.0.2.RELEASE</version></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><可选>真</可选></依赖>

我在本地机器上运行 minikube.我在这里遗漏了什么吗?

有人可以在这里分享一些意见吗.

解决方案

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:角色api版本:rbac.authorization.k8s.io/v1元数据:命名空间:你的名字空间名称:命名空间读取器规则:- apiGroups: ["", "extensions", "apps"]资源:[configmaps"、pods"、services"、endpoints"、secrets"]动词:[get"、list"、watch"]---种类:角色绑定api版本:rbac.authorization.k8s.io/v1元数据:名称:命名空间读者绑定命名空间:你的名字空间科目:- 种类:ServiceAccount名称:默认api组:"角色参考:种类:角色名称:命名空间读取器api组:"

您可以在此处详细了解角色和角色绑定:https://kubernetes.io/docs/reference/access-authn-authz/rbac/.

注意:您不必创建卷和卷挂载.我会说它是它的替代品.如果您想以这种方式使用它,那么您必须在 Spring Boot 应用程序配置中指定 spring.cloud.kubernetes.config.paths(我已将其写入 bootstrap.yaml强>资源文件).例如

弹簧:云:Kubernetes:配置:路径:/etc/config/application.yaml

然后通过 Kubernetes 部署配置创建 ConfigMap 卷并将其安装在该路径上.在我们的示例中,路径为 /etc/config.

让我知道它是否适合你:)

New to k8s.

Trying to read values from profile based config map. My configmap exists in default namespace. But, spring boot is not pickingup the values.

Config map looks like:

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

The config map is created in default namespace too.

kubectl get configmap -n default 

NAME                                  DATA   AGE
example-configmap-overriding-new-01   3      8d

My deployment file looks like

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

But the spring boot log says:-

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}"**

My spring boot config file looks like

@Configuration
public class ConfigConsumerConfig {

    @Value(value = "${globalkey}")
    private String globalkey;

    // with getter and setters 
}

My pom.xml has the following dependency too.

    <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>

I am running minikube in my local machine. Am I missing something here?

Could someone share some inputs here.

解决方案

spring-cloud-kubernetes doesn't have access to the Kubernetes API so it can't read the configMap. Check this docs for more details: https://github.com/spring-cloud/spring-cloud-kubernetes/blob/master/docs/src/main/asciidoc/security-service-accounts.adoc.

In short, apply this configuration and it will work fine:

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: ""

You can read about Roles and RoleBinding in more detail here: https://kubernetes.io/docs/reference/access-authn-authz/rbac/.

Note: You don't have to create volumes and volume mounts. I would say it is an alternative for it. If you would like to have it this way then you have to specify spring.cloud.kubernetes.config.paths in the spring boot application configuration (I have written it into bootstrap.yaml resource file). E.g.

spring: 
  cloud:
    kubernetes:
      config:
        paths: /etc/config/application.yaml

And then via Kubernetes Deployment configuration create ConfigMap volume and mount it on that path. In our example the path would be /etc/config.

Let me know if it works for you :)

这篇关于无法读取名称为 [xx] in namespace ['default'] 的 configmap 忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆