Creating a ReplicaSet

When trying to use the command line to launch a Kubernetes Service immediately, we usually fire kubectl run. However, it would creates a Deployment by default, and not only taking care of the Pod replica but also providing a container-updating mechanism. To simply create a standalone ReplicaSet, we can exploit a configuration YAML file and run it:

$ cat my-first-replicaset.yaml
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: my-first-replicaset
labels:
version: 0.0.1
spec:
replicas: 3
selector:
matchLabels:
project: My-Happy-Web
role: frontend
template:
metadata:
labels:
project: My-Happy-Web
role: frontend
env: dev
spec:
containers:
- name: happy-web
image: nginx:latest

The preceding file is the YAML for our first ReplicaSet. It defines a ReplicaSet named my-first-replicaset, which has three replicas for its Pods. Labels and the selector are the most characteristic settings of ReplicaSet. There are two sets of labels: one for ReplicaSet, the other for Pods. The first label for ReplicaSet is under the metadata of this resource, right beneath the name, which is simply used for description. However, the other label value under the template's metadata, the one for Pods, is also used for identification. ReplicaSet takes charge of the Pods which have the labels covered by its selector.

In our example configuration file, the selector of ReplicaSet looks for Pods with project: My-Happy-Web and role: frontend tags. Since we initiate Pods under control of this ReplicaSet, the Pods' labels should definitely include what selector cares. You may get following error message while creating a ReplicaSet with incorrectly labeled Pods: `selector` does not match template `labels`.

Now, let's create ReplicaSet through this file:

$ kubectl create -f my-first-replicaset.yaml
replicaset.extensions "my-first-replicaset" created
The API version of ReplicaSet in Kubernetes v1.9 
While this book is under construction, Kubernetes v1.9 is released. The API version of ReplicaSet turns to a stable version apps/v1 instead of apps/v1beta2. If you have an older version Kubernetes, please change the value of apiVersion to apps/v1beta2, or you can just update your Kubernetes system.
..................Content has been hidden....................

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