此篇文章中,我們將講述如何從configMap中引入?yún)?shù)配置,如何從掛載文件中引入文件配置。其中文件掛載是應用部署中常見的形式。
-
1、通過 valueRef 引入 ConfigMap 配置信息
- 1.1: 初始化項目
- 1.2: 定義將外部引入的配置項
- 1.3: 構建鏡像 & 發(fā)布應用
- 1.4: 確認配置的引用
-
2、通過 fileMount 引入 ConfigMap 配置信息
- 2.1: 初始化項目
- 2.2: 定義將外部引入的配置項
- 2.3: 構建 & 發(fā)布鏡像
- 2.4: 確認配置的引用
組件版本說明:
- SpringBoot:3.1.0
- SpringCloud:4.0.4
- SpringCloudKubernetes:3.0.4
- JDK17
1、通過 valueRef 引入 ConfigMap 配置信息
1.1: 初始化項目
引入maven依賴,核心依賴:spring-cloud-kubernetes-fabric8-config
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-fabric8-config</artifactId>
<version>${springcloud-kubernetes-fabric8.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>
1.2: 定義將外部引入的配置項
在 application.yaml 中定義
spring:
application:
name: springboot-cloud-k8s-config-valueref
greeting:
message: ${GREETING_MESSAGE:nice to meet you}
farewell:
message: ${FAREWELL_MESSAGE:see you next time}
在ConfigMap中定義對應的參數(shù)
apiVersion: v1
kind: ConfigMap
metadata:
name: springboot-k8s-config-valueref
labels:
app: springboot-k8s-config-valueref
data:
GREETING_MESSAGE: "Say Hello to the World outside"
我們定義兩個不同的ConfigMap便于更好地演示
apiVersion: v1
kind: ConfigMap
metadata:
name: springboot-k8s-config-valueref2
labels:
app: springboot-k8s-config-valueref
data:
farewell.message: "Say Farewell to the World outside"
1.3: 構建鏡像 & 發(fā)布應用
通過maven指令編譯一個鏡像
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.maven.plugin.version}</version>
<executions>
<!--如果想在項目打包時構建鏡像添加-->
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<images>
<image>
<name>org/${project.artifactId}:${project.version}</name>
<build>
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
</build>
</image>
</images>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
可以通過maven指令或IDE的插件
mvn clean package -Dmaven.test.skip=true
執(zhí)行后,就可以將構建的鏡像推送至本地/遠程倉庫
REPOSITORY TAG IMAGE ID CREATED SIZE
org/springboot-k8s-config-valueref 1.0-SNAPSHOT 8902aa877999 6 seconds ago 564MB
通過插件,可以進行應用打包 -> 構建docker鏡像 -> 在本地/遠程倉庫中覆蓋更新相同版本的鏡像 -> 并清除本地臨時文件
接下來,我們可以通過倉庫中的鏡像,發(fā)布一個pod
這邊使用 configMapRef & configMapKeyRef
進行引用 :
test.yaml示例文件如下:
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: ConfigMap
metadata:
name: springboot-k8s-config-valueref
labels:
app: springboot-k8s-config-valueref
data:
GREETING_MESSAGE: "Say Hello to the World outside"
- apiVersion: v1
kind: ConfigMap
metadata:
name: springboot-k8s-config-valueref2
labels:
app: springboot-k8s-config-valueref
data:
farewell.message: "Say Farewell to the World outside"
- apiVersion: v1
kind: Service
metadata:
labels:
app: springboot-k8s-config-valueref
name: springboot-k8s-config-valueref
spec:
type: NodePort
selector:
app: springboot-k8s-config-valueref
ports:
- nodePort: 30163
port: 8080
protocol: TCP
targetPort: 8080
- apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-k8s-config-valueref
labels:
app: springboot-k8s-config-valueref
group: org.example
spec:
strategy:
type: Recreate
replicas: 1
selector:
matchLabels:
app: springboot-k8s-config-valueref
template:
metadata:
labels:
app: springboot-k8s-config-valueref
spec:
volumes:
- name: autoconfig
containers:
- name: springboot-k8s-config-valueref
image: org/springboot-k8s-config-valueref:1.0-SNAPSHOT
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: springboot-k8s-config-valueref
env:
- name: FAREWELL_MESSAGE
valueFrom:
configMapKeyRef:
name: springboot-k8s-config-valueref2
key: farewell.message
執(zhí)行情況如下:
$ kubectl apply -f ~/springboot-demo/springboot-config-k8s-valueref/src/main/resources/deploy-valueref.yaml
configmap/springboot-k8s-config-valueref created
configmap/springboot-k8s-config-valueref2 created
service/springboot-k8s-config-valueref created
deployment.apps/springboot-k8s-config-valueref created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
springboot-k8s-config-valueref-57d464c66c-tg8nw 1/1 Running 0 3s
$ kubectl logs -f springboot-k8s-config-valueref-57d464c66c-tg8nw
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.0)
2023-09-18T12:17:05.206Z INFO 1 --- [ main] org.example.BootValueRefApplication : Starting BootValueRefApplication using Java 17.0.8 with PID 1 (/opt/app/springboot-k8s-config-valueref-1.0-SNAPSHOT.jar started by root in /opt/app)
2023-09-18T12:17:05.215Z INFO 1 --- [ main] org.example.BootValueRefApplication : The following 1 profile is active: "kubernetes"
2023-09-18T12:17:07.260Z INFO 1 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=cc47999e-9518-3998-aa7d-05324c2cb413
2023-09-18T12:17:08.015Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-09-18T12:17:08.028Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-09-18T12:17:08.029Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-09-18T12:17:08.150Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-09-18T12:17:08.153Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2632 ms
2023-09-18T12:17:09.339Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-18T12:17:09.412Z INFO 1 --- [ main] org.example.BootValueRefApplication : Started BootValueRefApplication in 6.462 seconds (process running for 7.914)
經(jīng)過以上步驟,一個簡單的應用已經(jīng)順利地跑起來了
1.4: 確認配置的引用
這邊測試是采用 minikube 在本地搭建 K8S集群。
由于本地環(huán)境,需要講暴露服務來訪問,minikube service springboot-k8s-config-valueref --url
minikube service springboot-k8s-config-valueref --url
http://127.0.0.1:51650
? Because you are using a Docker driver on darwin, the terminal needs to be open to run it.
請求服務API
$ curl http://127.0.0.1:51650
hello, 'Say Hello to the World outside', goodbye, 'Say Farewell to the World outside'
返回在外部ConfigMap中配置的參數(shù),通過configMapRef 和 configMapKeyRef 均可以獲取到參數(shù)
2、通過 fileMount 引入 ConfigMap 配置信息
以下通過文件掛載的常見形式加載服務變量,通常直接掛載到jar的執(zhí)行目錄下的/config目錄,作為springboot加載配置文件的第一優(yōu)先級,無需指定自動讀取
2.1: 初始化項目
引入maven依賴,核心依賴:spring-cloud-kubernetes-fabric8-config
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-fabric8-config</artifactId>
<version>${springcloud-kubernetes-fabric8.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>
2.2: 定義將外部引入的配置項
在 application.yaml 中定義參數(shù),部分文件掛載的參數(shù)可以不在文件中定義
server:
port: 8080
spring:
application:
name: springboot-k8s-config-filemount
dbuser: ${DB_USERNAME:default}
dbpassword: ${DB_PASSWORD:default}
定義 configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: springboot-k8s-config-filemount
labels:
app: springboot-k8s-config-filemount
data:
application.yaml: |-
greeting:
message: "Say Hello to the World outside"
farewell:
message: "Say Goodbye to the World outside"
定義配置類:
@Data
@Configuration
public class DbConfig {
@Value("${dbuser}")
private String dbUsername;
@Value("${dbpassword}")
private String dbPassword;
}
@Data
@Configuration
public class MyConfig {
@Value("${greeting.message:'default greeting message'}")
private String greetingMessage;
@Value("${farewell.message:'default farewell message'}")
private String farewellMessage;
}
2.3: 構建 & 發(fā)布鏡像
通過 maven compiler 構建鏡像
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.maven.plugin.version}</version>
<executions>
<!--如果想在項目打包時構建鏡像添加-->
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<images>
<image>
<name>org/${project.artifactId}:${project.version}</name>
<build>
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
</build>
</image>
</images>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
通過指令 或 IDE 插件,將鏡像打包
mvn clean package -Dmaven.test.skip=true
將鏡像推到倉庫
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
org/springboot-config-k8s-filemount 1.0-SNAPSHOT f93c2e1254d5 42 seconds ago 628MB
根據(jù)鏡像,發(fā)布一個Pod
使用 volumes
引入掛載的文件
使用 volumeMounts
指定掛載的文件
針對secret的數(shù)據(jù)需要在配置前加密,可以使用指定來獲取base64加密后的數(shù)據(jù): echo -n root | base64
,然后再填充到文件中
$ echo -n root | base64
cm9vdA==
樣例文件:test.yaml:
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Secret
metadata:
name: springboot-k8s-config-filemount-secret
labels:
app: springboot-k8s-config-filemount
data:
dbuser: cm9vdA==
dbpassword: cGFzc3dvcmQ=
- apiVersion: v1
kind: ConfigMap
metadata:
name: springboot-k8s-config-filemount
labels:
app: springboot-k8s-config-filemount
data:
application.yaml: |-
greeting:
message: "Say Hello to the World outside"
farewell:
message: "Say Goodbye to the World outside"
- apiVersion: v1
kind: Service
metadata:
labels:
app: springboot-k8s-config-filemount
name: springboot-k8s-config-filemount
spec:
type: NodePort
selector:
app: springboot-k8s-config-filemount
ports:
- nodePort: 30163
port: 8080
protocol: TCP
targetPort: 8080
- apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-k8s-config-filemount
labels:
app: springboot-k8s-config-filemount
group: org.example
spec:
strategy:
type: Recreate
replicas: 1
selector:
matchLabels:
app: springboot-k8s-config-filemount
template:
metadata:
labels:
app: springboot-k8s-config-filemount
spec:
volumes:
- name: config-volume
configMap:
name: springboot-k8s-config-filemount
items:
- key: application.yaml
path: application.yaml
containers:
- name: springboot-k8s-config-filemount
image: org/springboot-config-k8s-filemount:1.0-SNAPSHOT
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
env:
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: springboot-k8s-config-filemount-secret
key: dbuser
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: springboot-k8s-config-filemount-secret
key: dbpassword
volumeMounts:
- name: config-volume
mountPath: /opt/app/config/application.yaml
subPath: application.yaml
使用 kubectl apply -f test.yaml
的方式發(fā)布Pod
$ kubectl apply -f ~/springboot-demo/springboot-config-k8s-filemount/src/main/resources/deploy-filemount.yaml
secret/springboot-k8s-config-filemount-secret created
configmap/springboot-k8s-config-filemount created
service/springboot-k8s-config-filemount created
deployment.apps/springboot-k8s-config-filemount created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
springboot-k8s-config-filemount-89d6bdfcd-vvkqz 1/1 Running 0 3s
$ kubectl logs springboot-k8s-config-filemount-89d6bdfcd-vvkqz
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.0)
2023-09-20T10:38:40.477Z INFO 1 --- [ main] org.example.BootFilemountApplication : Starting BootFileamountApplication using Java 17.0.8 with PID 1 (/opt/app/springboot-config-k8s-filemount-1.0-SNAPSHOT.jar started by root in /opt/app)
2023-09-20T10:38:40.480Z INFO 1 --- [ main] org.example.BootFilemountApplication : The following 1 profile is active: "kubernetes"
2023-09-20T10:38:42.402Z INFO 1 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=49502b73-ffac-39f1-a249-dec4d3facce7
2023-09-20T10:38:43.379Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-09-20T10:38:43.402Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-09-20T10:38:43.403Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-09-20T10:38:43.517Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-09-20T10:38:43.520Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2845 ms
2023-09-20T10:38:44.714Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-20T10:38:44.792Z INFO 1 --- [ main] org.example.BootFilemountApplication : Started BootFilemountApplication in 6.37 seconds (process running for 8.022)
應用啟動,準備測試
2.4: 確認配置的引用
暴露端口 minikube service springboot-k8s-config-filemount --url
minikube service springboot-k8s-config-filemount --url
http://127.0.0.1:55369
? Because you are using a Docker driver on darwin, the terminal needs to be open to run it.
請求接口,獲取注入的參數(shù)值文章來源:http://www.zghlxwxcb.cn/news/detail-772871.html
$ curl http://127.0.0.1:55369
hello, 'Say Hello to the World outside', goodbye, 'Say Goodbye to the World outside'. myname: 'root', mypass: 'password'
參數(shù)值獲取成功注入的參數(shù)文章來源地址http://www.zghlxwxcb.cn/news/detail-772871.html
到了這里,關于Kubernetes-03-實踐篇 Spring-cloud-kubernetes 自動引入 K8S的 ConfigMap 參數(shù)(參數(shù)引用 和 文件掛載)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!