我在Minikube中与Kubernetes一起玩。我可以将spring boot示例应用程序部署到Kubernetes中。
我正在探索Kubernetes configMap。我可以使用Spring Cloud Starter成功运行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
HomeController:
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;
}
}
deployment.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角色,才能侦听配置映射更改。” 然后我通过下面的命令创建了集群视图角色
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时,这些属性不会立即重新加载。 我怀疑簇绑定中有问题。 请提供您的想法。任何帮助表示赞赏。
答案 0 :(得分:1)
要访问 ConfigMaps 并获取刷新事件:
查看配置属性类的注释
@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 和服务。
修改配置映射时,您将在 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
by y
答案 1 :(得分:0)
该部署未配置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。
答案 2 :(得分:0)
感谢您的回答。角色绑定对于命名空间中的角色视图就足够了,可以在容器中使用configmap。
我通过更新依赖关系解决了这个问题。带有2.1.8.Spring发行版的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>
您可以在此处找到存储库链接-https://github.com/ereshzealous/kubernetes-configmap-reload
谢谢 讨厌