Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

林子篆
林子篆

Posted on • Originally published atdannypsnl.github.io on

     

NOTE: get labels from Pod

This week our company wants to improve one of our projects which is based on Istio, exchanging weight in different checker instances, make the traffic distribution fairer. My co-worker provides a nice idea: We watching Pods by thelabel selector to discover others instance. Code looks like:

watcher,err:=client.CoreV1().Pods("istio-system").Watch(meta.ListOptions{Watch:true,LabelSelector:"xxx=yyy",})
Enter fullscreen modeExit fullscreen mode

But this causes a problem, you can see that string"xxx=yyy", we have no graceful way to avoid the repeating of label selector in Kubernetes config(for Kubernetes) and environment variable(for our program). And one day they would outdated.

In the beginning, we have a discussion and thought that it would unlikely to have the access to label selector value in Pod. Then start searching in the document. You know what? Surprisingly we can! Even have a few ways can choose! Let's take a look at them:

  1. Useenv variables
apiVersion:apps/v1kind:Deploymentmetadata:name:get-labelsspec:selector:matchLabels:name:get-labels-containertemplate:metadata:labels:name:get-labels-containerspec:containers:-name:a-containerimage:ubuntu:18.04env:-name:SELECTOR_VALUEvalueFrom:fieldRef:fieldPath:metadata.labels['name']
Enter fullscreen modeExit fullscreen mode

In this case, we can get one value from a label selector pair.

  1. Load them into a file

This requires a new API called:downwardAPI

apiVersion:apps/v1kind:Deploymentmetadata:name:get-labelsspec:selector:matchLabels:name:get-labels-containertemplate:metadata:labels:name:get-labels-containerspec:containers:-name:a-containerimage:ubuntu:18.04volumeMounts:-name:podinfomountPath:/etc/podinfocommand:-sleep-infvolumes:-name:podinfodownwardAPI:items:-path:"labels"fieldRef:fieldPath:metadata.labels
Enter fullscreen modeExit fullscreen mode

In this container, we can usecat /etc/podinfo/labels to get all label selectors. So if we want to get all of them then we can use this solution.

Now, hope this can save your time since it's in a weird place of Kubernetes document. And thanks for your read.

References

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a programming language theory lover; good at system software like Networking, OS.
  • Location
    Taiwan
  • Education
    Kaohsiung Medical University
  • Joined

More from林子篆

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp