让我们以前的Spring Boot Kubernetes示例Spring Boot Kubernetes Deploy 作为入门者并将Kubernetes Config Map添加到游戏中。
Spring Boot应用程序属性
首先让我们将简单的启动属性添加到MVC控制器,没什么大不了的:
@RestController public class ControllerMVC { @Value("${my.system.property:defaultValue}") protected String fromSystem; @RequestMapping("/sayhello") public String mvcTest() { return "I'm saying hello to Kubernetes with system property "+fromSystem+" !"; } } |
在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: default 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://192.168.99.100:30615 |
访问这个url返回:
$ curl http://192.168.99.100:30615/sayhello I'm saying hello to Kubernetes with system property updatedFromAnsible ! |
是的,我们通过将属性作为环境变量注入,通过Kubernetes configmap覆盖了spring属性my.system.property ,因为env.variables 在Spring Boot中具有更高的优先级 ,然后才会启用application.property文件的内容。
更新configMap
让我们稍微玩一下configmap吧。如果我们想要更改Map值,该怎么办? 最简单的方法是删除map并创建一个新map。
$ kubectl delete configmap app-config configmap "app-config" deleted $ kubectl create configmap app-config --from-literal=my.system.property=changedValue configmap "app-config" created $ tomask79:kubernetes-configmap tomask79$ kubectl delete configmap app-config |
现在再次curl 这个服务:
$ curl http://192.168.99.100:30615/sayhello I'm saying hello to Kubernetes with system property updatedFromAnsible ! |
configmap的新值没有反映出来,我们仍然是旧的。
要查看configmap的实际值,我们需要重新启动POD。由于我们使用部署,其中要求kubernetes始终运行一个副本,我们只需要删除POD。新实例 最终会运行。
$ kubectl delete pod test-app-745ff9546c-hrswc pod "test-app-745ff9546c-hrswc" 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://192.168.99.100:30615/sayhello I'm saying hello to Kubernetes with system property changedValue ! |
我们看到实际新的值!
点击标题查看原文
猜你喜欢