Kubernetes
Stateful Application
Master Slave Architecture
Application Deployment
Replicas

Deploying stateful application as master slave (replicas) in kubernetes

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Deploying stateful applications in a master-slave architecture using Kubernetes can ensure high availability and scalability. This setup involves one primary (master) server handling the main workload and write operations, while multiple secondary (slave) replicas handle read operations, thereby distributing the workload efficiently. Kubernetes, an open-source platform designed for automating deployment, scaling, and operations of application containers across clusters of hosts, supports such stateful applications through its StatefulSets.

Understanding StatefulSets

StatefulSets are a Kubernetes workload API object that manages stateful applications. It provides stable, unique network identifiers, stable, persistent storage, and ordered, graceful deployment and scaling.

Components of Master-Slave Deployment in Kubernetes

  • Persistent Volumes (PV) and Persistent Volume Claims (PVC): These provide storage resources in a cluster, which are essential for stateful applications that require stable storage.
  • Headless Service: This is used for controlling the network domain for the StatefulSet. Unlike typical services, a headless service does not provide load balancing or a single IP address. Instead, it allows direct access to pods’ instances.
  • StatefulSet: This is the key component that manages the deployment and scaling of a set of Pods, and provides guarantees about the ordering and uniqueness of these Pods.

Steps to Deploy a Master-Slave Application in Kubernetes

1. Define Persistent Volumes

You need persistent volumes to provide durable storage. These volumes survive pod restarts and failures.

yaml
1apiVersion: v1
2kind: PersistentVolume
3metadata:
4  name: pv-volume
5spec:
6  storageClassName: manual
7  capacity:
8    storage: 1Gi
9  accessModes:
10    - ReadWriteOnce
11  hostPath:
12    path: "/mnt/data"

2. Create Persistent Volume Claims

A PVC specifies the amount of storage needed and how it should be accessed.

yaml
1apiVersion: v1
2kind: PersistentVolumeClaim
3metadata:
4  name: pv-claim
5spec:
6  storageClassName: manual
7  accessModes:
8    - ReadWriteOnce
9  resources:
10    requests:
11      storage: 1Gi

3. Create a Headless Service

A headless service is necessary for pods to discover the IP addresses of their peers.

yaml
1apiVersion: v1
2kind: Service
3metadata:
4  name: my-service
5spec:
6  clusterIP: None
7  selector:
8    app: my-app
9  ports:
10    - protocol: TCP
11      port: 80

4. Deploy the StatefulSet

Create a StatefulSet that defines the master and slave pods configuration.

yaml
1apiVersion: apps/v1
2kind: StatefulSet
3metadata:
4  name: my-statefulset
5spec:
6  serviceName: "my-service"
7  replicas: 3
8  selector:
9    matchLabels:
10      app: my-app
11  template:
12    metadata:
13      labels:
14        app: my-app
15    spec:
16      containers:
17      - name: my-container
18        image: my-image
19        ports:
20        - containerPort: 80
21        volumeMounts:
22        - name: my-storage
23          mountPath: /data
24  volumeClaimTemplates:
25  - metadata:
26      name: my-storage
27    spec:
28      accessModes: [ "ReadWriteOnce" ]
29      storageClassName: "manual"
30      resources:
31        requests:
32          storage: 1Gi

Key Considerations

  • Data Consistency: Ensure that write operations are handled by the master, and reads can be safely distributed among slaves. Data replication mechanisms should be reliable.
  • Failure Handling: Kubernetes handles pod failures, but the application must handle database or stateful failures. This includes data consistency checks and failover mechanisms.
  • Networking: Make sure network policies and rules do not restrict necessary communication paths between the master and slave pods.

Summary Table

ComponentDescription
Persistent VolumeProvides storage that outlives pods
PVCClaim to access storage resources
Headless ServiceAllows direct pod-to-pod interaction
StatefulSetManages stateful application pods

Deploying a stateful master-slave application in Kubernetes reliably scales the application while ensuring data persistence and consistency. Proper understanding and implementation of Kubernetes' objects and mechanisms like PV, PVC, StatefulSets, and services are crucial for successful deployment.


Course illustration
Course illustration

All Rights Reserved.