spring-cloud-kubernetes与k8s的configmap
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-plugin${spring-boot.version}repackageorg.apache.maven.pluginsmaven-deploy-pl
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
${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的文件,内容如下:
spring:
application:
name: springcloudk8sconfigdemo
cloud:
kubernetes:
config:
sources:
- name: ${spring.application.name}
namespace: default
可见新增了配置项spring.cloud.kubernetes.config.source.name和spring.cloud.kubernetes.config.source.namespace,用于配置信息来源于kubernetes的哪个namespace下的哪个configmap;
3. 增加一个配置类DummyConfig.java,注解ConfigurationProperties的prefix="greeting"表示该类用到的配置项都是名为"greeting"的配置项的子内容 :
package com.bolingcavalry.springcloudk8sconfigdemo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@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;
}
}
- 启动类Springcloudk8sconfigdemoApplication.java,简单起见,将用于验证配置项是否生效的web接口也写在了这里面,即hello方法 :
package com.bolingcavalry.springcloudk8sconfigdemo;
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;
@SpringBootApplication
@RestController
@EnableConfigurationProperties(DummyConfig.class)
public class Springcloudk8sconfigdemoApplication {
@Autowired
private DummyConfig dummyConfig;
@GetMapping(“/hello”)
public String hello() {
return dummyConfig.getMessage()
-
" ["
-
new SimpleDateFormat().format(new Date())
-
“]”;
}
public static void main(String[] args) {
SpringApplication.run(Springcloudk8sconfigdemoApplication.class, args);
}
}
以上就是实战工程的所有代码了,仅仅只是引入了spring-cloud-kubernetes-config的依赖,以及在启动配置文件中指定了configmap的信息,即完成了获取配置文件的所有操作,至于代码中用到配置文件的地方,和使用SpringCloud Config并无差别。
解决权限问题
我这里的是minikube,在部署了应用之后,默认的serviceaccount是没有权限访问K8S的API Server资源的,执行以下命令可以提升权限:
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,再将springcloudk8sconfigdemo在kubernetes部署和启动,通过springcloudk8sconfigdemo提供的http接口验证应用是否已经从configmap中取得指定的配置;
- 在kubernetes环境新建名为springcloudk8sconfigdemo.yml的文件,内容如下:
kind: ConfigMap
apiVersion: v1
metadata:
name: springcloudk8sconfigdemo
data:
application.yml: |-
greeting:
message: Say Hello to the World
farewell:
message: Say Goodbye
spring:
profiles: development
greeting:
message: Say Hello to the Developers
farewell:
message: Say Goodbye to the Developers
spring:
profiles: production
greeting:
message: Say Hello to the Ops
- 在springcloudk8sconfigdemo.yml文件所在目录执行以下命令,即可在kubernetes创建名为的configmap的资源:
kubectl apply -f springcloudk8sconfigdemo.yml
- 在springcloudk8sconfigdemo项目的pom.xml文件所在目录,执行以下命令,即可编译构建部署全部完成:
mvn clean install fabric8:deploy -Dfabric8.generator.from=fabric8/java-jboss-openjdk8-jdk -Pkubernetes
操作成功后的控制台信息如下:
[INFO] Installing /usr/local/work/k8s/springcloudk8sconfigdemo/target/classes/META-INF/fabric8/kubernetes.json to /root/.m2/repository/com/bolingcavalry/springcloudk8sconfigdemo/0.0.1-SNAPSHOT/springcloudk8sconfigdemo-0.0.1-SNAPSHOT-kubernetes.json
[INFO]
[INFO] <<< fabric8-maven-plugin:3.5.37:deploy (default-cli) < install @ springcloudk8sconfigdemo <<<
[INFO]
[INFO]
[INFO] — fabric8-maven-plugin:3.5.37:deploy (default-cli) @ springcloudk8sconfigdemo —
[INFO] F8: Using Kubernetes at https://192.168.121.133:8443/ in namespace default with manifest /usr/local/work/k8s/springcloudk8sconfigdemo/target/classes/META-INF/fabric8/kubernetes.yml
[INFO] Using namespace: default
[INFO] Updating a Service from kubernetes.yml
[INFO] Updated Service: target/fabric8/applyJson/default/service-springcloudk8sconfigdemo.json
[INFO] Using namespace: default
[INFO] Updating Deployment from kubernetes.yml
[INFO] Updated Deployment: target/fabric8/applyJson/default/deployment-springcloudk8sconfigdemo.json
[INFO] F8: HINT: Use the command kubectl get pods -w
to watch your pods start up
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.538 s
[INFO] Finished at: 2019-07-14T17:06:25+08:00
[INFO] ------------------------------------------------------------------------
- 如果您的环境也是minikube,可以执行以下命令查看服务地址:
minikube service springcloudk8sconfigdemo --url
得到服务地址是:http://192.168.121.133:31646
5. 浏览器访问地址:http://192.168.121.133:31646/hello ,得到响应如下图,可见已经从configmap取得了配置文件,并且加载成功:
修改profile
前面的实战没有指定springboot工程的profile,接下来指定profile为development,看能否加载到配置文件中指定的配置,如下图红框所示:
- 修改项目的src\main\resources路径下的bootstrap.yml文件,增加配置项spring.profiles.active,修改后的完整内容如下:
Java高频面试专题合集解析:
当然在这还有更多整理总结的Java进阶学习笔记和面试题未展示,其中囊括了Dubbo、Redis、Netty、zookeeper、Spring cloud、分布式、高并发等架构资料和完整的Java架构学习进阶导图!
更多Java架构进阶资料展示
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
Gk,shadow_10,text_aHR0cHM6Ly94aW5jaGVuLmJsb2cuY3Nkbi5uZXQ=,size_16,color_FFFFFF,t_70)
- 修改项目的src\main\resources路径下的bootstrap.yml文件,增加配置项spring.profiles.active,修改后的完整内容如下:
Java高频面试专题合集解析:
[外链图片转存中…(img-eoFiZRdl-1714753410994)]
当然在这还有更多整理总结的Java进阶学习笔记和面试题未展示,其中囊括了Dubbo、Redis、Netty、zookeeper、Spring cloud、分布式、高并发等架构资料和完整的Java架构学习进阶导图!
[外链图片转存中…(img-kuWYxqzB-1714753410995)]
更多Java架构进阶资料展示
[外链图片转存中…(img-ELLtO79I-1714753410995)]
[外链图片转存中…(img-TbLl0uVS-1714753410996)]
[外链图片转存中…(img-yk3pxchW-1714753410996)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
更多推荐
所有评论(0)