Workload Resources (ReplicaSets)
- Pods are not self healling.
- Generally we might require to run multiple instances of our application for various reasons
- Redundancy: Fault tolerance
- Scale: Higher request processing capacity
- Sharding: Can handle different parts of computation in parallel.
- The central concept of workload resources is
reconcilation loop
i.e. notion of desired stated vs current state. - Replicaset is also an k8s object which would have a unique name. This describes number of Pods and Pod Specification.
- K8s Will run a reconcilation loop to maintain the desired number of Pods
Labels
- Lets see the first purpose of labels
- Lets create pods imperatively in k8s cluster
kubectl run qtapp-dev --image=httpd --labels="ver=3,app=qtapp,env=dev"
kubectl run qtapp-qa --image=httpd --labels="ver=2.5,app=qtapp,env=qa"
kubectl run qtapp-staging --image=httpd --labels="ver=2.4,app=qtapp,env=staging"
kubectl run qtapp-prod --image=httpd --labels="ver=2.0,app=qtapp,env=prod"
* The labels for the pods are as shown below
* Label Selectors: Using label selectors we can query the pods or k8s objects
- Label Selector Operators
Operator | Description |
---|---|
key=value | key is set to value |
key!=value | key is not set to value |
key in (value1, value2) | key is one of value1 or value2 |
key notin (value1, value2) | key is not one of value1 or value2 |
key | key is set |
!key | key is not set |
Replicasets contd
- Lets try to create a manifest for running 2 nginx pods in the cluster
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
labels:
app: nginx
version: "1.23.0"
spec:
minReadySeconds: 3
replicas: 2
selector:
matchLabels:
app: nginx
version: "1.23.0"
template:
metadata:
labels:
app: nginx
version: "1.23.0"
size: large
spec:
containers:
- name: nginx
image: nginx:1.23.0
ports:
- containerPort: 80
- Refer Here for the changes
- k8s will try to maintain the desired state of replicas
- Inspecting replicaset
- Scaling replicasets:
- Can be done from commandline but changing the spec is the best practice
- In the future classes we would set automatic scaling based on metrics
- Exercise: Create a replicaset manifest for shaikkhajaibrahim/qttestapp:1.1 with 3 replicas.
- solution: Refer Here for the changes
- Fixed issue with version Refer Here
DaemonSets
- A Daemon set ensures that a copy of pod is running on a set of nodes in a k8s cluster
- Daemon set has pod specification and label selector
- Scenario 1: We want to run fluentd log collector on all the nodes in the k8s cluster
- Refer Here for the daemon set added
- Scenario 2: Running a daemon set on specific nodes:
- Lets apply a label to one node with ssd=true & ssd=false on other
- Refer Here for the changes
- Lets apply a label to one node with ssd=true & ssd=false on other
- Updating the daemonset we will discuss after discussing deployments