# Write the manifest file
cat << 'EOF' > pod-labels.yaml
apiVersion: v1
kind: Pod
metadata:
name: red-frontend
namespace: labels # ceclare namespace in metadata
labels: # labels mapping in metadata
color: red
tier: frontend
annotations: # Example annotation
Lab: Kubernetes Pod Design for Application Developers
spec:
containers:
- image: httpd:2.4.38
name: web-server
---
apiVersion: v1
kind: Pod
metadata:
name: green-frontend
namespace: labels
labels:
color: green
tier: frontend
spec:
containers:
- image: httpd:2.4.38
name: web-server
---
apiVersion: v1
kind: Pod
metadata:
name: red-backend
namespace: labels
labels:
color: red
tier: backend
spec:
containers:
- image: postgres:11.2-alpine
name: db
---
apiVersion: v1
kind: Pod
metadata:
name: blue-backend
namespace: labels
labels:
color: blue
tier: backend
spec:
containers:
- image: postgres:11.2-alpine
name: db
EOF
# Create the Pods
kubectl create -f pod-labels.yaml
kubectl get pods -L color,tier
Note that multiple resources are separated by --- in YAML manifest files. Each pod is declared in the labels namespace. Each Pod also has a labels mapping that declares a color and a tier label. Values for the color label span red, green, and blue. Values for the tier label span frontend and backend. The first Pod in the file, named red-frontend, also declares an annotations mapping.
Use the -L (or --label-columns) kubectl get option to display columns for both labels: