简体   繁体   English

Kubernetes kubectl 命令在 configMap 或 Secrets 中查找 Key

[英]Kubernetes kubectl command to lookup Key in configMap or Secrets

I have about 10 ConfigMap and few secrets which stores my configuration in key and value.我有大约 10 个 ConfigMap 和一些秘密,它们将我的配置存储在键和值中。 I am looking for any kubectl command which i can use to find my key in any available configMap or secrets?我正在寻找可以用来在任何可用的 configMap 或机密中找到我的密钥的任何 kubectl 命令? I know I can open each file and find my key but is there any kubectl command which lookup all configMap or Serects to find my key?我知道我可以打开每个文件并找到我的密钥,但是是否有任何kubectl命令可以查找所有 configMap 或 Serects 以找到我的密钥?

It might be not the best solution but it works, at the moment it's the only way comes to my mind.这可能不是最好的解决方案,但它有效,目前这是我想到的唯一方法。 If I will find different solution, I will edit this answer.如果我能找到不同的解决方案,我会编辑这个答案。

If your kubectl command will only specify resource without specific name it will print information from all objects of that specified resource.如果您的kubectl命令将仅指定没有特定名称的resource ,它将打印来自该指定资源的所有对象的信息。 kubectl describe cm will print description of all ConfigMaps kubectl describe cm将打印所有ConfigMaps的描述

For test I've created 5 ConfigMaps from documentation example (with data.game.properties and data.ui.properties).为了测试,我从文档示例(使用 data.game.properties 和 data.ui.properties)创建了 5 个ConfigMap In 3 of them I've set the same key test with value 1 , 3 or 5 .在其中 3 个中,我使用值135设置了相同的键test

$ kubectl get cm
NAME   DATA   AGE
cm1    1      20m
cm2    1      21m
cm3    1      21m
cm4    1      21m
cm5    1      21m

So easiest way is to just use kubest get cm -o yaml which will print all ConfigMaps in YAML format and then grep them by key or value.所以最简单的方法是只使用kubest get cm -o yaml它将以 YAML 格式打印所有ConfigMaps ,然后通过键或值打印grep它们。

$ kubectl get cm -o yaml | grep test
      test=configmap1
        {"apiVersion":"v1","data":{"game.properties":"enemies=aliens\nlives=3\ntest=configmap1\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm1","namespace":"default"}}
      test=configmap3
        {"apiVersion":"v1","data":{"game.properties":"secret.code.allowed=true\nsecret.code.lives=30\ntest=configmap3\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm3","namespace":"default"}}
      test=configmap5
        {"apiVersion":"v1","data":{"ui.properties":"allow.textmode=true\nhow.nice.to.look=fairlyNice \ntest=configmap5\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm5","namespace":"default"}}

So cm1 include test=configmap1 , cm3 include test=configmap3 and cm5 include test=configmap5 .所以cm1包括test=configmap1cm3包括test=configmap3cm5包括test=configmap5 Instead of YAML you can also use JSON .您也可以使用JSON YAML

In addition, as ConfigMap is namespaced you can use -A flag to check all namespaces .此外,由于ConfigMapnamespaced ,您可以使用-A标志来检查所有namespaces

$ kubectl get cm -o yaml -A | grep test

It might be hard to read, but in console output it will be highlighted.它可能难以阅读,但在控制台 output 中会突出显示。

You can do the same with Secrets .你可以对Secrets做同样的事情。

Create the following kubectl plugin创建以下 kubectl 插件

#!/bin/bash

kubectl get secrets -o go-template='{{range $s:=.items}}{{range $k,$v:=$s.data}}{{printf "Secret %s: %s\n" $s.metadata.name $k}}{{end}}{{end}}' | grep -i $1
kubectl get configmap -o go-template='{{range $s:=.items}}{{range $k,$v:=$s.data}}{{printf "CM %s: %s\n" $s.metadata.name $k}}{{end}}{{end}}' | grep -i $1

It will have to be located somewhere on your execution path and be executable它必须位于您的执行路径上的某个位置并且是可执行的

# Make script executable
chmod +x /home/mark/bin/kubectl-find-key

# Check it can be found
kubectl plugin list

Lastly it can be used as follows最后可以如下使用

$ kubectl find key database
CM myapp-details: DATABASE_HOSTNAME

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

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