#yyds干货盘点#spring-cloud-kubernetes自动同步k8s的configmap更新

网友投稿 281 2022-09-12

#yyds干货盘点#spring-cloud-kubernetes自动同步k8s的configmap更新

欢迎访问我的GitHub

提前小结和上一篇的差异

要达到实时同步configmap变更的效果,需要将上一章的应用作以下改动:

增加以下两个jar依赖: org.springframework.boot spring-boot-actuator org.springframework.boot spring-boot-actuator-autoconfigure bootstrap.yml增加以下配置: management: endpoint: restart: enabled: true health: enabled: true info: enabled: true bootstrap.yml中的spring.cloud.kubernetes节点下增加子节点reload的配置,完整的bootstrap.yml内容如下: management: endpoint: restart: enabled: true health: enabled: true info: enabled: true spring: application: name: springcloudk8sconfigdemo profiles: active: development cloud: kubernetes: reload: #自动更新配置的开关设置为打开 enabled: true #更新配置信息的模式是主动拉取 mode: polling #主动拉取的间隔时间是500毫秒 period: 500 config: sources: - name: ${spring.application.name} namespace: default 在controller中增加path为==/health==的服务响应,在k8s部署时,健康和就绪探针会调用此接口,如果没有响应,pod就无法正常使用: @GetMapping("/health") public String health() { return "success"; } 以上就是开启自动更新的步骤了,您基于上一章的源码做上述更改即可,也可以随同本文一起重新开发一个全新应用,来实现获取configmap的配置,并且实时同步configmap的变化;

环境信息

本次实战的环境和版本信息如下:

操作系统:CentOS Linux release 7.6.1810 minikube:1.1.1 Java:1.8.0_191 Maven:3.6.0 fabric8-maven-plugin插件:3.5.37 spring-cloud-kubernetes:1.0.1.RELEASE springboot:2.1.6.RELEASE

准备完毕,可以开始实战啦!

源码下载

如果您不打算写代码,也可以从GitHub上下载本次实战的源码,地址和链接信息如下表所示:

名称 链接 备注
项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议


开发Java应用

通过maven创建名为springcloudk8sreloadconfigdemo的springboot工程,pom.xml内容如下,要注意的是新增了依赖==spring-cloud-starter-kubernetes-config==、==spring-boot-actuator==、==spring-boot-actuator-autoconfigure==,这是本次实战的重点: 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE com.bolingcavalry springcloudk8sreloadconfigdemo 0.0.1-SNAPSHOT springcloudk8sreloadconfigdemo Demo project for Spring Cloud Kubernetes with Kubernetes ConfigMap,Change of configmap is reloadable 1.8 2.1.6.RELEASE false false false 3.5 2.8.2 2.18.1 2.21.0 3.5.37 1.0.1.RELEASE Greenwich.SR2 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-actuator org.springframework.boot spring-boot-actuator-autoconfigure org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-kubernetes-config ${springcloud.kubernetes.version} org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin ${spring-boot.version} repackage org.apache.maven.plugins maven-deploy-plugin ${maven-deploy-plugin.version} true org.apache.maven.plugins maven-surefire-plugin ${maven-surefire-plugin.version} true false io.fabric8 fabric8-maven-plugin ${fabric8.maven.plugin.version} fmp resource kubernetes io.fabric8 fabric8-maven-plugin ${fabric8.maven.plugin.version} fmp resource build NodePort 项目的src\main\resources路径下不要创建application.yml文件,只创建名为bootstrap.yml的文件,内容如下: management: endpoint: restart: enabled: true health: enabled: true info: enabled: true spring: application: name: springcloudk8sreloadconfigdemo cloud: kubernetes: reload: #自动更新配置的开关设置为打开 enabled: true #更新配置信息的模式:polling是主动拉取,event是事件通知 mode: polling #主动拉取的间隔时间是500毫秒 period: 500 config: sources: - name: ${spring.application.name} namespace: default 可见新增了配置项spring.cloud.kubernetes.reload和spring.cloud.kubernetes.config,前者用于开启自动更新配置,执行更新模式为500毫秒拉取一次,后者指定配置来源于kubernetes的哪个namespace下的哪个configmap; 增加一个配置类DummyConfig.java,注解ConfigurationProperties的prefix="greeting"表示该类用到的配置项都是名为"greeting"的配置项的子内容 : package com.bolingcavalry.springcloudk8sreloadconfigdemo;

