首页 > 解决方案 > 如何从 kubernetes pod 中获取 configmap

问题描述

我在 docker 容器上运行了一个 spring boot 微服务,下面是 Dockerfile

FROM java:8-jre
MAINTAINER <>
WORKDIR deploy/
#COPY config/* /deploy/config/
COPY ./ms.console.jar /deploy/
CMD chmod +R 777 ./ms.console.jar
CMD ["java","-jar","/deploy/ms.console.jar","console"]
EXPOSE 8384

这里我的配置存储在外部文件夹中,即/config/console-server.yml当我启动应用程序时,它会在内部加载配置(弹簧启动功能)。

现在我想使用 configmap 来分离这个配置,因为我只是创建了一个 configmap 并存储了所有的配置细节。

kubectl create configmap console-configmap --from-file=./config/console-server.yml

kubectl 描述 configmap 控制台-configmap

以下是描述细节:

Name:         console-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
console-server.yml:
----
server:
  http:
    port: 8385
  compression:
    enabled: true
    mime-types: application/json,application/xml,text/html,text/xml,text/plain,text/css,application/javascript
    min-response-size: 2048

---
spring:
  thymeleaf:
    prefix: classpath:/static
  application:
    name: console-service
  profiles:
     active: native
  servlet:
    multipart:
      max-file-size: 30MB
      max-request-size: 30MB
---
host:
  gateway: http://apigateway:4000
  webhook: http://localhost:9000

我的部署 yml 是:

apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: consoleservice1
spec:
  selector:
    matchLabels:
      app: consoleservice
  replicas: 1 # tells deployment to run 3 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      labels:
        app: consoleservice
    spec:
      containers:
      - name: consoleservice
        image: ms-console
        ports:
        - containerPort: 8384
        imagePullPolicy: Always
        envFrom:
        - configMapRef:
            name: console-configmap
      imagePullSecrets:
        - name: regcresd

我的疑问是,我在 Dockerfile 中注释了 config 文件夹,所以在运行 pod 时,由于没有配置,它会抛出异常,我将如何将此控制台配置映射注入我的部署,我尝试过的已经共享,但遇到了同样的问题。

标签: kubernetes

解决方案


First of all, how are you consuming the .yml file in your application? If you consume your yml file contents as environment variables, your config should just work fine. But I suspect that you want to consume the contents from the config file inside the container. If that is the case you have to create a volume out of the configmap as follows:


apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: consoleservice1
spec:
  selector:
    matchLabels:
      app: consoleservice
  replicas: 1 # tells deployment to run 3 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      labels:
        app: consoleservice
    spec:
      containers:
      - name: consoleservice
        image: ms-console
        ports:
        - containerPort: 8384
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: /app/config
            name: config
      volumes:
        - name: config
          configMap:
            name: console-configmap
      imagePullSecrets:
        - name: regcresd

The file will be available in the path /app/config/console-server.yml. You have to modify it as per your needs.


推荐阅读