Try   HackMD

Kubernetes - Label, Annotaions

tags: Kubernetes

Foreword

When our Pods is increasing, management will be complicated. For example, how to create a Pod on different Node or how to manage Pods on different level.

Thus, Kubernetes has the element which called “Label”. According to the different property, we can give the Pod different Labels.

Label

Defination

Labels are key/value pairs that are attached to objects, such as pods.

Example

  • "release" : "stable", "release" : "canary"
  • "environment" : "dev", "environment" : "qa", "environment" : "production"
  • "tier" : "frontend", "tier" : "backend", "tier" : "cache"
  • "partition" : "customerA", "partition" : "customerB"

If the objects have Label we can know the objects belong which level immediately.

  • An object can have many Labels.
  • We can use the “selector” to find the Label which is we want.

Annotation

Defination

Annotations is the object which we can use to attach arbitrary non-identifying metadata to objects.

Example

  • Fields managed by a declarative configuration layer. Attaching these fields as annotations distinguishes them from default values set by clients or servers, and from auto-generated fields and fields set by auto-sizing or auto-scaling systems.
  • Build, release, or image information like timestamps, release IDs, git branch, PR numbers, image hashes, and registry address.
  • Pointers to logging, monitoring, analytics, or audit repositories.
  • Client library or tool information that can be used for debugging purposes: for example, name, version, and build information.

Practice

Step1 Create Pod

We can use “my-pod.yaml” to create a Pod.

apiVersion: v1 kind: Pod metadata: name: label-demo labels: environment: production app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80

Use Kubectl applt -f my-pod.yaml to create a Pod.

Step2 Get Information

Than use kubectl get pods to get the information. Add the --show-labels can get the Lebels of Pod.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

We can use kubectl describe pods label-demo to get the Annotataions.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

If we want to add Labels on Pod, we can use kubectl label pods pod-demo env=production to add Labels. Then we can see the new Labels on this Pod.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

We also can add Labels on Nose by kubectl label node <node_name> <labels>.

Supplement

And we can use “nodeSelector: <labels>” to select the node which we want to create this objest.

Eample

apiVersion: v1 kind: Pod metadata: name: label-demo labels: environment: production app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80 nodeSelector: kubernetes.io/os=linux

Summary

We can use the Labels the mark the object. Also, we can use the nodeSelector to select the node where we want to create.

Reference

https://ithelp.ithome.com.tw/articles/10194613

https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/