I've gone over the following docomentation page: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
The example deployment yaml is as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
We can see here three different times where the label app: nginx
is mentioned.
Why do we need each of them? I had a hard time understanding it from the official documentation.
The first label is for deployment itself, it gives label for that particular deployment. Lets say you want to delete that deployment then you run following command:
This will delete the entire deployment.
The second label is
selector: matchLabels
which tells the resources(service etc) to match the pod according to label. So lets say if you want to create the service which has all the pods having labels ofapp=nginx
then you provide following definition:The above service will look for the matchLabels and bind pods which have label
app: nginx
assigned to themThe third label is
podTemplate
labels, thetemplate
is actuallypodTemplate
. It describe the pod that it is launched. So lets say you have two replica deployment and k8s will launch 2 pods with the label specified intemplate: metadata: labels
. This is subtle but important difference, so you can have the different labels for deployment and pods generated by that deployment.First label:
It is deployment label which is used to select deployment. You can use below command using first label:
Second Label:
It is not a label . It is label selector to select pod with labels nginx. It is used by ReplicaSet.
Third Label:
It is pod label to identify pods. It is used by ReplicaSet to maintain desired num of replica and for that label selector is used. Also it is used to selects pod with below command: