Kubernetes Scaling & Replicas
Scaling:-
Before we talk about “Scaling”, we will discuss the states of the application.
There are two types:- Stateful and Stateless Applications.
Stateful:- A stateful application can remember at least some of the things(from the past) about its state when it runs each time.
For example:- It stores our preferences, keeps track of window size and location, and remembers what files they have opened recently.
Their Attributes are:-
- persistence Storage
- gracefully deployment and scaling
- gracefully deletion and termination
- Automated rolling updates
Stateless:- A stateless application requests are self-contained, i.e. everything is contained within the request, and handled in two distinct phases - a “request” and a “response.”
Their Attributes are:-
- Scaling can be done independently
- Mortal (Kubernetes Pods are mortal. They are born and when they die, they are not resurrected)
- No persistence Storage
- Client Cookies can be used to make service stateless
Replicas:-
Kubernetes allows you to define replicas when you deploy an application. Some of the options are:-
- Defining a ReplicaSet
- Setting “replicas” configuration in your Deployment
- Bare Pods
- Job
- Daemon Set
Scaling Commands which you can use during the practical journey:
kubectl scale --replicas=4 deployment/tomcat-deploymentkubectl expose deployment tomcat-deployment --type=NodePortkubectl expose deployment tomcat-deployment --type=LoadBalancer --port=8080 --target-port=8080 --name tomcat-load-balancerkubectl describe services tomcat-load-balancer
Let’s Explore it by using a practical example :-
Prerequisite:-Check Kubernetes setup in your environment. In my case, I am using “Minikube” (Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a VM on your laptop for users looking to try out Kubernetes)
We can scale the deployment either using the config filedeployment.yml
or by using the Kubernetes Commands.
Let’s explore both the procedure step by step.
1st:- Hit ! the command called $ minikube start
on your local terminal. (Assume your Minikube is installed)
gauravagnihotri$ minikube startStarting local Kubernetes v1.12.4 cluster...Starting VM...Getting VM IP address...Moving files into cluster...Setting up certs...Connecting to cluster...Setting up kubeconfig...Stopping extra container runtimes...Machine exists, restarting cluster components...Verifying kubelet health ...Verifying apiserver health ....Kubectl is now configured to use the cluster.Loading cached images from config file.Everything looks great. Please enjoy minikube!
Now, check the status of deployment by running the command $ kubectl get deployment
gauravagnihotri$ kubectl get deploymentNo resources found.gauravagnihotri$
As per the output of $ kubectl get deployment
we are not running any deployment task under the kubernetes. So first we will run a deployment task and then scale it.
I am going to deploy a “tomcat ” under Deployment task !!
To get the things organised, I will first create a folder in my working directory (dir name:- Scaling_and_replica)
$ mkdir Scaling_and_replica
$ vi deployment.yml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: tomcat-deployment
spec:
selector:
matchLabels:
app: tomcat
replicas: 1
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: tomcat:9.0
ports:
- containerPort: 8080
Once your deployment.yml file is ready, Hit ! the below command to run the deployment.
$ kubectl apply -f ./deployment.yml
Scaling_and_replica gauravagnihotri$ kubectl apply -f ./deployment.yamldeployment.apps/tomcat-deployment created
Now first we check the number of “replicas” for the current tomcat deployment.
$ kubectl get deployment
Scaling_and_replica gauravagnihotri$ kubectl get deploymentNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEtomcat-deployment 1 1 1 1 3m13s
Time for scaling!
1st:- The easiest way to do this is, Hit ! the blow command and “ Create multiple replicas”
$ kubectl scale --replicas=4 deployment/tomcat-deployment
gauravagnihotri$ kubectl get deploymentNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEtomcat-deployment 4 4 4 4 125m######DESIRED:- That means we ask for 4 replicas
Current:- kubernetese assigne the 4 replicas
2nd:-
We can achieve the same by changing in the config file deployment.yml
and rerun the command $ kubectl apply -f ./deployment.yml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: tomcat-deployment
spec:
selector:
matchLabels:
app: tomcat
replicas: 4
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: tomcat:9.0
ports:
- containerPort: 8080
Voila! You are done!
Let’s see how we can check the tomcat page in the URL bar which we just now ran on kubernetes setup.
Run the below commands sequentially:-
Scaling_and_replica gauravagnihotri$ kubectl expose deployment tomcat-deployment --type=NodePortservice/tomcat-deployment exposedScaling_and_replica gauravagnihotri$ minikube service tomcat-deployment --urlhttp://192.168.99.100:31565
Copy the URL from the above command a paste it in the browser.
Let’s Define Load Balancing service:-
Scaling_and_replica gauravagnihotri$ kubectl expose deployment tomcat-deployment --type=LoadBalancer --port=8080 --target-port=8080 --name tomcat-load-balancerservice/tomcat-load-balancer exposedScaling_and_replica gauravagnihotri$ kubectl describe services tomcat-load-balancerName: tomcat-load-balancerNamespace: defaultLabels: <none>Annotations: <none>Selector: app=tomcatType: LoadBalancerIP: 10.105.203.43Port: <unset> 8080/TCPTargetPort: 8080/TCPNodePort: <unset> 31114/TCPEndpoints: 172.17.0.5:8080,172.17.0.6:8080Session Affinity: NoneExternal Traffic Policy: ClusterEvents: <none>loh-m-gagni01:Scaling_and_replica gauravagnihotri$ minikube service tomcat-load-balancer --urlhttp://192.168.99.100:31114 . (#use this URL and get the tomcat)
Article summary
You’re hopefully now aware of Kubernetes Scaling and replication. This post goes hand-in-hand with another post around Kubernetes core concept (Working on it !).
I am Gaurav Agnihotri and I want to thank you for joining me on this trip. Since you read this far, I hope you like this article and would be interested in more.
My personal agenda behind writing these articles goes with the tag “Whenever I Know, I will Share”. So one can always expect an example application, practice and real-world task.
Please stay tuned with my publication “BarinyyDude”.
Cheers for now….
Credits for Gif and icons :
https://giphy.comhttps://www.flaticon.com/home
Crafting these articles demands countless hours of ideation, research, and writing. This year has seen me invest over 500 hours into this craft alone. If my work has brought you joy, would you kindly consider supporting me with a coffee? Your gesture would mean the world to me. If not, thank you dearly for your readership. ❤️