iT邦幫忙

0

SpringBoot 讀取 K8S 即時異動 ConfigMap 的設定值的疑問

  • 分享至 

  • xImage

在 K8S 環境, 使用 SpringBoot 開發一個 Application ,會去連結 http://server_ip_1/getData 撈取資料 ,這個連結設定放在 K8S ConfigMap 中(如下 configmap.yaml 的 api.url),需求是該api.url是可被異動的,並且在 K8S 系統對 ConfigMap 異動後, SpringBoot 也需要同步讀取到新的值, 依下列 application.properties, configmap.yaml, deployment.yaml 三個檔案設定與佈署在 K8S 中, 目前於K8S 啟動 Spring BOOT Application, 可以正常抓取到 ConfigMap 下 api.url 的值, 但是修改configmap.yaml 檔案且改變 api.url 為 "http://server_ip_2/getData" , 再執行 kubectl apply -f configmap.yaml 更新, 已確認 在 K8S 中 api.url已被更改為新值, 而 SpringBoot Application
卻為舊的值,似乎並未讀取到新值, 以下是這個需求的設定, 不知哪裡出錯了, 導致 SpringBoot 讀不到新值

** /src/main/resources**
** application.properties**

spring.application.name=K8SConfigExamp
spring.cloud.kubernetes.config.name=k8sconfig
spring.cloud.kubernetes.config.namespace=atmqrcode
management.endpoint.restart.enabled=true
management.endpoints.web.exposure.include=*
spring.cloud.kubernetes.reload.enabled=true
spring.cloud.kubernetes.reload.mode=event
spring.cloud.kubernetes.reload.period=5000
spring.cloud.kubernetes.reload.strategy=refresh

Spring Boot Controller

@RestController
@RefreshScope
public class ConfigController {	

    @Value("${api.url}")
    private String apiUrl;
	
    @GetMapping("/config")
    public String getConfig() {   	
        return "API URL: " + apiUrl ;
    }
}

** configmap.yaml **

apiVersion: v1
kind: ConfigMap
metadata:
  name: k8sconfig
  namespace: myspace
data:
  api.url: "http://server_ip_1/getData"

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8sconfigexamp
  namespace: myspace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k8sconfigexamp
  template:
    metadata:
      labels:
        app: k8sconfigexamp
    spec:
      containers:
      - name: k8sconfigexamp
        image: myimg/k8sconfigexamp:latest
        ports:
        - containerPort: 8080
        envFrom:
        - configMapRef:
            name: k8sconfig         
        volumeMounts:
        - name: app-logs
          mountPath: /app/logs
        - name: config-volume
          mountPath: /etc/config          
      volumes:
      - name: app-logs
        hostPath:
          path: /run/desktop/mnt/host/c/app/logs
      - name: config-volume
        configMap:
          name: k8sconfig

---
apiVersion: v1
kind: Service
metadata:
  name: k8sconfigexamp
  namespace: myspace
spec:
  selector:
    app: k8sconfigexamp
  ports:
    - protocol: TCP
      port: 80          
      targetPort: 8080 
  type: LoadBalancer

** java Library pom.xml**

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.2</version>
    <relativePath/> 
</parent>	

<groupId>idv.my.examp</groupId>
<artifactId>K8SConfigExamp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>K8SConfigExamp</name>	
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
        <springcloud.kubernetes.version>1.0.1.RELEASE</springcloud.kubernetes.version>
   		<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
	</properties>	

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                   <version>2023.0.3</version> 
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>	
	
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes-config</artifactId>
            <version>1.0.1.RELEASE</version>
        </dependency>     
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
增廣建文
iT邦研究生 5 級 ‧ 2024-07-22 08:16:05
最佳解答

看起來是因為configmap更新後但是pod沒有重啟
可以試試看設個rolling update

jazzinfo iT邦新手 5 級 ‧ 2024-07-22 09:41:59 檢舉

感謝回覆, kubectl rolling update 指令在 Kubernetes 1.18 版本後已被棄用,取而代之的是 kubectl rollout 指令,
執行以下指令後, 問題已解決

kubectl rollout restart deployment <deployment-name> -n namespace

我要發表回答

立即登入回答