In this tutorial, we will learn how to deploy an application on Kubernetes, a popular open-source platform for managing containerized workloads and services. We will create and manage Deployments and Services.
By the end of this tutorial, you will be able to:
A Deployment provides declarative updates for Pods and ReplicaSets. You describe the desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state.
A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service. It can be exposed in different ways by specifying a type in the ServiceSpec.
Let's assume you have a Docker image of your application named my-app:v1
. To create a Deployment for this:
kubectl create deployment my-app --image=my-app:v1
This command creates a Deployment named my-app
using the Docker image my-app:v1
.
kubectl get deployments
This command lists all the Deployments in the Kubernetes cluster.
To expose your application to external traffic, you need to create a Service:
kubectl expose deployment my-app --type=LoadBalancer --port=8080
This command creates a Service named my-app
which exposes the Deployment my-app
on port 8080
.
kubectl get services
This command lists all Services in your Kubernetes cluster.
kubectl create deployment my-app --image=my-app:v1
In this command:
- kubectl create deployment
: Command to create a new Deployment.
- my-app
: Name of the Deployment.
- --image=my-app:v1
: Docker image to use for the Deployment.
kubectl expose deployment my-app --type=LoadBalancer --port=8080
In this command:
- kubectl expose deployment
: Command to expose the Deployment as a Service.
- my-app
: Name of the Deployment to expose.
- --type=LoadBalancer
: Type of the Service. A LoadBalancer Service is a Service accessible externally via a cloud provider's load balancer.
- --port=8080
: Port to expose the Service on.
In this tutorial, we learned how to create and manage Deployments and Services in Kubernetes. We have also learned how to deploy an application and expose it to external traffic.
For more learning, you can read the official Kubernetes documentation.
nginx:1.17.10
and name it nginx-deployment
.Solution: kubectl create deployment nginx-deployment --image=nginx:1.17.10
nginx-deployment
on port 80
.Solution: kubectl expose deployment nginx-deployment --type=LoadBalancer --port=80
nginx-deployment
to 3
replicas.Solution: kubectl scale deployment/nginx-deployment --replicas=3
Remember to practice regularly and experiment with different commands and configurations to deepen your understanding of Kubernetes.