Spring Cloud Kubernetes配置Map重新加载不工作

gjmwrych  于 2022-11-02  发布在  Kubernetes
关注(0)|答案(3)|浏览(143)

我在Minikube中使用Kubernetes。我可以将Sping Boot 示例应用程序部署到Kubernetes中。
我正在探索Kubernetes配置Map。我可以成功地运行一个Spring Boot 应用程序与 Spring 云启动器和挑选属性键从配置Map。直到这里,我是成功的。
我目前面临的问题是重新加载配置Map。
以下是我的配置图:

配置Map.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: minikube-sample
  namespace: default
data:
  app.data.name: name
  application.yml: |-
    app:
      data:
        test: test

引导程序.yaml

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

家庭控制器:

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

部署.yaml

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

我使用了@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>

**我做了什么?我已经阅读了Spring Cloud文档。“需要服务帐户上的视图角色才能侦听配置Map更改。"**然后我通过以下命令创建了群集视图角色

C:\Users\eresh.gorantla\apps\minikube-sample\src\main\fabric8 (master -> origin)
λ kubectl create clusterrolebinding minikube-sample --clusterrole=view --serviceaccount=default:minikube --namespace=default
clusterrolebinding.rbac.authorization.k8s.io/minikube-sample created

但是当我在kubernetes中更新configmap时,属性并没有被动态地重新加载。我怀疑clusterrolebinding中有什么问题。请提供您的想法。任何帮助都将不胜感激。

n9vozmp4

n9vozmp41#

部署没有配置serviceAccountName,因此它使用default服务帐户。但是,问题中的命令-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

有关参考信息,请参见Using RBAC Authorization

cvxl0en2

cvxl0en22#

要访问ConfigMaps并获取刷新事件,请执行以下操作:
1.请参阅配置属性类@Configuration(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>

其中perm.yaml为:

kind: Role
  apiVersion: rbac.authorization.k8s.io/v1
  metadata:
    namespace: yldbg
    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: yldbg
  subjects:
  - kind: ServiceAccount
    name: default
    apiGroup: ""
  roleRef:
    kind: Role
    name: namespace-reader
    apiGroup: ""
  • 创建权限后,部署Pod和服务。
  • 修改配置Map时,您将在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

eh57zj3b

eh57zj3b3#

感谢gears的回答。rolebinding在命名空间中使用role视图就足够了,可以在容器中使用configmap。
我解决了更新依赖项的问题。Sping Boot 版本2.1.8.Release和spring-cloud-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>

您可以在此处找到存储库链接--https://github.com/ereshzealous/kubernetes-configmap-reload
谢谢你,艾瑞什

相关问题