kubernetes - 如何从 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 时,由于没有配置,它会抛出异常,我将如何将此控制台配置映射注入我的部署,我尝试过的已经共享,但遇到了同样的问题。
解决方案
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.
推荐阅读
- maxima - 如何将多个匿名函数的列表传递给 plot2d?
- ruby-on-rails - Rails 应用程序中的生产环境变量
- typescript - 如何获取使用 ts-loader 的样式组件的类型?
- javascript - Javascript mouseDown - 无法读取未定义的 currentTarget
- java - 代码依赖于java中的对象
- java - 从同一对象同时运行的线程
- vue.js - 不使用 npm 时如何加载外部 Vue 组件
- c++ - 如何实例化一个类的公共成员并将其作为 std::promise 返回?
- javascript - 空的 href 会通过最严格的内容安全政策吗?
- python - 如何使用 2 列值更新第三列中的值?