Skip to content

Pod Design

Yasser Sinjab edited this page Apr 8, 2019 · 10 revisions

Labels and Annotations

Labels is one of the best features in kubernetes for managing not only pods but even kubernetes resources.

Create a pod with two labels (app=frontend) and (env=prod):

kubectl run nginx --image=nginx --restart=Never --labels="app=frontend,env=prod"

Get the pod with all labels:

kubectl get po nginx --show-labels 

Get the pod with specific label (app):

kubectl get po nginx -L app

Label a running pod:

kubectl run nginx --image=nginx --restart=Never
kubectl label po nginx env=prod

Update a label for a current pod:

kubectl label po nginx env=dev --overwrite
kubectl get po nginx -L env

Create two pods with labels and let's do some selection:

kubectl run nginx2 --image=nginx --restart=Never --labels="app=frontend,env=dev"
kubectl run nginx1 --image=nginx --restart=Never --labels="app=frontend,env=prod"
# List all pods that has env=prod and app=frontend
kubectl get po -l env=prod,app=frontend

# List all pods that has app=frontend but not env=prod:
kubectl get po -l env!=prod,app=frontend --show-labels

# List all pods that has env either (dev) or prod:
kubectl get po -l 'env in (dev, prod)'

Use label to schedule a pod on a specific node. Let's explain node selector (you can use nodename too if you like)

# I'm using minikube
kubectl label node minikube gpu=false
kubectl explain pod.spec | grep node

Edit the pod.yaml and add node selector:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  nodeSelector:
    gpu: "false"
  containers:
  - image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

Now annotations: They are similar to labels but they are not meant to hold identifying information, but they can hold large pieces of information.

kubectl run nginx --image=nginx --restart=Never
kubectl annotate po nginx description='My lovely nginx'
kubectl describe po nginx | grep Annotations
# Annotations:        description: My lovely nginx

You can annotate multiple pods (nginx1, nginx2, nginx3) by simply adding them together in the same command:

kubectl run nginx1 --image=nginx --restart=Never
kubectl run nginx2 --image=nginx --restart=Never
kubectl run nginx3 --image=nginx --restart=Never
kubectl annotate po nginx1 nginx2 nginx3 description='My lovely nginx'
# Or
kubectl annotate po nginx{1..3} description='My lovely nginx'

3.3 -> 3.6 + 3.8

  • deployment with replica
  • change image + check status
  • change image to wrong image and rollback
  • pause and resume
  • revision
  • scale + autoscale
  • jobs and cron jobs
Clone this wiki locally