内容简介:让我们以前的Spring Boot Kubernetes示例首先让我们将简单的启动属性添加到MVC控制器,没什么大不了的:
让我们以前的Spring Boot Kubernetes示例 Spring Boot Kubernetes Deploy 作为入门者并将 Kubernetes Config Map 添加到游戏中。
Spring Boot应用程序属性
首先让我们将简单的启动属性添加到MVC控制器,没什么大不了的:
@RestController
<b>public</b> <b>class</b> ControllerMVC {
@Value(<font>"${my.system.property:defaultValue}"</font><font>)
<b>protected</b> String fromSystem;
@RequestMapping(</font><font>"/sayhello"</font><font>)
<b>public</b> String mvcTest() {
<b>return</b> </font><font>"I'm saying hello to Kubernetes with system property "</font><font>+fromSystem+</font><font>" !"</font><font>;
}
}
</font>
在application.properties中定义my.system.property:
server.port=8081 my.system.property=fromFile
测试该属性是否已加载可运行:
- mvn clean install
- $ java -jar target/demo-0.0.1-SNAPSHOT.jar
- $ curl http://localhost:8081/sayhello
输出应该是:
I'm saying hello to Kubernetes with system property fromFile
在Spring Boot中使用Kubernetes ConfigMap
在Spring Boot中使用Kubernetes配置映射的好方法是通过环境变量。因此,让我们使用configmap app-config 中的值定义环境变量my.system.property,以覆盖application.properties中的值:
首先让我们在configmap app-config使用键“my.system.property”和值“fromAnsible”定义:
kubectl create configmap app-config --from-literal=my.system.property=updatedFromAnsible
要查看configmap是否定义良好,请运行:
kubectl describe configmap app-config
输出应该是:
Name: app-config Namespace: <b>default</b> Labels: none Annotations: none Data ==== my.system.property: ---- updatedFromAnsible Events: none
好的,configmap已准备就绪,让kubernetes知道它并定义环境变量my.system.property,它从configmap app-config读取同名的密钥。
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-app
spec:
replicas: 1
template:
metadata:
labels:
app: test-app
spec:
containers:
- name: test-app
image: test-controller:1.0-SNAPSHOT
env:
# Define the environment variable
- name: my.system.property
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to my.system.property
name: app-config
# Specify the key associated with the value
key: my.system.property
ports:
- containerPort: 8081
现在使用kubernetes服务将部署清单上传到kubernetes
(这是所需的输出,如何上传请参阅 上一个repo ):
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-minikube 1 1 1 1 26d
test-app 1 1 1 1 2h
tomask79:kubernetes-configmap tomask79$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-6c47c66d8-gzvgc 1/1 Running 5 26d
test-app-745ff9546c-hrswc 1/1 Running 0 1h
tomask79:kubernetes-configmap tomask79$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 26d
test-app NodePort 10.105.171.8 <none> 8081:30615/TCP 2h
kubernetes服务运行于:
$ minikube service test-app --url http:<font><i>//192.168.99.100:30615</i></font><font> </font>
访问这个url返回:
$ curl http:<font><i>//192.168.99.100:30615/sayhello</i></font><font> I'm saying hello to Kubernetes with system property updatedFromAnsible ! </font>
是的,我们通过将属性作为环境变量注入,通过Kubernetes configmap覆盖了spring属性my.system.property ,因为env.variables 在Spring Boot中具有更高的优先级 ,然后才会启用application.property文件的内容。
更新configMap
让我们稍微玩一下configmap吧。如果我们想要更改Map值,该怎么办? 最简单的方法是删除map并创建一个新map。
$ kubectl delete configmap app-config configmap <font>"app-config"</font><font> deleted $ kubectl create configmap app-config --from-literal=my.system.property=changedValue configmap </font><font>"app-config"</font><font> created $ tomask79:kubernetes-configmap tomask79$ kubectl delete configmap app-config </font>
现在再次curl 这个服务:
$ curl http:<font><i>//192.168.99.100:30615/sayhello</i></font><font> I'm saying hello to Kubernetes with system property updatedFromAnsible ! </font>
configmap的新值没有反映出来,我们仍然是旧的。
要查看configmap的实际值,我们需要重新启动POD。由于我们使用部署,其中要求kubernetes始终运行一个副本,我们只需要删除POD。新实例 最终会运行。
$ kubectl delete pod test-app-745ff9546c-hrswc pod <font>"test-app-745ff9546c-hrswc"</font><font> deleted tomask79:kubernetes-configmap tomask79$ kubectl get pods NAME READY STATUS RESTARTS AGE hello-minikube-6c47c66d8-gzvgc 1/1 Running 5 27d test-app-745ff9546c-t92hb 1/1 Running 0 31s tomask79:kubernetes-configmap tomask79$ curl http:</font><font><i>//192.168.99.100:30615/sayhello</i></font><font> I'm saying hello to Kubernetes with system property changedValue ! </font>
我们看到实际新的值!
点击标题查看原文
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Android里应用程序,应用程序窗口和视图对象之间的关系
- 这几天在C程序中有哪些应用程序?
- 在iOS应用程序中登录系统使用的标准程序是什么?
- 使用 Bluemix、Watson Discovery 和 Cloudant 构建移动应用程序来分析其他应用程序
- 用Visual Studio的.NET程序员开发dapp应用程序
- 程序员疯狂记事:如何利用众多技术栈构建一个 Web 应用程序?!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
React 进阶之路
徐超 / 清华大学出版社 / 2018-4 / 69.00元
《React进阶之路》详细介绍了React技术栈涉及的主要技术。本书分为基础篇、进阶篇和实战篇三部分。基础篇主要介绍React的基本用法,包括React 16的新特性;进阶篇深入讲解组件state、虚拟DOM、高阶组件等React中的重要概念,同时对初学者容易困惑的知识点做了介绍;实战篇介绍React Router、Redux和MobX 3个React技术栈的重要成员,并通过实战项目讲解这些技术如......一起来看看 《React 进阶之路》 这本书的介绍吧!