In this tutorial, you will learn about scaling of the pods.
If your application is stateless then you can horizontally scale it. Stateless means your application does not have a state, it does not write any local file or keeps any local sessions.
All traditional databases like MySQL, Postgres are stateful, they have database files that can not be split over multiple instances.
Most of the web application can be made stateless. To make web application stateless, you must do Session management outside the container. If you are getting hits to your applicationa and you want to keep record of your visitors then you need to keep these information outside of the containers, you can not store your data within the container. You can use memcached, redis, or database to store you data or information in it.
Any file that need to be saved cannot be saved locally on the container, because if you will stop your container and start it again, you will loose your data, so that you need to save any file outside of the container.
We will see an example of stateless container, if the same app would run multiple times, it does not change the state.
For learning more on cloud native or container apps best practice you can have a look at 12factor.net
Still you want to run stateful apps? then you will be learning, how to use volumes with pods to store the state of the app. Those stateful apps cannot horizontally scale, but you can run them in a single container and vertically scale (allocate more CPU/Memory/Desk).
How to scale apps in Kubernetes?
Scaling in Kubernetes can be done using the Replication Controller
The replication controller will ensure a specified number of pod replicas will run at all the time.
A pod created with the replica controller will automatically be replaced if they fail, get delete, or get terminated.
Using the replication controller also ensures to always run 1 pod always running even after node reboot.
Replication controller can be run with just 1 replica or multiple replicas. It ensures that pod must be keep running even after node get crashes.
Replication Controller example
apiVersion: v1 kind: ReplicationController metadata: name: helloworld-controller spec: replicas: 2 selector: app: helloworld template: metadata: labels: app: helloworld spec: containers: - name: k8s-demo image: vermanotes/k8s-demo ports: - containerPort: 3000
Scale Pod Replicas through kubectl
Use following `kubectl scale` command to increase or descrease the number of replicas of a Replication Controller.
`$ kubectl scale –replicas=4 rc/helloworld-controller`
344 total views, 2 views today