简体   繁体   English

如何在 Bash 脚本中使用 Kubernetes 的秘密

[英]How to Make Use of Kubernetes Secrets in Bash Script

I have a secret stored in the Kubernetes pod which can be accessible by the following command.我在 Kubernetes pod 中存储了一个机密,可以通过以下命令访问它。

kubectl exec -it pod_name -- printenv | grep SFTP_PASSWORD

which will provide the output as这将提供输出为

SFTP_PASSWORD=password

I am accessing this secret in the bash script.我正在 bash 脚本中访问这个秘密。

sftp_password=$(kubectl exec -it pod_name -- printenv | grep SFTP_PASSWORD)

But the issue I am facing is the above command will return both key-value pairs instead of value.但我面临的问题是上述命令将返回两个键值对而不是值。

If it's an environment variable, you can just use ordinary Bourne shell parameter expansion syntax:如果是环境变量,则可以使用普通的 Bourne shell 参数扩展语法:

kubectl exec pod_name -- sh -c 'echo $SFTP_PASSWORD'

Note the single quotes, to prevent the local shell from expanding the parameter;注意单引号,防止本地shell扩展参数; the sh -c wrapper so that a remote shell will be started to do the parameter expansion; sh -c包装器,以便启动远程 shell 进行参数扩展; and in this non-interactive use the -it options are unnecessary.在这种非交互式使用中, -it选项是不必要的。

It's possible your cluster administrator prohibits kubectl exec , to prevent copying secret values out of the cluster in exactly this way.您的集群管理员可能禁止kubectl exec ,以防止以这种方式将机密值复制出集群。 This also avoids the non-standard printenv tool (it is part of GNU Coreutils and not the POSIX spec, so Alpine-based images may not have it).这也避免了非标准的printenv工具(它是 GNU Coreutils 的一部分,而不是 POSIX 规范,因此基于 Alpine 的图像可能没有它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM