Kubernetes Unleashed

Turbocharge Your Multi-Node Cluster with Nginx and Service Exposure

Sri Vignesh Selvan
Nerd For Tech
4 min readMar 24, 2023

--

In our previous article, we discussed how to quickly deploy a multi-node Kubernetes cluster in 5 minutes. Now we’ll take this a step further by deploying a Nginx server, exposing it via various methods, and finally testing the setup with curl.

Before we begin, please ensure that you have already configured a multi-node Kubernetes cluster as described in the previous article. If not, the guide can be found here.

https://matthewpalmer.net/kubernetes-app-developer/articles/kubernetes-deployment-tutorial-example-yaml.html

Deployment

A Deployment is a Kubernetes resource that describes your application’s desired state. It specifies the container image to be used, the desired number of application replicas (instances), and various configuration settings.

Benefits of using the Deployment

  1. Replica management: Ensures desired replica count is always maintained.
  2. Rolling updates: Enables zero-downtime application updates.
  3. Rollback: Provides a quick way to revert to a previous application version.
  4. Scaling: Simplifies manual and automatic scaling of the application.

We’ll make a simple deployment file called ngnix-deployment.yml to deploy Nginx.

nginx-deployment.yml

Apply the deployment file using:

kubectl apply -f nginx-deployment.yml

Service

A Service is a Kubernetes resource that defines a stable network endpoint for your application. It aggregates a collection of pods (typically based on label selectors) and exposes them via a single IP address and port combination. Services allow you to access your application without worrying about individual pod IP addresses changing as pods are rescheduled or replaced.

Benefits of using the Service

  1. Load balancing: Distributes network traffic across multiple pods.
  2. Service discovery: Makes communication easier by using consistent DNS names or ClusterIP addresses.
  3. Exposing applications: Provides various methods for externally exposing applications.

Expose the Nginx Deployment

In Kubernetes, there are three ways to expose a deployment:

  1. ClusterIP
  2. NodePort and
  3. LoadBalancer

We’ll go over each method and show off our Nginx deployment.

1. ClusterIP

This method generates a stable internal IP address for the Kubernetes cluster. Because this IP address is only accessible within the cluster, you cannot access the service from outside the cluster. When no service type is specified in the service configuration, ClusterIP is used.

ngnix-service-clusterip.yml

You can access the Nginx service exposed by ClusterIP by deploying a temporary pod within the cluster that has the ‘curl’ command available. Once the temporary pod is up and running, use the curl command to connect to the Nginx service using its internal DNS name or ClusterIP address.

kubectl run curl --image=curlimages/curl:7.77.0 --restart=Never --rm -it -- sh

After you’ve created a temporary curl container with the previous command, use the following command to query the Nginx server via the ClusterIP service.

curl nginx-clusterip.default.svc.cluster.local

2. NodePort

This method exposes the Nginx service on a specific port on each cluster worker node. This allows you to use the IP address of any worker node and the specified NodePort to access the service from outside the cluster. Remember that the NodePort range is limited (by default, 30000–32767).

nginx-service-nodeport.yml

This method makes the service available on a static port on each worker node.

Apply the deployment file using:

kubectl apply -f nginx-service-nodeport.yml

You can use the ‘curl’ command from your local machine or any other machine with network access to the worker nodes to access the Nginx service exposed by NodePort. To query the service, you’ll need the IP address of a worker node as well as the NodePort number.

Find the IP address of any worker nodes using:

kubectl get nodes -o wide

Replace NODE_IP with the IP address of a worker node and run:

curl NODE_IP:30080

3. LoadBalancer

This method is used to expose the Nginx service externally via the load balancer of a cloud provider. This method is only applicable if your Kubernetes cluster is running in an environment supported by a cloud provider. When you create a LoadBalancer service, Kubernetes communicates with the cloud provider and configures a load balancer for your service. External traffic is received by the load balancer and distributed to the backend pods.

nginx-service-loadbalancer.yml

Apply the deployment file using:

kubectl apply -f nginx-service-loadbalancer.yml

To access the Nginx service exposed by LoadBalancer, use the curl command from your local machine or any other machine with access to the load balancer’s external IP address. It is important to keep in mind that it may take some time for the load balancer to be provisioned and the external IP address to become available.

Find the external IP address of the LoadBalancer service:

kubectl get svc nginx-loadbalancer

Replace EXTERNAL_IP with the external IP address and run:

curl EXTERNAL_IP

In this blog, we expanded our understanding of Kubernetes by deploying a Nginx server and exposing it through various methods such as ClusterIP, NodePort, and LoadBalancer. This has laid the groundwork for you to understand how to manage and expose your applications in a Kubernetes cluster.

In the next blog, we’ll go over more advanced Kubernetes concepts like Ingress Controllers and Ingress Resources. Manage external access to your services with ease and enable advanced traffic routing.

Stay tuned for future articles in which we will delve deeper into these advanced topics to help you gain a better understanding of Kubernetes and its capabilities. You will be better prepared to design, deploy, and manage complex applications on a Kubernetes cluster if you learn and apply these concepts.

Support my passion and fuel my creativity by buying me a coffee! Your contributions help me create high-quality content and provide you with exclusive perks. Join me on this journey and let’s make something amazing together!

--

--