[英]How to fetch configmap from kubernetes pod
我在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
,當我啟動該應用程序時,它將在內部加載配置(spring boot功能)。
現在,我想使用configmap分隔此配置,為此,我僅創建了一個configmap並存儲了所有配置詳細信息。
kubectl創建configmap console-configmap --from-file =。/ config / console-server.yml
kubectl描述配置圖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文件夾,因此在運行pod時,由於沒有配置,它會引發異常,如何將這個console-configmap注入到我的部署中,我嘗試過共享的內容,但是遇到了同樣的問題。
首先,您如何在應用程序中使用.yml文件? 如果將yml文件內容用作環境變量,則配置應該可以正常工作。 但是我懷疑您想使用容器內配置文件中的內容。 如果是這種情況,則必須從configmap中創建一個卷,如下所示:
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
可用。 您必須根據需要對其進行修改。
您是否需要從配置文件中將key:value對作為環境變量加載,然后低於spec即可工作
envFrom:
- configMapRef:
name: console-configmap
如果需要將配置文件作為pod內的文件,則將configmap掛載為卷。 以下鏈接會有所幫助https://kubernetes.io/docs/tutorials/configuration/configure-redis-using-configmap/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.