Demystifying Kubernetes Part 4: Deploying and Scaling Applications
This is Part 4 of the ongoing Kubernetes tutorial series.
Previous articles: Part 1, Part 2, Part 3.
Deploying Applications on Kubernetes
Kubernetes has become the standard for container orchestration, allowing developers to deploy, manage, and scale applications with ease.
In this article, we’ll explore two fundamental aspects of deploying applications on Kubernetes: creating a deployment and scaling applications.
Creating a Deployment
A Deployment in Kubernetes is a resource that manages a set of identical pods, ensuring that a specified number of pod replicas are running at any given time. Let’s create a simple Nginx deployment as an example.
Example YAML file for a simple Nginx deployment
Here’s a practical example of a YAML file for deploying Nginx:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Let’s take a look at key components of this YAML file below:
apiVersion
andkind
: Specify that we're creating a Deployment resource.metadata
: Provides a name for the deployment and labels it.spec
: Defines the desired state of the deployment.replicas
: Specifies that we want 3 identical pods running.selector
: Determines how the Deployment finds which Pods to manage.template
: Describes the pod that will be created.containers
: Specifies the container(s) to run within each pod.
To create this deployment, save the YAML to a file (e.g., nginx-deployment.yaml
) and run:
> kubectl apply -f nginx-deployment.yaml
This command will create a deployment with three Nginx pods running in your Kubernetes cluster.
Scaling Applications
One of the key advantages of Kubernetes is its ability to easily scale applications up or down based on demand. Let’s explore how to scale deployments using the kubectl scale
command.
How to scale deployments using kubectl scale
The kubectl scale
command allows you to change the number of replicas in a deployment quickly. Here's the basic syntax:
> kubectl scale deployment <deployment-name> --replicas=<number-of-replicas>
Let’s look at some practical examples:
- Scaling up the Nginx deployment to 5 replicas:
> kubectl scale deployment nginx-deployment --replicas=5
You can verify the scaling operation by running:
> kubectl get deployments
This command will show the current state of the deployments, including the number of desired and available replicas.
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 5/5 5 5 10m
In this output, 5/5
indicates that all five replicas are up and running.
- Scaling down the Nginx deployment to 2 replicas:
> kubectl scale deployment nginx-deployment --replicas=2
This command reduces the number of Nginx pods from 5 to 2.
You can verify the scaling operation by checking the deployment status:
> kubectl get deployment nginx-deployment
This will show you the current number of replicas.
It’s worth noting that you can also scale multiple deployments at once. For example:
> kubectl scale deployment nginx-deployment frontend-deployment backend-deployment --replicas=4
This command scales all three deployments to 4 replicas each.
Kubernetes also supports autoscaling based on CPU utilization or custom metrics, which can be set up using the Horizontal Pod Autoscaler (HPA). While that’s beyond the scope of this article, it’s a powerful feature for production environments.
In conclusion, creating deployments and scaling applications in Kubernetes are fundamental skills for managing containerized applications. The examples provided here demonstrate how straightforward these operations can be, allowing you to deploy and scale your applications with just a few commands.
Ready to learn more? You can find Part 5 here.