ConfigMap 能带来什么好处?
传统的应用服务都有自己的配置文件,各自配置文件存储在服务所在节点。如果配置出现变更,就需要对应节点的配置文件。Kubernetes 利用了 Volume 功能,完整设计了一套配置中心,其核心对象就是ConfigMap,使用过程不用修改任何原有设计,即可无缝对 ConfigMap
操作步骤
创建ConfigMap类型的文件
apiVersion: v1
kind: ConfigMap
metadata:
name: jreap-boot-configmap
data:
application.properties: |
spring.profiles.active=dev
server.port=9006
server.servlet.context-path=/demo
spring.application.name=jreap-demo
application-dev.properties: |
spring.datasource.simple.dbtype=oracle
# oracle
spring.datasource.simple.datasource.oracle.druid.maxActive = 50
spring.datasource.simple.datasource.oracle.druid.validationQuery=select 1 from dual
spring.datasource.simple.datasource.oracle.url=jdbc:oracle:thin:@//xx.xxx.xxx.xxx/orcl
spring.datasource.simple.datasource.oracle.username=xxxxx
spring.datasource.simple.datasource.oracle.password=jxxxx
spring.datasource.simple.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.simple.datasource.oracle.druid.filters=stat
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
dubbo.properties: |
##是否启动QoS
dubbo.application.qos.enable=true
##启动QoS绑定的端口
dubbo.application.qos.port=22521
##是否允许远程访问
dubbo.application.qos.accept.foreign.ip=true
##\us5e94用名称
dubbo.application.name=jreap-demo-boot
Deployment yaml中引用 ConfigMap
通过 volume 形式映射到容器内部指定目录上,容器内部进程直接读取该目录下特定文件
当修改 configmap 那么容器内部配置将随之改变,一次性修改所有文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Chart.Name }}-deployment
labels:
app: {{ .Chart.Name }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Chart.Name }}
template:
metadata:
labels:
app: {{ .Chart.Name }}
spec:
containers:
- name: {{ .Chart.Name }}-container
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
ports:
- containerPort: {{ .Values.ports.containerPort }}
name: {{ .Values.ports.name }}
resources:
limits:
cpu: '1'
memory: 2Gi
requests:
cpu: '1'
memory: 2Gi
imagePullPolicy: Always
volumeMounts: //容器内部指定挂载目录
- name: jreap-boot-configmap
mountPath: /jreap-demo-boot/config/application.properties
subPath: application.properties
- name: jreap-boot-configmap
mountPath: /jreap-demo-boot/config/application-{{ .Values.active }}.properties
subPath: application-{{ .Values.active }}.properties
- name: jreap-boot-configmap
mountPath: /jreap-demo-boot/config/dubbo.properties
subPath: dubbo.properties
volumes: //引用目录,即宿主机设置 ConfigMap 文件地址
- name: jreap-boot-configmap
configMap:
name: jreap-boot-configmap
items:
- key: application.properties
path: application.properties
- key: application-{{ .Values.active }}.properties
path: application-{{ .Values.active }}.properties
- key: dubbo.properties
path: dubbo.properties
配置好后在kubesphere的显示如下
进入容器终端查看目录结构如下
dockefile中启动参数指定配置文件
有一点需要注意的是 ConfigMap 更新,数据卷也更新了,如果你的应用进程不进行配置重载,即实时读取配置数据,同样还是使用的老配置。这个问题可以通过把 Pod 的副本数减少到 0 进行重建 Pod 解决
FROM 192.168.23.125:8088/library/jdk:8
# 设置时区
# 解决ORA-01882: timezone region not found问题
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
# 构建参数
ARG APP_NAME="jreap-demo-boot"
ARG VERSION="5.3.1.beta3"
ARG JAR_PATH="/${APP_NAME}"
ARG JAR_FILE="app.jar"
# COPY: 将应用的配置文件也拷贝到镜像中
COPY ./target/$APP_NAME-$VERSION.jar $JAR_PATH/$JAR_FILE
ENTRYPOINT ["java","-Ddubbo.properties.file=jreap-demo-boot/config/dubbo.properties","-Dspring.config.location=jreap-demo-boot/config/","-jar","/jreap-demo-boot/app.jar"]