gpt4 book ai didi

spring-boot - Spring Cloud Kubernetes ConfigMap 重新加载不起作用

转载 作者:行者123 更新时间:2023-12-02 11:39:11 26 4
gpt4 key购买 nike

我在 Minikube 中使用 Kubernetes。我可以将 Spring Boot 示例应用程序部署到 Kubernetes 中。

我正在探索 Kubernetes configMap。我可以使用 Spring Cloud 启动器成功运行 Spring Boot 应用程序并从配置映射中选择属性键。到这里我就成功了。

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

这是我的配置图:

ConfigMap.yaml

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

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

家庭 Controller :
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 文档。 “需要服务帐户的 View 角色才能监听配置映射更改。”
然后我通过以下命令创建了集群 View 角色
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 时,属性不会即时重新加载。
我怀疑集群角色绑定(bind)有问题。
请提供您的想法。任何帮助表示赞赏。

最佳答案

部署没有 serviceAccountName配置为使用 default服务帐户。然而,问题中的命令 - kubectl create clusterrolebinding ... --serviceaccount=default:minikube... - 用于名为 minikube 的帐户在 default命名空间。

此外,创建 clusterrolebinding rolebinding 时可能“太多”因为命名空间会起作用。

部署用于 default命名空间( metadata.namespace: default ),这应该创建一个正确的 rolebinding授予 default 的只读权限帐户:

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

如需引用,请参阅 Using RBAC Authorization .

关于spring-boot - Spring Cloud Kubernetes ConfigMap 重新加载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59379679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com