Linking Deployment to ReplicaSet using the set-based selector

Deployment supports not only the equality-based selector, but also the set-based selector, to specify ReplicaSet. To do that, you can write spec.selector.matchExpressions[] to specify the key and in/notin operator. For example, if you want to specify project in (poc), environment in (staging), tier notn (backend,cache), then matchExpressions would be as follows:

$ cat deploy_set_selector.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3
selector:
matchExpressions:
- {key: project, operator: In, values: [poc]}
- {key: environment, operator: In, values: [staging]}
- {key: tier, operator: NotIn, values: [backend,cache]}
template:
metadata:
labels:
project: poc
environment: staging
tier: frontend
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80

As you can see, the YAML array is represented as -, and the map object as {}, to specify the key, operator, and values. Note that values would also be an array, so use the square bracket [] to specify one or more values.

One thing you need to aware of is one label, called the pod-template-hash label, which is created by Deployment. When you create a Deployment, it will also create a ReplicaSet object. At this time, Deployment will also assign the pod-template-hash label to the ReplicaSet. Let's see how it works:

$ kubectl create -f deploy_set_selector.yaml
deployment.apps "my-nginx" created

$ kubectl get rs
NAME DESIRED CURRENT READY AGE
my-nginx2-764d7cfff 3 3 3 19s

$ kubectl describe rs my-nginx2-764d7cfff
Name: my-nginx2-764d7cfff
Namespace: default
Selector: environment in (staging),pod-template-hash=320837999,project in (poc),tier notin (backend,cache)
...
...
Pod Template:
Labels: environment=staging
pod-template-hash=320837999
project=poc
tier=frontend
...
...

As you can see, the ReplicaSet my-nginx2-764d7cfff has an equality-based selector, as pod-template-hash=320837999 is appended to the Selector and Pod template. It will be used to generate a ReplicaSet and Pod name with a particular hash function (for example, my-nginx2-764d7cfff).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.156