bash - 如何从节点获取文件的内容并将其保存在 pod 内的变量中?
问题描述
我正在编写一个要从 pod 运行的自动化脚本。我想获取 pod 内文件的内容并将其保存在变量中。
该文件是:
$ cat ip_addr.txt
10.120.2.21
我的代码是:
kubectl exec -it --namespace=<namespace name> <pod name> -- bash -c "elastisearch=$(cat ipaddr.txt)"
当我运行 ls 或 cd 等其他命令时,它运行良好
当我elastisearch=$(cat ipaddr.txt)
在吊舱内运行时,它运行良好。
提前致谢。
解决方案
当您使用
kubectl exec -it --namespace=<namespace name> <pod name> -- bash -c "elastisearch=$(cat ipaddr.txt)"
你为这个会话设置它,并且只为这个会话。命令结束时会话结束。
你的情况会发生什么
- 开始一个会话
bash
- 将命令传递给它
elastisearch=$(cat ipaddr.txt)
- 变量
elastisearch
已设置 - 命令成功结束
- 会话结束,shell 被破坏并
elastisearch
随之变化
您有三种可能的解决方案:
elastisearch
在同一会话中运行需要变量的命令,例如:
kubectl exec -it --namespace=<namespace name> <pod name> -- bash -c "elastisearch=$(cat ipaddr.txt); some_other_command_that_need_this_variable"
...
spec:
containers:
- name: <app-name>
image: <app-image>
env:
- name: elastisearch
value: "10.120.2.21"
...
- 使用Dockerfile将变量烘焙到图像中
ENV elastisearch="10.120.2.21"
推荐阅读
- vim - 映射
在 Lexplore 中不起作用 - java - Java 8 DateTimeFormatter 解析可选部分
- git - 我可以在父目录中同时使用 Git 和 Mercurial - Git,在子目录中使用 Mercurial hg?
- laravel - 没有机器人的 Laravel 会话
- python - 我可以在 model_fn、Estimator、Tensorflow 中使用 python for 循环吗?
- javascript - 如何在 D3.js 中为堆叠栏中的每个文本标签添加背景?
- php - Sage Pay Reporting API - 生成的签名哈希无效
- html - 调整大小时无法居中 div
- javascript - 如何使用 SVG.js 为线条制作动画
- java - 如何在Java中用特殊字符分隔字符串