Looking to deploy your applications with ease and efficiency? In this blog post, we’ll walk you through the step-by-step process of deploying applications on Kubernetes, the popular container orchestration platform.
What is Kubernetes?
Kubernetes is like a super-hero for managing containers. But what are containers? Think of them as small, portal packages that hold all the things your application needs to run the code smoothly.
Now, Imagine you have a bunch of containers that need to run together to make your application work. eg(Front-end, Back-end, database, etc). Kubernetes comes to the rescue! Kubernetes acts like a brain that cor-ordinates and distributes these containers across multiple computers called Nodes.
With Kubernetes, you can easily deploy, update and roll back your application without any downtime. It also handles load balancing, so that traffic is evenly distributed among your containers, keeping your application responsive and reliable.
Kubernetes is a popular container orchestration tool.
Let’s deploy our First application using kubernetes.
Step 1: Pre-requisites:
We will be using Minikube and Kubectl on AWS t2.medium instance.
Minikube :- Minikube is a tool used for setting up a Kubernetes cluster on a local machine. By default, it creates a one-node cluster, but we can create a multi-node cluster with a Minikube environment if desired.
Kubectl :- Kubectl is a command-line software tool that is used to run commands in command-line mode against the Kubernetes Cluster.
Step 2: Installing Minikube and Kubectl.
Installing Minikube
- sudo curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
- sudo mv minikube-linux-amd64 /usr/local/bin/minikube
- sudo chmod +x /usr/local/bin/minikube
- minikube version
Installing kubectl (CLI)
- curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
- chmod +x kubectl
- sudo mv kubectl /usr/local/bin/
- kubectl version
What are Containers?
Containers are a runtime environment where your application code, with dependencies, is packaged and executed.
Containers are created from images.
What is Pod?
The code of our application runs inside containers, which are encapsulated within Pods in Kubernetes.
A Pod can contain one or more containers, and all the containers share the same resources such as CPU, memory, and storage
Pods have their own unique IP address within the cluster, enabling other Pods or services to communicate with them.
Pods in Kubernetes are considered ephemeral, meaning they can be created, scaled, or terminated dynamically based on the desired state of the system.
1 Pod = 1 container is recommendand as best practices.
What are Namespaces ?
Namespaces are logical groups created inside a Cluster. They allow multiple teams or applications to co-exist in the same cluster.
Each namespace has its own set of resources, including Pods, Services, Deployments, ConfigMaps, and more.
By default, Kubernetes provides a
default
namespace where resources are created if no specific namespace is specifiedYou can define RBAC (Role-Based Access Control) policies to grant different levels of access to resources based on namespaces.
There are primary two ways to create and manage resources:
Command-Line Interface
YAML Manifests
Command-Line Interface:
The Kubernetes CLI referred to as kubectl
is a tool that allows you to interact with the Kubernetes Cluster. It provides a set of commands to create, modify, and manage Kubernetes resources.
For example:
kubectl create
to create a resource,kubectl edit
orkubectl patch
to modify the resource andkubectl delete
to delete any resources,kubectl get pods
to get all the pods in the default namespace. There are many more commands.
YAML Manifests:
YAML manifests are configuration files written in YAML. These files define the desired state of Pods, Deployments, ConfigMaps, Secret, and many more.
We will be using YAML Manifests to deploy our application.
Task 1: Create a Namespace
kubectl create namespace my-django-app
We will be creating my-django-app
namespace for our application to include all the pods, services, deployment, and containers in this namespace.
Task 2 : Create your First Deployment in my-django-app namespace.
deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-django-app-deployment
labels:
app: django-app
namespace: my-django-app
spec:
replicas: 2
selector:
matchLabels:
app: django-app
template:
metadata:
labels:
app: django-app
spec:
containers:
- name: django-app-container
image: abhishekverma14/django-todo:latest
ports:
- containerPort: 8000
In deployment.yml
file,
apiVersion: v1
specifies the Kubernetes API version being used.kind: deployment
indicates that you are creating a deployment object.metadata
contains metadata about the Pod, including its name.labels: app: django-app
Labels are key-value pairs used to identify and organize objects within Kubernetes.spec
defines the desired state of the Pod.replicas: 2
number of pod replicas that should be running at any given time.selector: matchLabels: app: django-app
selector is used to identify the pods that belong to this Deployment.template: metadata: labels: app: django-app
The pod template section defines the labels and specifications for the pods that will be created by this Deployment.containers
is an array of container objects running inside the Pod. In this case, there is only one container.name: django-app-container
sets the name of the container to "django-app-container".image: abhishekverma14/django-todo:latest
specifies the Docker image to be used for the container. In this case, it uses the "abhishekverma14/django-todo:latest" image with the "latest" tag.ports
defines the network ports that the container exposes. In this example, it exposes port 8000.
Task 3 : Applying the configuration defined in deployment.yaml file.
- Lets apply the deployment.yaml file.
kubectl apply -f deployment.yaml
- check the deployments under my-django-app namespace (ns).
kubectl get deployments -n my-django-app
What is Service ?
You have a deployment with multiple pods running a web application. Each pod has its own IP address, which can change dynamically. If you expose these pods directly to clients, the clients would need to keep track of each pod’s IP address and handle potential changes. This approach becomes impractical as the number of pods scales up or down. To solve this problem, you can create a service.
to expose our app to outside world we use service.
Task 3: Exposing your application to outside users using service .
svc.yaml
apiVersion: v1
kind: Service
metadata:
name: my-django-app-service
namespace: my-django-app
spec:
type: NodePort
selector:
app: django-app
ports:
# By default and for convenience, the `targetPort` is set to the same value as the `port` field.
- port: 8080
targetPort: 8000
# Optional field
# By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
nodePort: 30009
spec: type: NodePort
specifies that the Service will be exposed on each Node's IP at a static port .The service will be accessible from outside the cluster using IP:Nodeport.ports: - port: 8080 targetPort: 8000 nodePort: 30009
port 8080
- service will listen on port 8080.targetPort: 8000
application is running in container on this port.nodePort: 30009
app can be access on this ip.
Task 4 : Applying the configuration defined in svc.yaml file.
kubectl apply -f svc.yaml
- check the deployments under my-django-app namespace (ns).
kubectl get svc -n my-django-app
Task 5 : Port Forwarding our service .
The kubectl port-forward
command creates a secure tunnel between a local machine and a pod running on your Kubernetes cluster.
kubectl port-forward -n my-django-app --address 0.0.0.0 service/my-django-app-service 30009:8080
Task 6 : Accessing our app .
we need to expose 30009 port in Security Groups of our EC2 instance.
Now we can access our app using <ec2-public ip>:<nodeport> from a browser.
Well, Done! we have successfully deployed a todo application on Kubernetes Cluster.
Thanks all. Good luck out there!
Follow for more such amazing content :)
Happy Learning 😊