如何从kubernetes pod获取configmap

时间:2019-07-03 13:48:46

标签: kubernetes

我在docker容器上运行了一个spring boot microservice,下面是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创建configmap控制台-configmap   --from-file =。/ config / console-server.yml

     

kubectl描述configmap console-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文件夹,因此在运行pods时,由于没有配置,它会引发异常,我将如何将此console-configmap注入到我的部署中,我尝试过共享的内容,但是遇到了相同的问题

2 个答案:

答案 0 :(得分:3)

首先,您如何在应用程序中使用.yml文件?如果将yml文件内容用作环境变量,则配置应该可以正常工作。但是我怀疑您想使用容器内配置文件中的内容。如果是这种情况,则必须从configmap中创建一个卷,如下所示:

Pool()

该文件将在路径 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 中可用。您必须根据需要对其进行修改。

答案 1 :(得分:0)

您是否需要从配置文件中将key:value对作为环境变量进行加载,然后按规范运行即可

envFrom:
        - configMapRef:
            name: console-configmap

如果需要将配置文件作为pod内的文件,则将配置文件作为卷挂载。以下链接会有所帮助 https://kubernetes.io/docs/tutorials/configuration/configure-redis-using-configmap/