Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

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. I am looking for any kubectl command which i can use to find my key in any available configMap or secrets ? 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 describe cm will print description of all ConfigMaps

For test I've created 5 ConfigMaps from documentation example (with data.game.properties and data.ui.properties). In 3 of them I've set the same key test with value 1,3 or 5.

$ 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.

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

So cm1 include test=configmap1, cm3 include test=configmap3 and cm5 include test=configmap5. Instead of YAML you can also use JSON.

In addition, as ConfigMap is namespaced you can use -A flag to check all namespaces.

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

It might be hard to read, but in console output it will be highlighted.

You can do the same with Secrets.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...