Spring Cloud Kubernetes - ConfigMap使用

一、ConfigMap

加载思路:

创建configmap,挂载配置文件为application.yml,最好指定profile

加载bootstrap配置 -> 指定profile -> 寻找对应profile挂载的文件 -> pooling or event

Service改造

1、引入依赖

 
 

bash

复制代码

## 接入 kubernetes configmap implementation ('org.springframework.cloud:spring-cloud-starter-kubernetes-client-config:2.1.4') ## 热更新 implementation("org.springframework.boot:spring-boot-actuator") implementation("org.springframework.boot:spring-boot-actuator-autoconfigure")

2、新增bootstrap.yml文件

根目录下新增bootstrap.yml文件

 
 

yaml

复制代码

server: port: 8080 spring: application: name: server cloud: kubernetes: enabled: false

本地开发时,指定spring.cloud.kubernetes.enabled=false,关闭kubernetes配置

3、修改各环境配置文件

修改dist目录下的各个配置文件为bootstrap.yml,同时修改配置,以test环境为例

 
 

yaml

复制代码

server: port: 8080 management: endpoint: restart: enabled: true health: enabled: true info: enabled: true spring: application: name: server cloud: kubernetes: reload: enabled: true mode: event config: sources: - name: demo-configmap namespace: test profiles: active: k8s-test

K8S部署

1、创建ConfigMap

kubectl create configmap demo-configmap --from-file=application.yml -n test

 
 

yaml

复制代码

server: port: 8080 # servlet.context-path: /${spring.application.name} spring: profiles: k8s-test mvc: pathmatch: matching-strategy: ant_path_matcher application: name: server cloud: kubernetes: config: sources: - name: demo-comfigmap namespace: test reload: enabled: true mode: event logging: config: classpath:logback.xml endpoint: restart: enabled: true health: enabled: true info: enabled: true

2、创建ServiceAccount

创建Role

 
 

vbnet

复制代码

apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: test name: config-reader rules: - apiGroups: [""] resources: ["pods","configmaps","services","endpoints"] verbs: ["get", "watch", "list"]

创建ServiceAccount

 
 

yaml

复制代码

apiVersion: v1 kind: ServiceAccount metadata: name: config-reader namespace: test

绑定Role和ServiceAccount

 
 

yaml

复制代码

apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: config-reader namespace: test roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: config-reader subjects: - kind: ServiceAccount name: config-reader namespace: test

3、创建Deployment,指定ConfigMap、ServiceAccount

 
 

arduino

复制代码

serviceAccountName: config-reader serviceAccount: config-reader

为什么使用bootstrap.yml,参考:www.jianshu.com/p/c955c44ae…

即系统级别的配置信息放在bootstrap.yml中,自定义的配置信息放在application.yml中。

建议

系统级别以及不可变的配置放在项目代码中,不做更改

可变的、自定义的配置放在configmap中做热更新

猜你喜欢

转载自blog.csdn.net/BASK2312/article/details/131662239