playing with labels
create helloworld-pod-with-labels.yaml
notice the labels part, you can define labels when deploy via yaml file. or you can use kubectl labels to change running object labels.
apiVersion: v1
kind: Pod
metadata:
name: helloworld
labels:
env: production
author: karthequian
application_type: ui
release-version: "1.0"
spec:
containers:
- name: helloworld
image: karthequian/helloworld:latest
create sample-infrastructure-with-labels.yaml
apiVersion: v1
kind: Pod
metadata:
name: homepage-dev
labels:
env: development
dev-lead: karthik
team: web
application_type: ui
release-version: "12.0"
spec:
containers:
- name: helloworld
image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
name: homepage-staging
labels:
env: staging
team: web
dev-lead: karthik
application_type: ui
release-version: "12.0"
spec:
containers:
- name: helloworld
image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
name: homepage-prod
labels:
env: production
team: web
dev-lead: karthik
application_type: ui
release-version: "12.0"
spec:
containers:
- name: helloworld
image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
name: login-dev
labels:
env: development
team: auth
dev-lead: jim
application_type: api
release-version: "1.0"
spec:
containers:
- name: login
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: login-staging
labels:
env: staging
team: auth
dev-lead: jim
application_type: api
release-version: "1.0"
spec:
containers:
- name: login
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: login-prod
labels:
env: production
team: auth
dev-lead: jim
application_type: api
release-version: "1.0"
spec:
containers:
- name: login
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: cart-dev
labels:
env: development
team: ecommerce
dev-lead: carisa
application_type: api
release-version: "1.0"
spec:
containers:
- name: cart
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: cart-staging
labels:
env: staging
team: ecommerce
dev-lead: carisa
application_type: api
release-version: "1.0"
spec:
containers:
- name: cart
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: cart-prod
labels:
env: production
team: ecommerce
dev-lead: carisa
application_type: api
release-version: "1.0"
spec:
containers:
- name: cart
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: social-dev
labels:
env: development
team: marketing
dev-lead: carisa
application_type: api
release-version: "2.0"
spec:
containers:
- name: social
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: social-staging
labels:
env: staging
team: marketing
dev-lead: marketing
application_type: api
release-version: "1.0"
spec:
containers:
- name: social
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: social-prod
labels:
env: production
team: marketing
dev-lead: marketing
application_type: api
release-version: "1.0"
spec:
containers:
- name: social
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: catalog-dev
labels:
env: development
team: ecommerce
dev-lead: daniel
application_type: api
release-version: "4.0"
spec:
containers:
- name: catalog
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: catalog-staging
labels:
env: staging
team: ecommerce
dev-lead: daniel
application_type: api
release-version: "4.0"
spec:
containers:
- name: catalog
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: catalog-prod
labels:
env: production
team: ecommerce
dev-lead: daniel
application_type: api
release-version: "4.0"
spec:
containers:
- name: catalog
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: quote-dev
labels:
env: development
team: ecommerce
dev-lead: amy
application_type: api
release-version: "2.0"
spec:
containers:
- name: quote
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: quote-staging
labels:
env: staging
team: ecommerce
dev-lead: amy
application_type: api
release-version: "2.0"
spec:
containers:
- name: quote
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: quote-prod
labels:
env: production
team: ecommerce
dev-lead: amy
application_type: api
release-version: "1.0"
spec:
containers:
- name: quote
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: ordering-dev
labels:
env: development
team: purchasing
dev-lead: chen
application_type: backend
release-version: "2.0"
spec:
containers:
- name: ordering
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: ordering-staging
labels:
env: staging
team: purchasing
dev-lead: chen
application_type: backend
release-version: "2.0"
spec:
containers:
- name: ordering
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: ordering-prod
labels:
env: production
team: purchasing
dev-lead: chen
application_type: backend
release-version: "2.0"
spec:
containers:
- name: ordering
image: karthequian/ruby:latest
---
create deployment
localhost:~ xunyang$ kubectl create -f helloworld-pod-with-labels.yaml
pod "helloworld" created
localhost:~ xunyang$ kubectl get pod
NAME READY STATUS RESTARTS AGE
helloworld 0/1 ContainerCreating 0 0s
localhost:~ xunyang$ kubectl get pod
NAME READY STATUS RESTARTS AGE
helloworld 1/1 Running 0 2s
check labels
localhost:~ xunyang$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
helloworld 1/1 Running 0 24s application_type=ui,author=karthequian,env=production,release-version=1.0
add label
localhost:~ xunyang$ kubectl label pod/helloworld app=helloworld
pod "helloworld" labeled
localhost:~ xunyang$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
helloworld 1/1 Running 0 7m app=helloworld,application_type=ui,author=karthequian,env=production,release-version=1.0
change lable data
localhost:~ xunyang$ kubectl label pod/helloworld app=helloxunyang
error: 'app' already has a value (helloworld), and --overwrite is false
localhost:~ xunyang$ kubectl label pod/helloworld app=helloxunyang --overwrite
pod "helloworld" labeled
localhost:~ xunyang$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
helloworld 1/1 Running 0 8m app=helloxunyang,application_type=ui,author=karthequian,env=production,release-version=1.0
delete label
localhost:~ xunyang$ kubectl label pod/helloworld app-
pod "helloworld" labeled
localhost:~ xunyang$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
helloworld 1/1 Running 0 8m application_type=ui,author=karthequian,env=production,release-version=1.0
Search labels
create deployment
localhost:~ xunyang$ kubectl create -f sample-infrastructure-with-labels.yaml
pod "homepage-dev" created
pod "homepage-staging" created
pod "homepage-prod" created
pod "login-dev" created
pod "login-staging" created
pod "login-prod" created
pod "cart-dev" created
pod "cart-staging" created
pod "cart-prod" created
pod "social-dev" created
pod "social-staging" created
pod "social-prod" created
pod "catalog-dev" created
pod "catalog-staging" created
pod "catalog-prod" created
pod "quote-dev" created
pod "quote-staging" created
pod "quote-prod" created
pod "ordering-dev" created
pod "ordering-staging" created
pod "ordering-prod" created
raw data
localhost:~ xunyang$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cart-dev 1/1 Running 0 3m application_type=api,dev-lead=carisa,env=development,release-version=1.0,team=ecommerce
cart-prod 1/1 Running 0 3m application_type=api,dev-lead=carisa,env=production,release-version=1.0,team=ecommerce
cart-staging 1/1 Running 0 3m application_type=api,dev-lead=carisa,env=staging,release-version=1.0,team=ecommerce
catalog-dev 1/1 Running 0 3m application_type=api,dev-lead=daniel,env=development,release-version=4.0,team=ecommerce
catalog-prod 1/1 Running 0 3m application_type=api,dev-lead=daniel,env=production,release-version=4.0,team=ecommerce
catalog-staging 1/1 Running 0 3m application_type=api,dev-lead=daniel,env=staging,release-version=4.0,team=ecommerce
helloworld 1/1 Running 0 18m application_type=ui,author=karthequian,env=production,release-version=1.0
homepage-dev 1/1 Running 0 3m application_type=ui,dev-lead=karthik,env=development,release-version=12.0,team=web
homepage-prod 1/1 Running 0 3m application_type=ui,dev-lead=karthik,env=production,release-version=12.0,team=web
homepage-staging 1/1 Running 0 3m application_type=ui,dev-lead=karthik,env=staging,release-version=12.0,team=web
login-dev 1/1 Running 0 3m application_type=api,dev-lead=jim,env=development,release-version=1.0,team=auth
login-prod 1/1 Running 0 3m application_type=api,dev-lead=jim,env=production,release-version=1.0,team=auth
login-staging 1/1 Running 0 3m application_type=api,dev-lead=jim,env=staging,release-version=1.0,team=auth
ordering-dev 1/1 Running 0 2m application_type=backend,dev-lead=chen,env=development,release-version=2.0,team=purchasing
ordering-prod 1/1 Running 0 2m application_type=backend,dev-lead=chen,env=production,release-version=2.0,team=purchasing
ordering-staging 1/1 Running 0 2m application_type=backend,dev-lead=chen,env=staging,release-version=2.0,team=purchasing
quote-dev 1/1 Running 0 3m application_type=api,dev-lead=amy,env=development,release-version=2.0,team=ecommerce
quote-prod 1/1 Running 0 3m application_type=api,dev-lead=amy,env=production,release-version=1.0,team=ecommerce
quote-staging 1/1 Running 0 3m application_type=api,dev-lead=amy,env=staging,release-version=2.0,team=ecommerce
social-dev 1/1 Running 0 3m application_type=api,dev-lead=carisa,env=development,release-version=2.0,team=marketing
social-prod 1/1 Running 0 3m application_type=api,dev-lead=marketing,env=production,release-version=1.0,team=marketing
social-staging 1/1 Running 0 3m application_type=api,dev-lead=marketing,env=staging,release-version=1.0,team=marketing
You can search for labels with the flag --selector
(or -l
).
localhost:~ xunyang$ kubectl get pods --selector env=production
NAME READY STATUS RESTARTS AGE
cart-prod 1/1 Running 0 3m
catalog-prod 1/1 Running 0 3m
helloworld 1/1 Running 0 18m
homepage-prod 1/1 Running 0 3m
login-prod 1/1 Running 0 3m
ordering-prod 1/1 Running 0 3m
quote-prod 1/1 Running 0 3m
social-prod 1/1 Running 0 3m
localhost:~ xunyang$ kubectl get pods -l env=production
NAME READY STATUS RESTARTS AGE
cart-prod 1/1 Running 0 3m
catalog-prod 1/1 Running 0 3m
helloworld 1/1 Running 0 18m
homepage-prod 1/1 Running 0 3m
login-prod 1/1 Running 0 3m
ordering-prod 1/1 Running 0 3m
quote-prod 1/1 Running 0 3m
social-prod 1/1 Running 0 3m
condition search,
=
!=
in
notin
localhost:~ xunyang$ kubectl get pods -l dev-lead=karthik
NAME READY STATUS RESTARTS AGE
homepage-dev 1/1 Running 0 4m
homepage-prod 1/1 Running 0 4m
homepage-staging 1/1 Running 0 4m
localhost:~ xunyang$ kubectl get pods -l dev-lead=karthik,env=staging
NAME READY STATUS RESTARTS AGE
homepage-staging 1/1 Running 0 4m
localhost:~ xunyang$ kubectl get pods -l dev-lead!=karthik,env=staging
NAME READY STATUS RESTARTS AGE
cart-staging 1/1 Running 0 5m
catalog-staging 1/1 Running 0 5m
login-staging 1/1 Running 0 5m
ordering-staging 1/1 Running 0 5m
quote-staging 1/1 Running 0 5m
social-staging 1/1 Running 0 5m
localhost:~ xunyang$ kubectl get pods -l 'release-version in (1.0,2.0)'
NAME READY STATUS RESTARTS AGE
cart-dev 1/1 Running 0 5m
cart-prod 1/1 Running 0 5m
cart-staging 1/1 Running 0 5m
helloworld 1/1 Running 0 20m
login-dev 1/1 Running 0 5m
login-prod 1/1 Running 0 5m
login-staging 1/1 Running 0 5m
ordering-dev 1/1 Running 0 5m
ordering-prod 1/1 Running 0 5m
ordering-staging 1/1 Running 0 5m
quote-dev 1/1 Running 0 5m
quote-prod 1/1 Running 0 5m
quote-staging 1/1 Running 0 5m
social-dev 1/1 Running 0 5m
social-prod 1/1 Running 0 5m
social-staging 1/1 Running 0 5m
localhost:~ xunyang$ kubectl get pods -l 'release-version in (1.0,4.0)'
NAME READY STATUS RESTARTS AGE
cart-dev 1/1 Running 0 5m
cart-prod 1/1 Running 0 5m
cart-staging 1/1 Running 0 5m
catalog-dev 1/1 Running 0 5m
catalog-prod 1/1 Running 0 5m
catalog-staging 1/1 Running 0 5m
helloworld 1/1 Running 0 20m
login-dev 1/1 Running 0 5m
login-prod 1/1 Running 0 5m
login-staging 1/1 Running 0 5m
quote-prod 1/1 Running 0 5m
social-prod 1/1 Running 0 5m
social-staging 1/1 Running 0 5m
ocalhost:~ xunyang$ kubectl get pods -l "release-version in (1.0,2.0),team in (marketing,ecommerce)"
NAME READY STATUS RESTARTS AGE
cart-dev 1/1 Running 0 6m
cart-prod 1/1 Running 0 6m
cart-staging 1/1 Running 0 6m
quote-dev 1/1 Running 0 6m
quote-prod 1/1 Running 0 6m
quote-staging 1/1 Running 0 6m
social-dev 1/1 Running 0 6m
social-prod 1/1 Running 0 6m
social-staging 1/1 Running 0 6m
localhost:~ xunyang$ kubectl get pods -l 'release-version notin (1.0,2.0)'
NAME READY STATUS RESTARTS AGE
catalog-dev 1/1 Running 1 12m
catalog-prod 1/1 Running 1 12m
catalog-staging 1/1 Running 1 12m
labels will also work with kubectl get services/deployments/all --show-labels
and will return labels for your services, deployments or all objects.
localhost:~ xunyang$ kubectl get all --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cart-dev 1/1 Running 0 8m application_type=api,dev-lead=carisa,env=development,release-version=1.0,team=ecommerce
cart-prod 1/1 Running 0 8m application_type=api,dev-lead=carisa,env=production,release-version=1.0,team=ecommerce
cart-staging 1/1 Running 0 8m application_type=api,dev-lead=carisa,env=staging,release-version=1.0,team=ecommerce
catalog-dev 1/1 Running 0 8m application_type=api,dev-lead=daniel,env=development,release-version=4.0,team=ecommerce
catalog-prod 1/1 Running 0 8m application_type=api,dev-lead=daniel,env=production,release-version=4.0,team=ecommerce
catalog-staging 1/1 Running 0 8m application_type=api,dev-lead=daniel,env=staging,release-version=4.0,team=ecommerce
helloworld 1/1 Running 0 23m application_type=ui,author=karthequian,env=production,release-version=1.0
homepage-dev 1/1 Running 0 8m application_type=ui,dev-lead=karthik,env=development,release-version=12.0,team=web
homepage-prod 1/1 Running 0 8m application_type=ui,dev-lead=karthik,env=production,release-version=12.0,team=web
homepage-staging 1/1 Running 0 8m application_type=ui,dev-lead=karthik,env=staging,release-version=12.0,team=web
login-dev 1/1 Running 0 8m application_type=api,dev-lead=jim,env=development,release-version=1.0,team=auth
login-prod 1/1 Running 0 8m application_type=api,dev-lead=jim,env=production,release-version=1.0,team=auth
login-staging 1/1 Running 0 8m application_type=api,dev-lead=jim,env=staging,release-version=1.0,team=auth
ordering-dev 1/1 Running 0 8m application_type=backend,dev-lead=chen,env=development,release-version=2.0,team=purchasing
ordering-prod 1/1 Running 0 8m application_type=backend,dev-lead=chen,env=production,release-version=2.0,team=purchasing
ordering-staging 1/1 Running 0 8m application_type=backend,dev-lead=chen,env=staging,release-version=2.0,team=purchasing
quote-dev 1/1 Running 0 8m application_type=api,dev-lead=amy,env=development,release-version=2.0,team=ecommerce
quote-prod 1/1 Running 0 8m application_type=api,dev-lead=amy,env=production,release-version=1.0,team=ecommerce
quote-staging 1/1 Running 0 8m application_type=api,dev-lead=amy,env=staging,release-version=2.0,team=ecommerce
social-dev 1/1 Running 0 8m application_type=api,dev-lead=carisa,env=development,release-version=2.0,team=marketing
social-prod 1/1 Running 0 8m application_type=api,dev-lead=marketing,env=production,release-version=1.0,team=marketing
social-staging 1/1 Running 0 8m application_type=api,dev-lead=marketing,env=staging,release-version=1.0,team=marketing
you can delete pods, services or deployments by label as well! For example kubectl delete pods -l dev-lead=karthik
will delete all pods who's dev-lead was Karthik.
localhost:~ xunyang$ kubectl delete pods -l dev-lead=karthik
pod "homepage-dev" deleted
pod "homepage-prod" deleted
pod "homepage-staging" deleted
clear data
kubectl delete pod --all