Spring boot Spring Cloud Kubernetes ConfigMap重新加载不工作

Spring boot Spring Cloud Kubernetes ConfigMap重新加载不工作,spring-boot,kubernetes,minikube,configmap,spring-cloud-kubernetes,Spring Boot,Kubernetes,Minikube,Configmap,Spring Cloud Kubernetes,我在Minikube和Kubernetes玩。我可以将spring引导示例应用程序部署到Kubernetes中 我正在探索库伯内特斯地图。我可以使用SpringCloudStarter成功运行SpringBoot应用程序,并从配置映射中选择属性键。在这之前,我是成功的 我目前面临的问题是configmap重新加载 这是我的配置图: ConfigMap.yaml apiVersion: v1 kind: ConfigMap metadata: name: minikube-sample

我在Minikube和Kubernetes玩。我可以将spring引导示例应用程序部署到Kubernetes中

我正在探索库伯内特斯地图。我可以使用SpringCloudStarter成功运行SpringBoot应用程序,并从配置映射中选择属性键。在这之前,我是成功的

我目前面临的问题是configmap重新加载

这是我的配置图:

ConfigMap.yaml

 apiVersion: v1
kind: ConfigMap
metadata:
  name: minikube-sample
  namespace: default
data:
  app.data.name: name
  application.yml: |-
    app:
      data:
        test: test
management:
    endpoint:
        health:
            enabled: true
        info:
            enabled: true
        restart:
            enabled: true
spring:
    application:
        name: minikube-sample
    cloud:
        kubernetes:
            config:
                enabled: true
                name: ${spring.application.name}
                namespace: default
            reload:
                enabled: true
apiVersion: apps/v1
kind: Deployment
metadata:
  name: minikube-sample
  namespace: default
spec:
  selector:
      matchLabels:
        app: minikube-sample

  replicas: 1
  template:
    metadata:
      labels:
        app: minikube-sample
    spec:
      containers:
        - name: minikube-sample
          image: minikube-sample:latest
          imagePullPolicy: Never
          ports:
            - containerPort: 8080
          env:
            - name: env.namespace
              value: default
          volumeMounts:
            - name: config
              mountPath: /config
      volumes:
        - name: config
          configMap:
            name: minikube-sample
bootstrap.yaml

 apiVersion: v1
kind: ConfigMap
metadata:
  name: minikube-sample
  namespace: default
data:
  app.data.name: name
  application.yml: |-
    app:
      data:
        test: test
management:
    endpoint:
        health:
            enabled: true
        info:
            enabled: true
        restart:
            enabled: true
spring:
    application:
        name: minikube-sample
    cloud:
        kubernetes:
            config:
                enabled: true
                name: ${spring.application.name}
                namespace: default
            reload:
                enabled: true
apiVersion: apps/v1
kind: Deployment
metadata:
  name: minikube-sample
  namespace: default
spec:
  selector:
      matchLabels:
        app: minikube-sample

  replicas: 1
  template:
    metadata:
      labels:
        app: minikube-sample
    spec:
      containers:
        - name: minikube-sample
          image: minikube-sample:latest
          imagePullPolicy: Never
          ports:
            - containerPort: 8080
          env:
            - name: env.namespace
              value: default
          volumeMounts:
            - name: config
              mountPath: /config
      volumes:
        - name: config
          configMap:
            name: minikube-sample
家庭控制器:

package com.minikube.sample.rest.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.minikube.sample.properties.PropertiesConfig;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Gorantla, Eresh
 * @created 06-12-2018
 */
@RestController
@RequestMapping("/home")
public class HomeResource {

    @Autowired
    PropertiesConfig config;

    @GetMapping("/data")
    public ResponseEntity<ResponseData> getData() {
        ResponseData responseData = new ResponseData();
        responseData.setId(1);
        responseData.setName(config.getName());
        responseData.setPlace("Hyderabad");
        responseData.setValue(config.getTest());
        return new ResponseEntity<>(responseData, HttpStatus.OK);
    }

    @Getter
    @Setter
    public class ResponseData {
        private String name;
        private Integer id;
        private String place;
        private String value;
    }
}
我使用@ConfigurationProperties重新加载属性

依赖关系

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes</artifactId>
            <version>1.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes-config</artifactId>
            <version>1.1.0.RELEASE</version>
        </dependency>
但是,当我在kubernetes中更新configmap时,不会动态地重新加载属性。 我怀疑clusterrolebinding有问题。
请提供您的想法。感谢您的帮助

部署没有配置
服务帐户名
,因此它使用
默认
服务帐户。但是,问题中的命令-
kubectl create clusterrolebinding--serviceaccount=default:minikube…
-用于
default
命名空间中名为
minikube
的帐户

此外,当命名空间的
rolebinding
可以工作时,创建
clusterrolebinding
可能“太多”

由于部署是针对
default
命名空间(
metadata.namespace:default
),这应该创建一个适当的
rolebinding
,以向
default
帐户授予只读权限:

kubectl create rolebinding default-sa-view \
  --clusterrole=view \
  --serviceaccount=default:default \
  --namespace=default

有关参考,请参阅

谢谢你的回答。rolebinding在命名空间中具有role视图就足以使configmap在容器中可用

我通过更新依赖项解决了这个问题。带有2.1.8.Release的Spring引导版本和Spring的kubernetes 1.1.0.Release版本不适合我。我怀疑添加了许多依赖项。我清理了pom文件,效果很好

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.minikube.sample</groupId>
    <artifactId>kubernetes-configmap-reload</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>minikube-sample</name>
    <description>Demo project for Spring Cloud Kubernetes</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes-config</artifactId>
            <version>1.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

4.0.0
org.springframework.boot

谢谢
要访问ConfigMaps并获取刷新事件,请执行以下操作:

  • 请参阅配置属性类上的注释
    @配置(proxyBeanMethods=false)
    另请参见配置属性类上的
    @RefreshScope

    @Configuration(proxyBeanMethods = false)
    @ConfigurationProperties(prefix = "bean")
    @RefreshScope
    public class ClientConfig {
    
     private String message = "Default Message from java code - to be overwritten from config";
    
     public String getMessage() {...
     public void setMessage(String message) {...
    }
    
  • 2添加访问ConfigMaps的权限

    kubectl create -f perm.yaml -n <NAMESPACE>
    
    • 创建权限后,部署POD和服务

    • 修改配置映射时,您将在pod日志中看到一个刷新事件

      EventBasedConfigurationChangeDetector - Detected change in config maps
      EventBasedConfigurationChangeDetector - Reloading using strategy: REFRESH
      PropertySourceBootstrapConfiguration - Located property source: [BootstrapPropertySource {name='bootstrapProperties-configmap.client-svc.myns'}]
      SpringApplication - The following profiles are active: kubernetes
      

    到yl时

    您有任何错误吗?您使用的是什么K8s版本?对于我来说,设置访问ConfigMaps的权限(在我的情况下是默认名称空间),正如在这个回答中所解释的,成功了!