import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;

/**

@Description: 配置类,此处可以加载配置文件中的内容 @author: willzhao E-mail: zq2599@gmail.com @date: 2019/7/27 18:24br/>*/@Configuration@ConfigurationProperties(prefix = "greeting")public class DummyConfig { private String message = "This is a dummy message"; public String getMessage() {return this.message;} public void setMessage(String message) {this.message = message;}} 4. 启动类Springcloudk8sreloadconfigdemoApplication.java,简单起见,将用于验证配置项是否生效的web接口也写在了这里面,即hello方法 ,这个方法是应用的关键,方法内会返回配置文件的值,我们的应用能否成功取得k8s的configmap的配置文件,通过此方法的返回值就能验证了,还要增加path为==/health==的方法,因为在k8s部署时健康探针和就绪探针会调用此接口,如果没有响应pod就无法正常使用: ```java package com.bolingcavalry.springcloudk8sreloadconfigdemo;

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;import java.util.Date;

@SpringBootApplicationbr/>@RestController@EnableConfigurationProperties(DummyConfig.class)public class Springcloudk8sreloadconfigdemoApplication {

@Autowired private DummyConfig dummyConfig; @GetMapping("/health") public String health() { return "success"; } @GetMapping("/hello") public String hello() { return dummyConfig.getMessage() + " [" + new SimpleDateFormat().format(new Date()) + "]"; } public static void main(String[] args) { SpringApplication.run(Springcloudk8sreloadconfigdemoApplication.class, args); }

}

- 以上就是实战工程的所有代码了,仅仅只是引入了少量jar依赖,以及在启动配置文件中指定了configmap的信息和同步模式,即完成了获取配置文件的所有操作,至于代码中用到配置文件的地方,和使用SpringCloud Config并无差别。 ### 解决权限问题 我这里的是minikube,在部署了应用之后,默认的serviceaccount是没有权限访问K8S的API Server资源的,执行以下命令可以提升权限: ```shell kubectl create clusterrolebinding permissive-binding \ --clusterrole=cluster-admin \ --user=admin \ --user=kubelet \ --group=system:serviceaccounts

注意:以上办法只能用于开发和测试环境,不要用在生产环境,生产环境应参考Kubernetes的RBAC授权相关设置来处理,步骤如下:

创建role: apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: default name: pod-reader rules: - apiGroups: [""] resources: ["pods","configmaps"] verbs: ["get", "watch", "list"] 创建ServiceAccount: apiVersion: v1 kind: ServiceAccount metadata: name: config-reader namespace: default 绑定Role和ServiceAccount: apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: pod-reader namespace: default roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: pod-reader subjects: - kind: ServiceAccount name: config-reader namespace: default 在deployment中指定上面的ServiceAccount;

验证

现在进入验证阶段,验证步骤: 在kubernetes环境创建configmap; 再将springcloudk8sreloadconfigdemo在kubernetes部署和启动; 访问springcloudk8sreloadconfigdemo的http接口,验证应用能取得configmap中的配置; 修改configmap中的配置; 再次访问springcloudk8sreloadconfigdemo的http接口,看返回的配置内容是否是修改后的;

接下来,验证开始:

尝试另一种同步模式

欢迎关注51CTO博客:程序员欣宸

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:海伦司酒馆上市:蹭星巴克故事,做“瑞幸式”营销!
下一篇:k8s-day2-名词解释:master
相关文章

 发表评论

暂时没有评论,来抢沙发吧~