title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
20:101 | ReplicaSets and Deployments |
20:102 | The most important building block of Kubernetes applications is the ReplicaSet, that is, a Pod replicated N times. Usually, however, you use a more complex object that is built on top of the ReplicaSet – the Deployment. Deployments not only create a ReplicaSet, but also monitor them to ensure that the number of replicas is kept constant regardless of hardware faults and other events that might involve the ReplicaSets. In other words, they are a declarative way of defining ReplicaSets and Pods. |
20:103 | Replicating the same functionalities, and thus the same Pods, is the simplest operation to optimize for performance: the more replicas we create of the same Pod, the more hardware resources and threads must be made available for the functionality encoded by that Pod. Thus, when we discover that a functionality becomes a bottleneck in the system, we may just increase the number of replicas of the Pod that encodes that functionality. |
20:104 | Each Deployment has a name (metadata->name), an attribute that specifies the desired number of replicas (spec->replicas), a key-value pair (spec -> selector-> matchLabels) that selects the Pods to monitor, and a template (spec->template) that specifies how to build the Pod replicas: |
20:105 | apiVersion: apps/v1 |
20:106 | kind: Deployment |
20:107 | metadata: |
20:108 | name: my-deployment-name |
20:109 | namespace: my-namespace #this is optional |
20:110 | spec: |
20:111 | replicas: 3 |
20:112 | selector: |
20:113 | matchLabels: |
20:114 | my-pod-label-name: my-pod-label-value |
20:115 | ... |
20:116 | template: |
20:117 | ... |
20:118 | |
20:119 | namespace is optional and, if not provided, a namespace called default is assumed. Namespaces are a way of keeping separate the objects of a Kubernetes cluster. For instance, a cluster can host the objects of two completely independent applications, each placed in a separate namespace in order to prevent possible name collisions. In a few words, Kubernetes namespaces have the same purpose as .NET namespaces: preventing name collisions. |
20:120 | Indented inside the template is the definition of the Pod to replicate. Complex objects such as Deployments can also contain other kinds of templates, for instance, a template of disk-like memory required by the external environment. We will discuss this in more detail in the Advanced Kubernetes concepts section. |
20:121 | In turn, the Pod template contains a metadata section with the labels used to select the Pods, and a spec section with a list of all of the containers: |
20:122 | metadata: |
20:123 | labels: |
20:124 | my-pod-label-name: my-pod-label-value |
20:125 | ... |
20:126 | spec: |
20:127 | containers: |
20:128 | ... |
20:129 | - name: my-container-name |
20:130 | image: <Docker imagename> |
20:131 | resources: |
20:132 | requests: |
20:133 | cpu: 100m |
20:134 | memory: 128Mi |
20:135 | limits: |
20:136 | cpu: 250m |
20:137 | memory: 256Mi |
20:138 | ports: |
20:139 | - containerPort: 6379 |
20:140 | env: |
20:141 | - name: env-name |
20:142 | value: env-value |
20:143 | ... |
20:144 | |
20:145 | Each container has a name and must specify the name of the Docker image to use to create the containers. If the Docker image is not contained in the public Docker registry, the name must be a URI that also includes the repository’s location. |
20:146 | Then, containers must specify the memory and CPU resources that they need to be created in the resources->requests object. A Pod replica is created only if these resources are currently available. The resources->limits object, instead, specifies the maximum resources a container replica can use. If, during the container execution, these limits are exceeded, action is taken to limit them. More specifically, if the CPU limit is exceeded, the container is throttled (its execution is stopped to restore its CPU consumption), while, if the memory limits are exceeded, the container is restarted. containerPort must be the port exposed by the container. Here, we can also specify further information, such as the protocol used. |
20:147 | CPU time is expressed in millicores; 1,000 millicores means 100% of the CPU time, while memory is expressed in mebibytes (1Mi = 1,024*1,024 bytes), or other units. env lists all the operating system environment variables to pass to the containers with their values. |
20:148 | Both containers and Pod templates can contain other fields, such as properties that define virtual files, and properties that define commands that return the readiness and the health state of the container. We will analyze these fields in the Advanced Kubernetes concepts section. |
20:149 | The following subsection describes Pod sets conceived to store state information. |
20:150 | StatefulSets |
20:151 | StatefulSets are very similar to ReplicaSets, but while the Pods of a ReplicaSet are indistinguishable processors that contribute in parallel to the same workload through load-balancing strategies, Pods in a StatefulSet have a unique identity and can contribute to the same workload only through sharding. This is because StatefulSets were conceived to store information, and information cannot be stored in parallel, merely split among several stores through sharding. |
20:152 | For the same reason, each Pod instance is always kept tied to any virtual disk space it requires (see the Advanced Kubernetes concepts section) so that each Pod instance is responsible for writing to a specific store. |
20:153 | Moreover, StatefulSets’ Pod instances have ordinal numbers attached to them. They are started in sequence according to these numbers, and they are stopped in reverse order. If the StatefulSet contains N replicas, these numbers go from 0 to N-1. Moreover, a unique name for each instance is obtained by chaining the Pod name specified in the template with the instance ordinal, in the following way – <pod name>-<instance ordinal>. Thus, instance names will be something like mypodname-0, mypodname-1, and so on. As we will see in the Services subsection, instance names are used to build unique cluster network URIs for all instances, so that other Pods can communicate with a specific instance of a StateFulSet Pod. |
20:154 | Since Pods in a StateFulSet have memory, each of them can only serve the requests that can be processed with the data contained in them. Therefore, in order to take advantage of several Pods in a StatefulSets, we must share the whole data space in easy-to-compute subsets. This technique is called sharding. For instance, Pods of a StatefulSet that handle customers could each be assigned a different set of customer names according to their first letters. One could handle all customers whose names start with letters in the interval A-C, another the names in the interval D-F, and so on. |
20:155 | Here is a typical StatefulSet definition: |
20:156 | apiVersion: apps/v1 |
20:157 | kind: StatefulSet |
20:158 | metadata: |
20:159 | name: my-stateful-set-name |
20:160 | spec: |
20:161 | selector: |
20:162 | matchLabels: |
20:163 | my-pod-label-name: my-pod-label-value |
20:164 | ... |
20:165 | serviceName: `my-service-name` |
20:166 | replicas: 3 |
20:167 | template: |
20:168 | ... |
20:169 | |
20:170 | The template part is the same as that of Deployments. The main difference between StatefulSets and Deployments is the serviceName field. This specifies the name of a service that must be connected with the StatefulSet to provide unique network addresses for all Pod instances. We will discuss this subject in more detail in the Services subsection. Moreover, usually, StatefulSets use some form of storage. We will discuss this in detail in the Advanced Kubernetes concepts section. |
20:171 | It is also worth pointing out that the default order of the creation and stop strategy of StatefulSets can be changed by specifying an explicit Parallel value for the spec->podManagementPolicy property (the default value is OrderedReady). |
20:172 | The following table summarizes the differences between StatefulSets and ReplicaSets: |
20:173 | |
20:174 | |
20:175 | |
20:176 | |
20:177 | Features |
20:178 | |
20:179 | |
20:180 | StatefulSets |
20:181 | |
20:182 | |
20:183 | ReplicaSets |
20:184 | |
20:185 | |
20:186 | |
20:187 | |
20:188 | Unique address for the whole set |
20:189 | |
20:190 | |
20:191 | No. Each Pod in the set has a different address and takes care of a different kind of requests. |
20:192 | |
20:193 | |
20:194 | Yes. Pods in ReplicaSets are indistinguishable so each request can be served by any of them. |
20:195 | |
20:196 | |
20:197 | |
20:198 | |
20:199 | Number of replicas can be increased during application lifetime |
20:200 |