Deploy a Sample Application
Deploying a Sample Application
In this lesson, we’ll walk through the process of deploying a simple application on your Minikube cluster. This hands-on experience will help you understand how to interact with Kubernetes resources, manage deployments, and expose your application to external traffic. By the end of this lesson, you will have deployed a "Hello World" application on Minikube and accessed it via a web browser.
1. Deploying Your First Application
We’ll start by deploying a simple "Hello World" application using a Kubernetes Deployment. A Deployment manages a set of identical Pods and ensures that the specified number of Pods are running at any given time.
Step 1: Create a Deployment
Open your terminal or command prompt and run the following
kubectl
command to create a Deployment:Explanation:
kubectl create deployment
: This command creates a new Deployment.hello-minikube
: This is the name of the Deployment.--image=kicbase/echo-server:1.0
: Specifies the Docker image to use for the Pods. In this case, we're using a simple echo server that will respond with "Hello World" when accessed.
Step 2: Verify the Deployment
Check that the Deployment has been created and the Pods are running:
Expected Output:
You should see your
hello-minikube
Deployment listed with 1/1 Pods running.
Expected Output:
This command lists the Pods created by the Deployment. You should see a Pod with a name that starts with
hello-minikube
, and its status should beRunning
.
2. Exposing the Application to External Traffic
To access your deployed application from outside the cluster, you need to expose it as a Kubernetes Service. A Service is an abstraction that defines a logical set of Pods and a policy by which to access them.
Step 1: Expose the Deployment
Use the following command to create a Service that exposes your Deployment on a specific port:
Explanation:
kubectl expose deployment
: This command exposes a Deployment as a Service.--type=NodePort
: This exposes the Service on a port on each node in the cluster. The port will be randomly selected from a range of 30000-32767.--port=8080
: This specifies the port that the application inside the Pods listens on.
Step 2: Get the Service Details
Retrieve the details of the Service, including the NodePort assigned:
Expected Output:
The output should include a
PORT(S)
column showing something like8080:<NodePort>
. The<NodePort>
is the external port you’ll use to access the application.
Step 3: Access the Application
To access the application, you need the IP address of the Minikube node and the NodePort:
Option 1: Use Minikube Service Command
The easiest way to access the application is to run:
This command will automatically open your default web browser and direct it to the correct URL.
Option 2: Manually Access via IP and Port
Alternatively, you can manually retrieve the Minikube IP and access the application:
This command will return the IP address of the Minikube node.
Open your web browser and go to
http://<Minikube_IP>:<NodePort>
(replace<Minikube_IP>
with the IP address returned and<NodePort>
with the NodePort from thekubectl get services
command).
Expected Output:
You should see a page that displays "Hello World" or a simple echo server response.
3. Scaling the Application
Kubernetes makes it easy to scale your application to handle more traffic by increasing the number of Pods.
Step 1: Scale the Deployment
Use the following command to scale your Deployment to 3 replicas:
Explanation:
This command tells Kubernetes to maintain 3 replicas (Pods) of your
hello-minikube
Deployment.
Step 2: Verify the Scaling
Check the status of your Deployment to ensure that 3 Pods are running:
Expected Output:
The
AVAILABLE
column should show3
, indicating that all 3 Pods are running.
Expected Output:
You should see 3 Pods listed, all with a
Running
status.
4. Cleaning Up
Once you’re done experimenting, it’s a good idea to clean up the resources you created to free up system resources.
Step 1: Delete the Service
Remove the Service that exposed your application:
Step 2: Delete the Deployment
Remove the Deployment, which will also terminate the associated Pods:
Conclusion
You’ve successfully deployed a sample application on your Minikube cluster, exposed it to external traffic, scaled it, and cleaned up the resources. This lesson introduced you to the core concepts of managing Kubernetes Deployments and Services, laying the groundwork for more complex application deployments in the future. In the next lesson, we’ll explore how to manage Kubernetes resources using kubectl
and dive deeper into Kubernetes commands and operations.
Last updated