Striveonger

vuePress-theme-reco Mr.Lee    2015 - 2025
Striveonger Striveonger
主页
分类
  • 文章
  • 笔记
  • 工具
标签
时间轴
author-avatar

Mr.Lee

264

Article

134

Tag

主页
分类
  • 文章
  • 笔记
  • 工具
标签
时间轴

SpingBoot 与 ConfigMap 的碰撞

vuePress-theme-reco Mr.Lee    2015 - 2025

SpingBoot 与 ConfigMap 的碰撞

Mr.Lee 2025-04-29 11:30:23 favorKubernetesConfigMap

书接上回, 上次咱们创建一个 own-open-apis 项目, 并接入kubernetes 的环境中. 但是完成度比较糙. 没有对配置文件做处理. 今天我们来对这部分做一下优化.

项目地址: https://github.com/Striveonger/own-open-apis 欢迎大家 star, fork 哈~

闲言少叙, 开始正文

# 一. 配置文件外挂

对SpringBoot项目做调整, 以适应需求

# 1. 修改配置文件内容

server:
  port: 18081
  servlet:
    context-path: /own/open/api/v1

spring:
  application:
    name: own-open-apis
  config:
    import:
      # 导入磁盘中的配置文件
      - file:@config.file.path@/app-config.yaml
1
2
3
4
5
6
7
8
9
10
11
12

# 2. 修改 pom.xml

<!-- 配置不同环境下的配置文件的路径 -->
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- 开发环境中用相对路径, 方便多人协作开发 -->
            <config.folder.path>./internal/configs</config.folder.path>
        </properties>
    </profile>
    <profile>
        <id>k8s</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <config.folder.path>/opt/app/configs</config.folder.path>
        </properties>
    </profile>
</profiles>

<build>
    ....
    <resources>
        <!-- 配置文件中开启 @xxx.xxx@ -->
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    ....
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# 3. configs/app-config.yaml

logging:
  level:
    root: info
  file:
    path: logs

own:
  open-apis:
    storage:
      memory:
        max-rows: 10
1
2
3
4
5
6
7
8
9
10
11

# 4. 调整目录结构

❯ tree . -L 3
.
├── LICENSE
├── README.md
├── assets
├── ci-cd
│   ├── VERSION
│   ├── build.sh
│   ├── configs
│   │   └── app-config.yaml
│   ├── docker
│   │   ├── Dockerfile
│   │   └── app.sh
│   ├── helm
│   │   ├── Chart.yaml
│   │   ├── charts
│   │   ├── templates
│   │   └── values.yaml
│   ├── init
│   └── package
│       ├── own-open-apis-1.0.0.tgz
│       └── values.yaml
├── docs
│   ├── devel
│   ├── guide
│   └── images
├── internal
│   ├── configs
│   │   └── app-config.yaml
│   └── service
│       ├── pom.xml
│       └── src
├── test
├── vendor
└── website
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

把后端服务与配置文件剥离

# 二. 修改Helm Chart

# 1. ConfigMap

{{- $appConfigYaml := .Files.Get "../configs/app-config.yaml" | fromYaml }}
{{- $mergeAppConfigYaml := mergeOverwrite $appConfigYaml .Values.app.config.applicationYaml    }}

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "own-open-apis.name" . }}-app-config-map
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "own-open-apis.labels" . | indent 4 }}
data:
  app-config.yaml: |-
{{ toYaml $mergeAppConfigYaml | indent 4  }}
1
2
3
4
5
6
7
8
9
10
11
12
13

values.yaml 文件里配置项与配置文件中内容进行合并

# 2. _volume.tpl

定义了应用通用的日志文件和配置文件挂载目录

{{- define "common.volumes" -}}
- name: app-logs
  hostPath:
    path: /var/logs/own/open/apis
    type: "DirectoryOrCreate"
- name: app-config
  configMap:
    name: {{ include "own-open-apis.name" . }}-app-config-map
{{- end -}}

{{- define "common.volumeMounts" -}}
- name: app-logs
  mountPath: {{.Values.app.config.applicationYaml.logging.file.path | default "/var/log/"}}
  readOnly: false
- name: app-config
  mountPath: /opt/app/configs
  readOnly: true
{{- end -}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • volumeMounts: 是 存储资源的挂载. 属于容器层, 决定数据如何被容器访问 - 必须引用 volumes 中已定义的卷名称
  • volumes: 是 存储资源的定义. 属于 Pod 层, 决定数据如何存储和共享
  • 在Pod中定义的ConfigMap, HostPath等资源, 可以直接共享给同一Pod下的多个容器

emptyDir(临时目录)、hostPath(宿主机路径) configMap(配置文件注入)、secret(密钥注入) persistentVolumeClaim(持久化存储卷)

# 3. deployment.yaml

修改现有配置, 加入卷的挂载(ConfigMap是以挂载卷的方式, 放入容器的磁盘上的)

---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  namespace: "{{ .Release.Namespace }}"
  name: "{{ include "own-open-apis.name" . }}-deployment"
  labels:
{{ include "own-open-apis.labels" .  | indent 4 }}
spec:
  replicas: {{ .Values.app.replicaCount }}
  selector:
    matchLabels:
{{ include "own-open-apis.selectorLabels" .  | indent 6 }}
  template:
    metadata:
      namespace: "{{ .Release.Namespace }}"
      name: "{{ include "own-open-apis.name" . }}"
      labels:
{{ include "own-open-apis.labels" .  | indent 8 }}
    spec:
      restartPolicy: "Always"
      containers:
        - name: "{{ include "own-open-apis.name" . }}"
          image: "{{ .Values.app.image.repository }}:{{ .Values.app.image.tag }}"
          imagePullPolicy: "{{.Values.app.image.pullPolicy }}"
          resources:
            limits:
              cpu: "{{ .Values.app.resources.cpu }}"
              memory: "{{ .Values.app.resources.memory }}"
            requests:
              cpu: "{{ .Values.app.resources.cpu }}"
              memory: "{{ .Values.app.resources.memory }}"
          ports:
            - containerPort: {{ .Values.app.port }}
              protocol: "TCP"
          env:
{{- toYaml .Values.env | nindent 12 }}
          volumeMounts:
{{ include "common.volumeMounts" . | indent 12}}
      volumes:
{{ include "common.volumes" . | indent 8}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# 4. values.yaml

新增配置

app:
  config:
    applicationYaml:
      logging:
        level:
          root: info
        file:
          path: /var/log/own-open-apis
      own:
        open-apis:
          storage:
            memory:
              max-rows: 10
1
2
3
4
5
6
7
8
9
10
11
12
13

# 三. 打包发布

# 1. 修改打包命令

mvn -f internal/service/pom.xml clean package -DskipTests -P k8s
1

我们这次修改了目录结构, 增加环境配置, 需要在打包时指定环境

# 2. 安装 Chart

helm upgrade --install own-open-apis ci-cd/package/own-open-apis-$(cat ./ci-cd/VERSION).tgz \
      --values ci-cd/package/values.yaml \
      --create-namespace --namespace own \
      --set app.config.applicationYaml.own.open-apis.storage.memory.max-rows=3
1
2
3
4

这样就可以公共启动命令来控制关键配置项了, 不同环境下使用不同的配置项

# 3. 成果展示

image-20250429182612188