1 前言
“
Kubernetes有專門的ConfigMap和Secret來管理配置,但它也有一些侷限性,所以還是希望通過Spring Cloud Config來管理。在Kubernetes上面的微服務系統會有所不同,我們來探索一下如何整合Spring Cloud Kubernetes來做配置管理。
2 服務端引入依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.2.0.RELEASE</version></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kubernetes</artifactId></dependency>
服務端啟動類如下:
package com.pkslow.config;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServer@EnableDiscoveryClientpublic class ConfigServerK8s { public static void main(String[] args) { SpringApplication.run(ConfigServerK8s.class, args); }}
服務端的application.properties配置如下:
server.port=8888spring.application.name=config-server-k8sspring.cloud.config.server.git.uri=https://github.com/pkslow/pkslow-configspring.cloud.config.server.git.username=admin@pkslow.comspring.cloud.config.server.git.password=***spring.cloud.config.server.git.default-label=masterspring.cloud.config.server.git.search-paths=demo
這裡的應用名字為config-server-k8s,後續部署到k8s也是用這個名字。
k8s的資源定義檔案如下:
apiVersion: apps/v1kind: Deploymentmetadata: name: config-server-k8s-deploymentspec: selector: matchLabels: app: config-server-k8s replicas: 1 template: metadata: labels: app: config-server-k8s spec: containers: - name: config-server-k8s image: pkslow/config-server-k8s:1.0-SNAPSHOT ports: - containerPort: 8888---apiVersion: v1kind: Servicemetadata: labels: app: config-server-k8s name: config-server-k8sspec: ports: - port: 8888 name: config-server-k8s protocol: TCP targetPort: 8888 selector: app: config-server-k8s type: ClusterIP---apiVersion: extensions/v1beta1kind: Ingressmetadata: name: config-server-k8s annotations: kubernetes.io/ingress.class: nginxspec: rules: - http: paths: - path: / backend: serviceName: config-server-k8s servicePort: 8888 host: config-server-k8s.localhost
保持Service名字統一為config-server-k8s。
3 客戶端引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kubernetes</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
spring-cloud-starter-kubernetes為服務發現;
spring-cloud-starter-config作為配置客戶端;
spring-boot-starter-actuator提供EndPoint來重新整理配置。
啟動類為:
package com.pkslow.config;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class ConfigClientK8s { public static void main(String[] args) { SpringApplication.run(ConfigClientK8s.class, args); }}
配置檔案bootstrap.properties如下:
server.port=8080# 服務名spring.application.name=config-client-k8s# 讀取配置時的profilespring.profiles.active=dev# 讀取配置時的程式碼分支spring.cloud.config.label=release# 開放重新整理介面management.endpoints.web.exposure.include=*management.endpoint.health.show-details=always# 通過服務名找到配置伺服器spring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.service-id=config-server-k8s
展示配置結果的Web服務:
package com.pkslow.config;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RefreshScope@RestControllerpublic class PkslowController { @Value("${pkslow.age:0}") private Integer age; @Value("${pkslow.email:null}") private String email; @Value("${pkslow.webSite:null}") private String webSite; @GetMapping("/pkslow") public Map<String, String> getConfig() { Map<String, String> map = new HashMap<>(); map.put("age", age.toString()); map.put("email", email); map.put("webSite", webSite); return map; }}
客戶端的k8s檔案:
apiVersion: apps/v1kind: Deploymentmetadata: name: config-client-k8s-deploymentspec: selector: matchLabels: app: config-client-k8s replicas: 1 template: metadata: labels: app: config-client-k8s spec: containers: - name: config-client-k8s image: pkslow/config-client-k8s:1.0-SNAPSHOT ports: - containerPort: 8080---apiVersion: v1kind: Servicemetadata: labels: app: config-client-k8s name: config-client-k8sspec: ports: - port: 8080 name: config-client-k8s protocol: TCP targetPort: 8080 selector: app: config-client-k8s type: ClusterIP---apiVersion: extensions/v1beta1kind: Ingressmetadata: name: config-client-k8s annotations: kubernetes.io/ingress.class: nginxspec: rules: - http: paths: - path: / backend: serviceName: config-client-k8s servicePort: 8080 host: config-client-k8s.localhost
注意Service名字為config-client-k8s。
4 部署與測試總結一下,服務端主要做了兩件事:
(1)提供配置服務,從Github中讀取配置;
(2)把自己註冊到Kubernetes中去,以讓客戶端發現並讀取配置。
客戶端主要做了件事:
(1)作為配置客戶端,從服務端讀配置;
(2)把自己註冊到Kubernetes中去,讓服務端可以訪問;
(3)提供重新整理配置的功能給外界呼叫。
根據客戶端的名字、配置的label和profile,客戶端便會讀取release分支的配置檔案config-client-k8s-dev.properties的內部。
訪問http://config-client-k8s.localhost/pkslow結果如下:
如果修改了配置資訊,客戶端不能及時生效,需要通過傳送POST請求到http://config-client-k8s.localhost/actuator/refresh,這點不再贅述。
5 服務端統一重新整理如果改了大量配置,或者基礎配置,想讓所有客戶端生效怎麼辦?總不能一個個去重新整理?而且在客戶端有多個Pod需要LoadBalance的情況下,無法確保每個Java應用都能重新整理到。
所以讓服務去讀取所有相關的客戶端,並重新整理。實現很簡單,直接新增一個RefreshController就可以了:
package com.pkslow.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import java.util.HashMap;import java.util.List;import java.util.Map;@RestControllerpublic class RefreshController { @Autowired private DiscoveryClient discoveryClient; private RestTemplate rest = new RestTemplate(); @GetMapping("/refresh") public Map<String, String> refresh() { Map<String, String> result = new HashMap<>(); List<String> services = discoveryClient.getServices(); result.put("Basic Info", "Total services in k8s:" + services.size()); services.stream() .filter(s -> (!"config-server-k8s".equals(s)) && s.startsWith("config-client")) .forEach(service -> { List<ServiceInstance> instances = discoveryClient.getInstances(service); instances.forEach(instance -> { String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/actuator/refresh"; try { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<>(null, headers); ResponseEntity<String> response = rest.postForEntity(url, entity, String.class); result.put(service + " " + url, response.getStatusCode().getReasonPhrase()); } catch (Exception e) { result.put(service + " " + url, e.getMessage()); } }); }); return result; }}
注意上面的過濾邏輯,因為不是所有Service都可以、都需要refresh,具體邏輯看業務。
請求http://config-server-k8s.localhost/refresh結果如下(把客戶端的replicas設定成了3):
{ "config-client-k8s http://10.1.0.126:8080/actuator/refresh": "OK", "config-client-k8s http://10.1.0.125:8080/actuator/refresh": "OK", "Basic Info": "Total services in k8s:7", "config-client-k8s http://10.1.0.122:8080/actuator/refresh": "OK"}
注:可能會遇到許可權不足的問題,建立一個對應的Kubernetes ServiceAccount即可,不清楚可以參考:把Spring Cloud Data Flow部署在Kubernetes上,再跑個任務試試
6 總結“
客戶端其實不一定需要引入DiscoveryService,如果不通過Server的ServiceId來尋找地址,而是直接配置服務端地址spring.cloud.config.uri。但服務端是需要的,因為要獲取客戶端的資訊來實現統一reload。
配置管理其實是一門大學問,把Spring Cloud Config放在Kubernetes上用只是其中一種場景。
多讀書,多分享;多寫作,多整理。