[英]kubernetes understanding configmap from volume
I am playing around with kubernetes config map and wanted to understand how the volume mounts work我正在玩 kubernetes 配置 map 并想了解卷安装的工作原理
I have a json config file called client_config.json
that is我有一个名为client_config.json
的 json 配置文件
{
"name": "place",
"animal": "thing",
"age": 10
}
I created a config map using the command我使用命令创建了一个配置 map
kubectl create configmap client-config --from-file=client_config.json
I then mount it to a volume onto my deployment as然后我将它安装到我的部署中的卷上
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-web-app
spec:
replicas: 2
selector:
matchLabels:
name: go-web-app
template:
metadata:
labels:
name: go-web-app
spec:
containers:
- name: application
image: my/repo
ports:
- containerPort: 3000
volumeMounts:
- name: config
mountPath: /client-config
readOnly: true
volumes:
- name: config
configMap:
name: client-config
In my go application I am able to read the config using在我的 go 应用程序中,我可以使用
func config(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadFile(filepath.Clean("./client_config.json"))
if err != nil {
fmt.Fprintf(w, fmt.Sprintf("error reading config: %s", err))
} else {
fmt.Fprintf(w, fmt.Sprintf("config value is : %s", string(b)))
}
}
My question is that configmap was mounted to a mount path as我的问题是 configmap 被挂载到挂载路径
mountPath: /client-config
But I am reading it from the code as但我从代码中读取它
b, err := ioutil.ReadFile(filepath.Clean("./client_config.json"))
What is the use of the mount path /client-config
when I do not need to even reference it when I am reading the configmap as a file?当我将 configmap 作为文件读取时,甚至不需要引用挂载路径/client-config
有什么用?
Thanks to David 's comment I was able to resolve the issue.感谢David的评论,我能够解决这个问题。
Issue was I was including the client_config.json
file along with the image, because of which my code was able to reference it via the path ./client_config.json
问题是我在图像中包含了client_config.json
文件,因此我的代码能够通过路径./client_config.json
引用它
After I rebuilt the image without client_config.json
in it I got the error在我重建没有client_config.json
的图像后,我得到了错误
error reading config: open client_config.json: no such file or directory
I then corrected my code to use the mount path and I am now able to read the configmap as a file from the volume然后我更正了我的代码以使用挂载路径,现在我可以将配置映射作为文件从卷中读取
b, err := ioutil.ReadFile(filepath.Clean("/client-config/client_config.json"))
if err != nil {
fmt.Fprintf(w, fmt.Sprintf("error reading config: %s", err))
} else {
fmt.Fprintf(w, fmt.Sprintf("config value is : %s", string(b)))
}
./client_config.json
is a relative file path. ./client_config.json
是相对文件路径。 Most probably, your app's current working directory is /client-config
.最有可能的是,您的应用程序的当前工作目录是/client-config
。 Your actual file is located at /client-config/client_config.json
.您的实际文件位于/client-config/client_config.json
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.