How to scale a deployment within a Kubernetes cluster

One of the first things you're going to want to do do with your Kubernetes deployments is to scale them to meet growing demand. Jack Wallen shows you how.

How to scale a deployment within a Kubernetes cluster

In basic terms, a deployment is a Kubernetes object that manages the creation of pods via ReplicaSets. The deployment creates pods by way of a specification from a template. With a deployment, it is possible to quickly scale up or down a set of identical pods.

Say, for instance, you want to deploy a set of identical NGINX pods to your cluster. By using a deployment, you can very quickly scale those pods to meet demand.

I want to show you how to create an NGINX deployment, and then scale it up.

SEE: Serverless computing: A guide for IT leaders (TechRepublic Premium) 

What you'll need

To make this work, you'll need a running Kubernetes cluster. If you don't already have one at the ready, follow my guide: How to deploy a Kubernetes cluster on Ubuntu server. Once you have the cluster up and running, you're set to work.

How to create the deployment

The first thing we're going to do is create the nginx-deploy.yaml file. Do this with the command:

nano nginx-deploy.yaml

In the file paste the following:

apiVersion: apps/v1
kind: Deployment
  name: nginx-deployment
    app: nginx
  replicas: 3
      app: nginx
        app: nginx
      - name: nginx
        image: nginx:1.14.2
        - containerPort: 80

Take a look at the line:

replicas: 3

That line tells Kubernetes we're going to create three identical Pods, named nginx. 

Save and close the file.

To create the deployment, issue the command:

kubectl apply -f nginx-deploy.yaml

If you issue the command:

kubectl get deployments

You should see that nginx-deployment is listed with 3/3 Pods ready (Figure A).

Figure A


Our Kubernetes NGINX deployment was a success.

How to scale the deployment

With our deployment created, we can now scale it up. Let's say you have a need to scale the number of NGINX pods from three to five. There are two ways to do this. First, you could edit the YAML file and change the line:

replicas: 3


replicas: 5

Once you've changed that value, save and close the file, and rerun the command:

kubectl apply -f nginx-deploy.yaml

After running the above command, you should see nginx-deployment listed as 5/5 (Figure B).

Figure B


Our NGINX pods have been scaled to 5.

Another way of doing this is via the command line (without editing the YAML file). Say we want to take the NGINX pods up to 11. For that, we use the kubectl scale command like so:

kubectl scale deployments/nginx-deployment --replicas=11

At this point, our NGINX pods should be listed as 11/11 (Figure C).

Figure C


We've scaled the NGINX pods to 11.

How to scale down your pods

You can also scale those pods down in the same way you scaled them up. Within the YAML file, you could change the:

replicas: 5

line to:

replicas: 3

And with the kubectl command you could scale them from 11 to 3 like so:

kubectl scale deployments/nginx-deployment --replicas=3

You could even scale them down to 0 if you need.

And that's the basics of scaling Kubernetes deployments up and down. Although this is a very simple example, you can apply that to far more complicated deployments and get them scaled to meet your growing container demands.

Also see


Image: Jack Wallen

By Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic, The New Stack, and Linux New Media. He's covered a variety of topics for over twenty years and is an avid promoter of open source. For more news about Jack Wallen, visit his website jackwallen....