kubectl exec 向pod的文件中增加内容

时间:2021/07/01

事件:测试在写脚本时需要向POD的文件中增加内容

问题:执行 kubectl exec -it podname -n namespace -- echo "hello world" >> /etc/profile

没有写到POD的/etc/profile 文件,写入到了本地的/etc/profile文件

在测试无法解决的时候需要找到我

解决:

模拟问题:

创建POD

kubectl exec -it podname -n namespace -- echo "hello world" >> /etc/profile

模拟写入

kubectl exec -it nginx -n default -- bash `echo "hello world" >> /etc/profile`

裂开

尝试

kubectl exec -it nginx -n default -- bash -c "echo "hello world" >> /etc/profile"
hello

裂开

再尝试

kubectl exec -it nginx -n default -- bash -c "echo hello world >> /etc/profile"

问题解决

反思

如果用-c 那么bash 会从第一个非选项参数后面的字符串中读取命令,如果字符串有多个空格,第一个空格前面的字符串是要执行的命令,也就是$0, 后面的是参数,即$1, $2....

过手如登山,一步一重天
原文地址:https://www.cnblogs.com/zisefeizhu/p/14958899.html