How it works...

The ReplicaSet defines a set of Pods by using a Pod template and labels. As in the ideas from previous sections, the ReplicaSet only manages the Pods via their labels. It is possible that the Pod template and the configuration of the Pod are different. This also means that standalone Pods can be added into a set by using label modification.

Let's evaluate this concept of selectors and labels by creating a ReplicaSet similar to the diagram at the beginning of this recipe:

The ReplicaSet would cover Pods which have the same labels describing in its selector

First, we are going to create a CentOS Pod with the labels project: My-Happy-Web, role: frontend, and env: test:

// use subcommand "run" with tag restart=Never to create a Pod
$ kubectl run standalone-pod --image=centos --labels="project=My-Happy-Web,role=frontend,env=test" --restart=Never --command sleep 3600
pod "standalone-pod" created

// check Pod along with the labels
$ kubectl get pod -L project -L role -L env
NAME READY STATUS RESTARTS AGE PROJECT ROLE ENV
standalone-pod 1/1 Running 0 3m My-Happy-Web frontend test

After adding this command, a standalone Pod runs with the labels we specified.

Next, go create your first ReplicaSet example by using the YAML file again:

$ kubectl create -f my-first-replicaset.yaml
replicaset.apps "my-first-replicaset" created

// check the Pod again
$ kubectl get pod -L project -L role -L env
NAME READY STATUS RESTARTS AGE PROJECT ROLE ENV
my-first-replicaset-fgdc8 1/1 Running 0 14s My-Happy-Web frontend dev
my-first-replicaset-flc9m 1/1 Running 0 14s My-Happy-Web frontend dev
standalone-pod 1/1 Running 0 6m My-Happy-Web frontend test

As in the preceding result, only two Pods are created. It is because the Pod standalone-pod is considered one of the sets taken by my-first-replicaset. Remember that my-first-replicaset takes care of the Pods labeled with project: My-Happy-Web and role:frontend (ignore the env tag). Go check the standalone Pod; you will find it belongs to a member of the ReplicaSet as well:

$ kubectl describe pod standalone-pod
Name: standalone-pod
Namespace: default
Node: ubuntu02/192.168.122.102
Start Time: Sat, 05 May 2018 16:57:14 -0400
Labels: env=test
project=My-Happy-Web
role=frontend
Annotations: <none>
Status: Running
IP: 192.168.79.57
Controlled By: ReplicaSet/my-first-replicaset
...

Similarly, once we delete the set, the standalone Pod will be removed with the group:

// remove the ReplicaSet and check pods immediately
$ kubectl delete rs my-first-replicaset && kubectl get pod
replicaset.extensions "my-first-replicaset" deleted
NAME READY STATUS RESTARTS AGE
my-first-replicaset-fgdc8 0/1 Terminating 0 1m
my-first-replicaset-flc9m 0/1 Terminating 0 1m
standalone-pod 0/1 Terminating 0 7m
..................Content has been hidden....................

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