<video id="xrz79"><output id="xrz79"><delect id="xrz79"></delect></output></video>
<video id="xrz79"><dl id="xrz79"></dl></video>
<dl id="xrz79"><delect id="xrz79"><meter id="xrz79"></meter></delect></dl>
<dl id="xrz79"></dl>
<dl id="xrz79"><output id="xrz79"><font id="xrz79"></font></output></dl>
<dl id="xrz79"><output id="xrz79"><font id="xrz79"></font></output></dl>
<video id="xrz79"><output id="xrz79"></output></video>
<video id="xrz79"></video>
<video id="xrz79"></video>
<video id="xrz79"></video>
<video id="xrz79"><output id="xrz79"><delect id="xrz79"></delect></output></video>
<dl id="xrz79"><output id="xrz79"></output></dl>
在k8s中使用Spring Boot重新加載Configmap

在k8s中使用Spring Boot重新加載Configmap

事件如何到達Spring Boot?Kubernetes是否知道我們的Spring Boot應用程序?

技術開發 編程 技術框架 技術發展

 

在k8s中使用Spring Boot重新加載Configmap

事件如何到達Spring Boot?Kubernetes是否知道我們的Spring Boot應用程序?

在Kubernetes中,configmap用于保留應用程序的屬性。一旦Spring應用程序由于屬性文件中的更改而需要重新啟動才能生效?,F在,借助fabric8 Kubernetes客戶端在云中,不再需要重新啟動。我們可以將配置更改反映到Spring Boot live中,而無需停機。

有兩種重載模式:輪詢和事件。顧名思義,輪詢意味著會定期輪詢Kubernetes API。事件更有效,因為它在configmap更改時生效。我們將在這篇文章中介紹事件模式的重新加載。

事件如何到達Spring Boot?Kubernetes是否知道我們的Spring Boot應用程序?否。請勿將此事件與Kubernetes事件混淆。

image.png


在上方可以看到在Spring Boot和Kubernetes API Server之間使用了Websocket。假設在此示例中使用名稱空間“ default”,則此地址中對響應的更改將反映到KubernetesClient https:// {api_server_clusterip} / api / v1 / namespaces / default / configmaps / k8s-live-reload-configmap。如果kube-proxy不允許WebSocket,則HttpClient將對API Server進行輪詢。

在Spring端,給出bootstrap.yaml重載模式和configmap名稱。我故意禁用了執行器端點,因為大多數示例都包含執行器端點,但是由于fabric8-client在內部刷新Bean,因此不需要執行器端點。但是在pom.xml中,由于fabric8-config依賴于執行器配置類,因此需要執行器依賴性。 看例子

spring:

  application:

    name: k8s-live-reload-example

  cloud:

    kubernetes:

      reload:

        enabled: true

        mode: event

      config:

        sources:

          - name: k8s-live-reload-configmap

          

management:

  endpoints:

    enabled-by-default: false #actuator endpoints disabled in order to show it is not required to reload config

用@ConfigurationProperties注釋的Bean被刷新。

@Configuration

@ConfigurationProperties(prefix = "bean" )

@Data

public class Config {

    private String testvalue;

}


運行示例

我們需要構建應用程序映像并將其推送到Docker,而Kubernetes需要查看該映像。我建議使用Minikube 1.16.0。由于DNS和VM(Hyper-V,VirtualBox),以前的版本存在問題。該版本允許將minikube作為docker容器運行??梢詮倪@里下載:https : //github.com/kubernetes/minikube/releases假設您沒有落后于代理或VPN(您可能有網絡問題),請在安裝后使用此命令啟動minikube。您必須正在運行docker。

minikube start --driver=docker

這將下載圖像并啟動minikube。然后安裝kubectl:https ://kubernetes.io/docs/tasks/tools/install-kubectl

首先,我們需要角色綁定來查詢API服務器。否則KubernetesClient無法訪問API服務器。默認為此處的命名空間。

kubectl create rolebinding default-sa-view --clusterrole=view --serviceaccount=default:default --namespace=default

獲取示例項目

git clone https://github.com/gungor/springboot-k8s-configmap-reload-example.git

cd springboot-k8s-configmap-reload-example

現在將名為“ k8s-live-reload-configmap”的配置映射添加到kubernetes

kubectl apply -f src/k8s/config-map.yml

在活動的Shell中切換到kubernetes docker守護進程,以使Kubernetes到達應用程序映像。運行以下命令,然后運行輸出中的最后一條命令。

minikube docker-env

構建應用程序映像

mvn clean install spring-boot:build-image

部署和服務

kubectl create deployment liveconfig-demo --image=springboot-configmap-livereload-example:0.0.1-SNAPSHOT

kubectl create service clusterip liveconfig-demo --tcp=8080:8080

測試

現在在更改配置和服務之前查看config

kubectl get service liveconfig-demo #copy CLUSTER-IP returned from this command

minikube ssh

curl http://CLUSTER-IP:8080/liveconfigtest

它應該返回oldvalue?,F在,通過下面的命令或通過minikube儀表板編輯配置映射

kubectl edit configmap k8s-live-reload-configmap

編輯configmap后,重新檢查應用程序

minikube ssh

curl http://CLUSTER-IP:8080/liveconfigtest

它返回新值。

技術開發 編程 技術框架 技術發展

老师成人痴汉在线播放