Solution Labels And Selectors
- How many pods are there in the
devenvironment:kubectl get pods --selector env=dev - It is possible to print the output of
kubectl get podswithout the header, use:kubectl get pods --selector env=dev --no-headers - To list all objects in a particular namespace:
kubectl get all -selector env=prod --no-headers - Can check for multiple labels at the same time and it will show the pod that is included in all of the labels specified:
kubectl get all --selector env=prod,bu=finance,tier=frontend - A good way to troubleshoot a bad Kubernetes file is via the
kubectl create -f <yaml_file.yaml>and Kubernetes will tell you what the problem is. - The example
replicasetfile isreplicaset-definition-1:apiVersion: apps/v1 kind: Replicaset metadata: name: replicaset-1 spec: replicas: 2 selector: matchLabels: tier: front-end template: metadata: labels: tier: nginx spec: containers: - name: nginx image: nginx - The replicaset needs the value in
tierto match that oftemplate–>metadata=–>labels–>tier - What the file looks like after the changes have been made:
apiVersion: apps/v1 kind: Replicaset metadata: name: replicaset-1 spec: replicas: 2 selector: matchLabels: tier: nginx template: metadata: labels: tier: nginx spec: containers: - name: nginx image: nginx