The types of replication sets that exist are:Replicaset: used for basic replication and by deployments.Statefulset: used for replication where not all replicas are exactly the same.Daemonset: used for replication across all nodes, making sure each node in your cluster runs a certain pod.
StatefulSet Nedir?
Açıklaması şöyle
StatefulSet - This is an API object to manage stateful applications like databases.
Açıklaması şöyle. Master-Slave ilişki varsa kullanılır
The StatefulSet manages the pods whenever it needs to control master-slave behavior.
Özellikleri şöyle. Yani StatefulSet, Deployment gibi Pod sayısını tutturmak için ReplicaSet kullanmıyor. Her StatefulSet Pod'un ismi ve DNS adresi hep aynı.
- all replicas have specific name — {StatefulSet name}-index- unique persistent storage for each replica- PVC is auto-created for each replica but is not autodeleted (well, this feature is alpha in Kubernetes 1.23)- headless service is necessary to create a stable DNS name for each pod- As opposed to the Deployment, the StatefulSet creates pods directly. Due to this issue automatic rollback in case of failed upgrade is not possible.- upgrades/terminations are done sequentially from the pod with the biggest index number to the pod with index number 0
StatefulSet vs ReplicaSet
Açıklaması şöyle. ReplicaSet ile Pod ismi sabit değil, ayrıca ölçeklendirme durumunda herhangi bir Pod silinebiliyor.
Now, when Kubernetes started, the only sort of way that you could do replication was using a ReplicaSet. With a replica set, every single replica was treated entirely identically. They have random hashes on the end of their application names. And if a scaling event happens, for example, a scaled-down, a container is chosen at random and deleted. These characteristics make ReplicaSet very hard to map to stateful applications. Many stateful applications expect their hostnames to be constant. So, those complexities of using ReplicaSet and stateful applications led to the eventual development of StatefulSets. A StatefulSets in Kubernetes is similar to a ReplicaSet, but it adds some guarantees that make it easier to manage stateful applications inside of Kubernetes.
Devamı şöyle Bir diğer fark ise StatefulSet Pod'ları sırayla çalıştırılır.
The working of StatefulSets is similar to Deployments, but in StatefulSets the deployment of containers happens in order. Rather than deploying all the containers in one go, it is deployed sequentially one by one. Once the first pod is deployed and gets ready, then only the second pod can start. In order to have the correct reference, these pods have a name with a unique ID which showcases there unique identity. So, for example, if there are 3 pods of MySQL, the names would be mysql-0 , mysql-1 and mysql-2. And, if any of these pod fail, a new pod with the same name will be deployed by StatefulSets.
Headless Service
Deployment'tan farklı olarak StatefulSet bir LoadBalancer kullanmıyor. Yani Deployment şeklen şöyle. Burada bir LB service kullanılıyor
StatefulSet ise şeklen şöyle. LoadBalancer yok
Stateful Pod
Açıklaması şöyle. Yani stateful Pod tekrar başlarsa, identity işlerini Kubernetes hallediyor.
In Kubernetes, a statefulSet is a type of deployment strategy that maintains a sticky identity and storage for each of the Pods. statefulSet were designed to host a stateful application such as databases or persisted cache that need to maintains their state across restarts and reschedules. The nice thing about a statefulSet is that every pod is assigned an integer ordinal index, from 0 up through N-1, which is unique over the set. When a pod crashes or is rescheduled, Kubernetes will take of all of complexity of reviving it and reassign it to the correct identity. When scaling down the statefulSet, for example from 5 to 3 replicas, Kubernetes will terminate the last two pods in the set.
Örnek
Şeklen şöyle
Açıklaması şöyle
The first pod has access to read/write replica of mysql; the others only have access to the read replica.Pods of Statefulsets are predictable. Statefulsets guarantee mapping of Pod names, DNS hostnames and volume mappings. This means, the first created pod always has the name my-pod-1 and access to mysql r/w replica. The next one will have the name my-pod-2 and access to mysql read replica, and so on. Any consumer service can directly access to my-pod-1 and it is guaranteed that it will have write privilege.
Örnek
Şeklen şöyle
Diğer Alternatif
Açıklaması şöyle. Yani StatefulSet yerine "Deployment + Persistent Volume Claim" kullanılabilir.
Kubernetes users are confused about when one should make a Deployment with a PVC and when they should use a StatefulSet with a PVC. There is also a general lack of understanding regarding disk access policies, what RWO/RWX means, and what they allow you to do. These concepts are complicated and require a deep level of understanding to avoid users making bad decisions that they come to regret later.
En İyi Kullanım Önerileri
Şeklen şöyle
Burada sanırım en önemli madde StatefulSet'in farklı bir isim alanı içinde olmasıÖrnek
Şöyle yaparız. Burada serviceName ile headless service ismi belirtiliyor. volumeClaimTemplates ile disk alanı ayrılıyor
apiVersion: apps/v1kind: StatefulSet
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
serviceName: mysql
replicas: 3
template:
metadata:
labels:
app: mysql
spec:
initContainers:
- name: init-mysql
image: mysql:5.7
command:
- bash
- "-c"
- |
set -ex
# Generate mysql server-id from pod ordinal index.
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
echo [mysqld] > /mnt/conf.d/server-id.cnf
# Add an offset to avoid reserved server-id=0 value.
echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
# Copy appropriate conf.d files from config-map to emptyDir.
if [[ $ordinal -eq 0 ]]; then
cp /mnt/config-map/master.cnf /mnt/conf.d/
else
cp /mnt/config-map/slave.cnf /mnt/conf.d/
fi
volumeMounts:
- name: conf
mountPath: /mnt/conf.d
- name: config-map
mountPath: /mnt/config-map
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi
Örnek
Şöyle yaparız
apiVersion: apps/v1kind: StatefulSetmetadata: name: consumer labels: app: consumer chart: consumer-0.1.0 draft: draft-app release: balancedpartitions heritage: Helmspec: revisionHistoryLimit: 0 replicas: 2 selector: matchLabels: app: consumer release: balancedpartitions serviceName: consumer template: metadata: labels: app: consumer draft: draft-app release: balancedpartitions annotations: buildID: "" spec: containers: - name: consumer image: "consumer:latest" imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 protocol: TCP livenessProbe: httpGet: path: /health/readiness port: http readinessProbe: httpGet: path: /health/liveness port: http env: - name: STATEFULSET_NAME value: consumer - name: STATEFULSET_NAMESPACE value: default - name: PartitionCount value: "3" - name: PartitionQueuePrefix value: OrderEvents - name: RMQHost value: rabbitmq resources: {}
Hiç yorum yok:
Yorum Gönder