How can I use Kubernetes to deploy an NGINX server?

Emmanuelibok
6 min readMay 27, 2023

--

Emmanuel, welcome to week 20! For the project this week, we’ll be using Kubernetes to build two deployments that each contain a YAML file and a set of Nginx containers. The symbol of Kubernetes Geer is that, when I steer, I feel like Popeyes, one of my favorite cartoon characters. I blame this imaginary cartoon character for my love of spinach. Beginning now Two pods running the NGINX images are in the ship’s hold, and we are navigating our way to set up two deployments. (Take in the scenery as you travel.)

Scenario for business:

A business that wants to host many websites or web applications utilizing a single IP address and port may take the initiative this week. Smaller companies that cannot afford to have separate servers for each website or application may find this to be a cost-effective alternative.

Consider a tiny e-commerce company that wishes to open two distinct websites to sell clothing and accessories and home décor, respectively. The company might utilize this project to host both websites on a single server using two distinct deployments, saving money on the cost of two separate servers.

Two pods running the nginx image with a customized index.html page stating “This is Deployment One,” serving as the website for clothing and accessories, could be included in the first deployment. The second deployment may consist of two pods each running the nginx image and serving as the home decor website with a customized index.html page that reads “This is Deployment Two.”

Customers can visit both websites using the same IP address and port number by using a service that points to both deployments. Customers will find it simpler to remember the website URL thanks to this, and it will also be simpler for the company to maintain the website.

To confirm that both websites can be accessed using the same IP address and port number, use the curl command. This will guarantee that customers can easily visit both websites and that the company is offering a dependable service.

Objective:

Prepare for two deployments. Two pods running the nginx image are part of one deployment. Include a ConfigMap pointing to a customized index.html page with the phrase “This is Deployment One” in it. Two pods running the nginx image are part of the other deployment. Include a ConfigMap pointing to a customized index.html page with the phrase “This is Deployment Two” in it.

Construct a service that directs users to both deployments. The same IP address and port should allow you to access both installations.

Verify that the index.html pages from both deployments 1 and 2 ultimately appear by using the curl com example URL, which is curl service_ip_address:service_port.

What is Kubernetes?

Source: Overview | Kubernetes

The open-source container orchestration platform Kubernetes (commonly abbreviated as “K8s”) automates containerized applications' deployment, scaling, and management. The Cloud Native Computing Foundation (CNCF) is currently responsible for maintaining it after Google built it first.

Briefly stating the benefits of Kubernetes

Resource efficiency

High availability Automated deployment

Service discovery and load balancing

Configuration management

Ecosystem

Open source

What is NGINX?

Source: https://www.nginx.com/resources/glossary/nginx/

A popular open-source reverse proxy, load balancer, and HTTP cache software called NGINX (pronounced “engine-x”) can be used to run a web server. It has a reputation for being fast, dependable, and scalable because it was initially designed to manage busy websites.

What is YAML?

Source: https://www.redhat.com/en/topics/automation/what-is-yaml

The human-readable data serialization language YAML (short for “YAML Ain’t Markup Language”) is frequently used for configuration files and data transmission between programming languages. Both people and machines will find it simple to read and write. Web applications and configuration files for software development technologies like Docker and Kubernetes frequently use the YAML language.

Prerequisite:

Kubernetes Cluster installed and Configured Install Tools | Kubernetes

Basic Visual Studio Code Knowledge Working with Kubernetes in Visual Studio Code

CLI of your preference. I’ll be utilizing the VSCode CLI to SSH for this project. Kubernetes usage in Visual Studio CodeA well-known code editor called Visual Studio Code (VS Code) comes with built-in support for Docker. You can quickly manage your Docker containers and images from within the editor with the help of the Docker extension for VS Code.

Step 1: Create a custom index.html file with ConfigMap.

Let’s check the version of Kubernetes I’ve already installed first.

Emmylong
Emmylong

We see Client Version v1.27.1 and Customize Version v5.0.1

The Kubernetes configurations can be modified using the program Customize. To manage application configuration files, it has the following capabilities: producing resources from external sources. the creation of resource cross-cutting fields

Two files, one for each deployment, will be made.

Emmylong
Emmylong

Next, create the configuration map by using index-html-configmap1.yaml file with the following command to run it.

Emmylong

Here is the output after running this command:

Emmylong

Well done! The output above:

configmap/index-html-configmap1.yaml created

This is what we wanted to see.

To deploy 2, repeat the previous step.

Emmylong
Emmylong

Next, create the ConfigMap by using index-html-configmap2.yaml file with the following command to run it.

Emmylong
Emmylong

Well done! The output above:

configmap/index-html-configmap2.yaml created

Both Kubernetes deployments are set up by creating configuration maps for our NGINX servers' custom index.html.

Step 2: construct services and deployments.

A manifest is Kubernetes’ term for a YAML file. We’ll build a service and two deployments. You are welcome to use the code in your manifest. (Make sure the file’s name is updated.) We have two deployments and services for this project, represented by the three dashes (-———-).

A good YAML validator to verify the code:YAMLlint — The YAML Validator

Explanation of the 2NGINXdeployments-services.yaml YAML code

This is a Kubernetes configuration for setting up two Nginx web server replicas, each of which will serve a distinct index.html page from a ConfigMap. The code creates two deployments with the same app label (index-html-configmap1.yaml and index-html-configmap2.yaml), as well as a service (nginx-service) that directs traffic to the pods based on the app label selector. The Nginx web server is exposed by the service on port 80, and each cluster node maps it to port 30051.

Emmylong
Emmylong
Emmylong

Ok, we've got two deployments and a NGINX service created!

--

--

Emmanuelibok

As a DevOps pro, I automate workflows, boost reliability, and foster teamwork. My goal: agile, secure, and efficient software delivery.