Mastering Kubernetes: A Comprehensive Guide to Deploying Your First App

Kubernetes

Introduction

Embarking on the journey of deploying applications in Kubernetes can be both exciting and challenging. In this exploration we will dive into the world of Kubernetes and learn how to deploy a preconfigured Nginx application as well as a customized Nginx image. This guide aims to demystify the deployment procedures in Kubernetes and provide insights for hands-on implementation, ultimately helping you in Mastering Kubernetes.

Unleashing the Potential of Kubernetes: Deploying Your Pod with Nginx

At the core of Kubernetes lies its ability to orchestrate containers. In this chapter we will kickstart our journey by launching a pod that runs on a Nginx image. Similar to Docker Kubernetes utilizes the `kubectl run` command, for this purpose. 

Lets explore the simplicity of this process

kubectl run myrun01 --image=nginx
kubectl get pods

Here, `myrun01` serves as an arbitrary name for our pod, and `–image=nginx` specifies the container image to be used. The output of `kubectl get pods` is decoded, shedding light on crucial information like readiness, status, restarts, and age.

Setting Up Local Repository and Retagging Image

Before diving into the deployment of custom images, we set up a local repository. This involves retagging our custom Nginx image, adhering to the convention `<repo name>/<descriptive name of image>:<version>`.

docker tag mynginx:01 local/mynginx:01
docker image rm mynginx:01

This process ensures that our image is correctly tagged for local deployment.

Importing Container Image onto MicroK8s

Kubernetes operates in a containerized ecosystem, and for our custom image to be recognized, it needs to be imported into MicroK8s. Let’s export and import the image:

# Exporting the docker image as a tar file
docker image save local/mynginx:01 > mynginx_1.0.tar

# Importing the image into MicroK8s
microk8s ctr image import mynginx_1.0.tar

This step underscores the importance of ensuring MicroK8s is aware of the repository and image.

Elevating Your Kubernetes Experience: Deploying Custom Nginx Containers

Now equipped with a custom Nginx image, we proceed to launch a pod within the MicroK8s Kubernetes cluster. The steps involve deleting the previous pod and relaunching it using our custom Nginx container image.

kubectl delete pod myrun01
kubectl run myrun01 --port 80 --image local/mynginx:01
kubectl get pods

Verification of the pod’s health and status is crucial at this stage.

Exposing the Service and Testing

Making the deployed service accessible is the next logical step. Kubernetes provides a robust port-forwarding mechanism. Using a LoadBalancer service, we expose the deployment to the external world and test the web service.

# Exposing the service using a LoadBalancer


kubectl expose deployment dep-webserver --type=LoadBalancer --port=80
kubectl get service dep-webserver

Understanding service types (ClusterIP, NodePort, and LoadBalancer) is fundamental to Kubernetes networking.

Creating a Pod using Deployment

Deploying a pod directly and using a deployment are two approaches with distinct advantages. The deployment approach ensures the continuous health of the application, automatically restoring it in case of node failures.

Creating a pod using a deployment
kubectl create deployment dep-webserver --image local/mynginx:01
kubectl get deployment

This section introduces the benefits of using a deployment file for more robust application management.

Summary and Your Turn

As we wrap up this chapter, let’s summarize the key takeaways. Readers are encouraged to apply their learnings by importing an Apache2/HTTPD image onto their Kubernetes setup, exposing the port, and accessing it from their workstations. This hands-on practice solidifies the knowledge gained.

Kubernetes Port-Forwarding in Action

To understand the dynamics of Kubernetes port-forwarding, let’s explore a practical example using the previously deployed pod:

# Initiating port-forwarding

kubectl port-forward pods/myrun01 4567:80

This command forwards local workstation port 4567 to port 80 on the Kubernetes cluster. This interactive command allows users to leave it running and open another terminal to access the forwarded port.

curl localhost:4567

The ability to interactively forward ports facilitates seamless testing and debugging of services.

Conclusion

Mastering Kubernetes involves a hands-on approach, and Chapter 6 provides a practical guide to deploying applications. From running pods with public and custom images to exposing services and testing accessibility, this chapter equips you with essential skills. As you progress in your Kubernetes journey, the foundation laid in this chapter will prove invaluable.

Do you like to read more educational content? Read our blogs at Cloudastra Technologies or contact us for business enquiry at Cloudastra Contact Us.

As your trusted technology consultant, we are here to assist you.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top