gpt4 book ai didi

kubernetes - 如何从 kubernetes pod 获取 configmap

转载 作者:行者123 更新时间:2023-12-02 11:53:44 24 4
gpt4 key购买 nike

我有一个 Spring Boot 微服务在 docker 容器上运行,下面是 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,当我启动应用程序时,它会在内部加载配置(Spring Boot功能)。

现在我想使用 configmap 分离此配置,因为我只需创建一个 configmap 并存储所有配置详细信息。

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

kubectl describe 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 中评论了配置文件夹,因此在运行 pod 时,由于没有配置而引发异常,我如何将此控制台配置映射注入(inject)到我的部署中,我尝试过的内容已经共享,但遇到了相同的问题.

最佳答案

首先,您如何在应用程序中使用 .yml 文件?如果您使用 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
volumeMounts:
- mountPath: /app/config
name: config
volumes:
- name: config
configMap:
name: console-configmap
imagePullSecrets:
- name: regcresd

该文件将在路径 /app/config/console-server.yml 中提供。您必须根据您的需要对其进行修改。

关于kubernetes - 如何从 kubernetes pod 获取 configmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56871647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com