-->

Cannot read configmap with name: [xx] in namespace

2019-08-18 07:40发布

问题:

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.

回答1:

the configmap manifest needs to be corrected. the format is incorrect

try this

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

you should mount configmap as volume to use the config file in your application

          volumeMounts:
          - name: config
            mountPath: /config
        volumes:
        - name: config
          configMap:        
            name: example-configmap-overriding-new-01


回答2:

Can you please try to mount your configmap also, may be that can help

volumeMounts:
- mountPath: /app/config
  name: example-configmap-overriding-new-01

volumes:
- name: example-configmap-overriding-new-01
  configMap:
      name: example-configmap-overriding-new-01

Let me know please if it works. Thanks

Corrected syntax mistake