spring boot 在 k8s 使用 kubernets configmap作为配置中心
引用& 参考
https://amit4aws.medium.com/spring-cloud-kubernetes-configwatcher-7a206fe269e3
https://github.com/spring-cloud/spring-cloud-kubernetes#kubernetes-propertysource-implementations
spring boot 服务程序读取 kubernets configmap 配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
<version>1.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Spring boot 版本使用:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.11.RELEASE</version>
创建项目配置文件 bootstrap.yml
spring:
application:
name: projectName
cloud:
kubernetes:
reload:
enabled: true
strategy: refresh
mode: event
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
restart:
enabled: true
主要说明:
(1)
spring.cloud.kubernetes.reload.enabled=true
需要打开刷新功能;(2) 加载策略
strategy
:
refresh
:只对特定的配置生效,有注解@ConfigurationProperties
或@RefreshScope
。restart_context
:整个Spring Context
会优雅重启,里面的所有配置都会重新加载。(考虑是否可在线使用此策略)需要打开
actuator endpoint
,所以要配置management.endpoint
。actuator依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
shutdown
:重启容器。(3)模式
mode
- 事件
Event
:会通过k8s API
监控ConfigMap
的变更,读取配置并生效。Polling
:定期查看是否有变化,有变化则触发,默认为15秒。
以上配置 发现 并为配置 从何处的configmap读取, 是通过 Spring Cloud Kubernetes Configuration Watcher Controller 可以实现监听configmap的变化后重新加载应用程序上下文和订阅哪些ConfigMap事件以及需要更新哪些服务
Spring Cloud Kubernetes Configuration Watcher Controller (扩展,可忽略实际已部署在k8s集群下)
一、k8s 下部署
apiVersion: v1
kind: ServiceAccount
metadata:
name: ns-admin
namespace: ns
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: ns-admin-role
namespace: ns
rules:
- apiGroups:
- ""
resources:
- namespaces
- endpoints
- services
- nodes
- nodes/proxy
- pods
- configmaps
verbs:
- list
- get
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: ns-admin-role-binding
namespace: ns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: ns-admin-role
subjects:
- kind: ServiceAccount
name: ns-admin
namespace: ns
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-cloud-kubernetes-configuration-watcher-deployment
namespace: ns
spec:
selector:
matchLabels:
app: spring-cloud-kubernetes-configuration-watcher
template:
metadata:
labels:
app: spring-cloud-kubernetes-configuration-watcher
spec:
serviceAccountName: ns-admin
containers:
- name: spring-cloud-kubernetes-configuration-watcher
image: springcloud/spring-cloud-kubernetes-configuration-watcher:2.0.2
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
port: 8888
path: /actuator/health/readiness
livenessProbe:
httpGet:
port: 8888
path: /actuator/health/liveness
ports:
- containerPort: 8888
Spring Cloud Kubernetes Configuration Watcher will react to changes in ConfigMaps with a label of
spring.cloud.kubernetes.config
with the valuetrue
or any Secret with a label ofspring.cloud.kubernetes.secret
with the valuetrue
. If the ConfigMap or Secret does not have either of those labels or the values of those labels is nottrue
then any changes will be ignored.The labels Spring Cloud Kubernetes Configuration Watcher looks for on ConfigMaps and Secrets can be changed by setting
spring.cloud.kubernetes.configuration.watcher.configLabel
andspring.cloud.kubernetes.configuration.watcher.secretLabel
respectively.If a change is made to a ConfigMap or Secret with valid labels then Spring Cloud Kubernetes Configuration Watcher will take the name of the ConfigMap or Secret and send a notification to the application with that name.
The HTTP implementation is what is used by default. When this implementation is used Spring Cloud Kubernetes Configuration Watcher and a change to a ConfigMap or Secret occurs then the HTTP implementation will use the Spring Cloud Kubernetes Discovery Client to fetch all instances of the application which match the name of the ConfigMap or Secret and send an HTTP POST request to the application’s actuator
/refresh
endpoint. By default it will send the post request to/actuator/refresh
using the port registered in the discovery client.
配置configmap
通过上述可以得知 按照默认configmap 配置 需要加上label 标签 spring.cloud.kubernetes.config = ture
并且在同一namespace下与服务同名
apiVersion: v1
kind: ConfigMap
metadata:
name: projectName
namespace: ns
labels:
spring.cloud.kubernetes.config: "true"
data:
application.yml: |-
server:
port: 80
spring:
datasource:
# 使用阿里的Druid连接池
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
# 填写你数据库的url、登录名、密码和数据库名
url: url
username: username
password: password
druid:
# 连接池的配置信息
# 初始化大小,最小,最大
initial-size: 10
min-idle: 10
maxActive: 30
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 180000
If the values are set to true then Configuration Watcher will take the name of the ConfigMap or Secret and will send a notification to the application available with the same name.
应用如何应用自动刷新配置
自动重启应用
修改 bootstrap.yml
spring:
cloud:
kubernetes:
reload:
strategy: restart_context
整个Spring Context
会优雅重启,里面的所有配置都会重新加载。
热更新配置
spring:
cloud:
kubernetes:
reload:
strategy: refresh
只对特定的配置生效,有注解@ConfigurationProperties
或 @RefreshScope
以durid数据库连接为例
@Configuration
@Slf4j
@RefreshScope
public class DataSourceConfig {
@Bean
@RefreshScope
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
log.info("Init DruidDataSource");
return new DruidDataSourceBuilder().build();
}
}
todo : 采用策略