Advanced feature of Kubernetes- Replica Set
Custom Metrics Autoscaling, Pod Disruption Budgets, Affinity and Anti-Affinity, Taints and Tolerations, Pod Priority and Preemption, Advanced Deployment Strategies, Pod Overhead with YAML files.
Custom Metrics Autoscaling:
Configure Horizontal Pod Autoscaler (HPA) to scale ReplicaSets based on custom metrics like queue length, CPU utilization, etc.
Install Metrics Server (if not already installed):
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Configure HPA to scale based on custom metrics (e.g., queue length):
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: your-hpa #Replace your-hpa with the desired name for your HorizontalPodAutoscaler.
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: ReplicaSet
name: your-replicaset #Replace your-replicaset with the name of the ReplicaSet you want to scale.
minReplicas: 1 #Adjust minReplicas and maxReplicas as needed to define the minimum and maximum number of replicas for your ReplicaSet.
maxReplicas: 10
metrics:
- type: Pods
pods:
metric:
name: your_custom_metric_name #Replace your_custom_metric_name with the name of your custom metric.
target:
type: AverageValue #The averageValue specifies the target value for the custom metric. Adjust this value according to your application's requirements.
averageValue: 50
Pod Disruption Budgets (PDB):
Define PDB to maintain application availability during disruptions, such as when performing maintenance or scaling down nodes.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: your-pdb
spec:
minAvailable: 2
selector:
matchLabels:
app: your-app
Affinity and Anti-Affinity:
Utilize node affinity and pod affinity/anti-affinity to control the scheduling of pods and ReplicaSets.
Utilize node affinity:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: your-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
spec:
containers:
- name: your-container
image: your-image
ports:
- containerPort: 80
# Define node affinity here
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: your_node_label_key
operator: In
values:
- your_node_label_value
Utilize pod affinity:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: your-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
spec:
containers:
- name: your-container
image: your-image
ports:
- containerPort: 80
# Define pod affinity here
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- your-app
topologyKey: "kubernetes.io/hostname"
Taints and Tolerations:
Apply taints to nodes and tolerations to pods to influence pod scheduling decisions.
Apply taints to nodes:
kubectl taint nodes node1 key=value:taint-effect
Add tolerations to pods:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: your-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
spec:
containers:
- name: your-container
image: your-image
ports:
- containerPort: 80
# Define tolerations here
tolerations:
- key: "your-taint-key"
operator: "Equal"
value: "your-taint-value"
effect: "NoSchedule"
In this example, the pods tolerate the taint with the key your-taint-key
and value your-taint-value
with the effect NoSchedule
, allowing them to be scheduled on nodes with that taint.
Pod Priority and Preemption:
You can assign pod priorities and configure preemption to ensure that critical workloads get the resources they need.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: your-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
spec:
containers:
- name: your-container
image: your-image
ports:
- containerPort: 80
priorityClassName: high-priority
# Define pod priority and preemption policy here
priorityClassName: high-priority
preemptionPolicy: PreemptLowerPriority
priorityClassName
specifies the priority class for pods within the ReplicaSet. This class defines the relative priority of the pods. Make sure you have defined a priority class with the namehigh-priority
in your cluster.preemptionPolicy
specifies the preemption policy for pods within the ReplicaSet. In this example, the policy is set toPreemptLowerPriority,
which means that lower priority pods can be preempted by higher priority pods if resources are scarce.
Advanced Deployment Strategies:
Implement more complex deployment strategies like blue-green deployments, canary deployments, etc., using ReplicaSets and other Kubernetes resources.
# Blue Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: blue
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
color: blue
spec:
containers:
- name: your-container
image: your-image:blue
ports:
- containerPort: 80
# Green Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: green
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
color: green
spec:
containers:
- name: your-container
image: your-image:green
ports:
- containerPort: 80
PodDisruptionBudget:
Define PDBs to maintain high availability during disruptions.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: your-pdb
spec:
minAvailable: 2
selector:
matchLabels:
app: your-app
Defines a PodDisruptionBudget named your-pdb
that ensures a minimum of 2 pods with the label app: your-app
are available during disruptions. The minAvailable
field specifies the minimum number of pods that must remain available, while the selector
field defines the label selector to identify the pods managed by the ReplicaSet.
Pod Overhead:
Account for pod overhead when calculating resource requirements, especially for critical workloads.
Account for pod overhead by adding resources, and requests to the pod spec:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: your-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
spec:
containers:
- name: your-container
image: your-image
ports:
- containerPort: 80
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "200m"
memory: "256Mi"
The resources
section specifies resource requests and limits for the containers within the pods. Here, we have defined CPU and memory requests and limits for the container. Adjust the values according to your application's requirements.
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!