1. Overview
spring boot
application to run on the k8s
cluster as a container, but different environments require different configuration files. We can use an external configuration center, such as nacos
, apollo
. k8s
also provides configMap
to decouple the environment configuration information from the container image to facilitate the modification of the application configuration. This article mainly introduces the use of spring boot
k8s
of configMap
as the external configuration from the following aspects:
spring boot
Load configuration file introductionk8s
ofconfigMap
- Use
k8s
ofconfigMap
as the external configuration file
2. Introduction to spring boot loading configuration file
When the application starts, Spring Boot will automatically find and load the application.properties
and application.yaml
files from the following locations.
The order of configuration file priority from highest to lowest is as follows:
file:./config/
-the highest priority (/config
subdirectory under the project root path)file:./
-second priority (under the project root path)classpath:/config/
-the third priority (under itemresources/config
)classpath:/
-the fourth priority (projectresources
root directory)
High priority configuration will override low priority configuration
application.properties
andapplication.yaml
files in the same directory, thenapplication.properties
will overwrite theapplication.yaml
file
If we want to specify the configuration file of which environment to run when we run, there are three ways:
- Configure
spring.profiles.active=dev
specify the loading environmentapplication.properties
file under the project resources folder - When starting the jar, specify the environment loaded by
--spring.profiles.active=prod
- When starting the jar, specify
--spring.config.location=target/application.properties
load the configuration file location
3. Introduction of 060e8101e28f18 of configMap
ConfigMap
is a API
object used to store non-confidential data in key-value pairs. When used, pod
can be used as environment variables, command line parameters or configuration files in storage volumes.
Note :
ConfigMap
does not provide confidentiality or encryption functions. If the data you want to store is confidential, please usesecret
or use other third-party tools to ensure the privacy of your data instead ofConfigMap
.
ConfigMap
is not designed to save large amounts of data. The data storedConfigMap
1 MiB
. If you need to save data that exceeds this limit, you can consider mounting a storage volume or using a separate database or file service.
Several ways to create configMap
uses the directory to create (
--from-file
specifies that all files in the directory will be used toConfigMap
. There are as many key-value pairs as there are files. key is the file name, and the value is the file name. Content )kubectl create cm [configmap名称] --from-file=[目录]
use file to create (
--from-file
this parameter can be used multiple times, the effect is the same as specifying the entire directory)kubectl create cm [configmap名称] --from-file=[文件] --from-file=[文件]
created from a literal (
--from-literal
this parameter can be used multiple times)kubectl create cm [configmap名称] --from-literal=[键=值] --from-literal=[键=值]
Example: Use literal values to create a configMap
kubectl create cm myconfigMap --from-literal=env=dev --from-literal=name=test
[root@node01 test]# kubectl describe cm myconfigMap
Name: myconfigmap
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
env:
----
dev
name:
----
test
Events: <none>
use
yaml
manifest file to createCreate a
game-demo.yaml
file with the following content:apiVersion: v1 kind: ConfigMap metadata: name: game-demo data: # 类属性键;每一个键都映射到一个简单的值 player_initial_lives: "3" ui_properties_file_name: "user-interface.properties" # 类文件键,键的名字就是文件名,值就是文件的内容 game.properties: | enemy.types=aliens,monsters player.maximum-lives=5 user-interface.properties: | color.good=purple color.bad=yellow allow.textmode=true
Use the following command to create configMap
:
kubectl apply -f game-demo.yaml
4. ☀️ Use k8s's configMap
as an external configuration file
From the previous description we can know, spring boot loader highest priority profile is under the project root path /config
subdirectory , so it can be configMap
mount configuration files to the project root container config
child Directory.
- You can use the following command to create a
configMap
from the file:
kubectl create cm spring-boot-demo --from-file=application.yaml
- Create a
spring-boot-demo.yaml
file with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-boot-demo
namespace: default
labels:
app: spring-boot-demo
spec:
replicas: 1
selector:
matchLabels:
app: spring-boot-demo
template:
metadata:
labels:
app: spring-boot-demo
spec:
containers:
- name: spring-boot-demo
image: ${ORIGIN_REPO}/spring-boot-demo:${IMAGE_TAG}
imagePullPolicy: Always
env:
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 8080
resources:
requests:
cpu: 200m
memory: 500Mi
# 指定配置文件挂载到 /app/config 目录下,构建镜像时的jar包也在 /app 目录下
volumeMounts:
- mountPath: /app/config
name: config
imagePullSecrets:
- name: docker-password
volumes:
- configMap:
# 指定我们创建的configMap的名字
name: spring-boot-demo
# 自定义的名字,需要跟 volumeMounts.name 匹配
name: config
---
apiVersion: v1
kind: Service
metadata:
name: spring-boot-demo
namespace: default
labels:
app: spring-boot-demo
spec:
ports:
- name: port
port: 80
protocol: TCP
targetPort: 8080
selector:
app: spring-boot-demo
type: ClusterIP
- Use the following command to create a deployment with the configuration mounted:
kubectl apply -f spring-boot-demo.yaml
- Use the following command to enter the container to check whether it is mounted to the configured directory:
# 查看pod
kubectl get pod -n default
# 进入容器
kubectl exec -it spring-boot-demo-76bd6c8857-kwss6 bash
5. configMap
content of mounted 060e8101e29b6d will be automatically updated
configMap
used in the volume is updated, the projected keys will eventually be updated. kubelet
configMap
is the latest during each periodic synchronization. However, kubelet
uses its local cache to obtain the current value of configMap
The type of cache can be configured by KubeletConfiguration structure ConfigMapAndSecretChangeDetectionStrategy
field.
configMap
can realize content dissemination through watch operation (default form), TTL-based caching can also be realized, and all requests can be redirected directly to the API
server. Therefore, from configMap
is updated, until the new primary key is projected to the Pod, this time span may be kubelet
the synchronization period of 060e8101e29c64 plus the propagation delay of the cache. The propagation delay here depends on the selected cache type (corresponding to the propagation delay of the watch operation, the TTL duration of the cache, or 0, respectively).
configMap
used by 160e8101e29ca1 as environment variables will not be automatically updated. To update these data requires restarting the Pod.
Reference documents:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。