6. Labels, annotations, selectors

Create app1-prod deployment with labels (creates also Deployment)

$ kubectl run app1-prod --image=gcr.io/kuar-demo/kuard-amd64:1 --replicas=3 --port=8080 --labels="ver=1,myapp=app1,env=prod"

Create service (only routable inside cluster). The service is assigned Cluster IP (DNS record is automatically created) which load-balance across all of the pods that are identified by the selector

$ kubectl expose deployment app1-prod

Create app1-test deployment

$ kubectl run app1-test --image=gcr.io/kuar-demo/kuard-amd64:2 --replicas=1 --labels="ver=2,myapp=app1,env=test"

Create app2-prod

$ kubectl run app2-prod --image=gcr.io/kuar-demo/kuard-amd64:2 --replicas=2 --port=8080 --labels="ver=2,myapp=app2,env=prod"

Create service

$ kubectl expose deployment app2-prod

Check if the DNS record was properly created for the Cluster IPs. app2-prod [name of the service], myns [namespace that this service is in], svc [service], cluster.local. [base domain name for the cluster]

$ kubectl run nslookup --rm -it --restart=Never --image=busybox -- nslookup app2-prod
$ kubectl run nslookup --rm -it --restart=Never --image=busybox -- nslookup app2-prod.myns

Create app2-staging

$ kubectl run app2-staging --image=gcr.io/kuar-demo/kuard-amd64:2 --replicas=1 --labels="ver=2,myapp=app2,env=staging"

Show deployments

$ kubectl get deployments -o wide --show-labels

Change labels

$ kubectl label deployments app1-test "canary=true"

Add annotation - usually longer than labels

$ kubectl annotate deployments app1-test description="My favorite deployment with my app"

List ‘canary’ deployments (with canary label)

$ kubectl get deployments -o wide --label-columns=canary

Remove label

$ kubectl label deployments app1-test "canary-"

List pods including labels

$ kubectl get pods --sort-by=.metadata.name --show-labels

List pods ver=2 using the –selector flag

$ kubectl get pods --selector="ver=2" --show-labels

List pods with 2 tags

$ kubectl get pods --selector="myapp=app2,ver=2" --show-labels

List pods where myapp=(app1 or app2)

$ kubectl get pods --selector="myapp in (app1,app2)" --show-labels

Label multiple pods

$ kubectl label pods -l canary=true my=testlabel

List all services

$ kubectl get services -o wide

Get service details

$ kubectl describe service app1-prod

Get service endpoints

$ kubectl describe endpoints app1-prod

List IPs belongs to specific pods

$ kubectl get pods -o wide --selector=myapp=app1,env=prod --show-labels

Cleanup all deployments

$ kubectl delete services,deployments -l myapp