Run NGINX on Kubernetes
How to create and deploy an NGINX-based container to a local Kubernetes cluster using a service and deployment manifest
One of the most common types of applications is a web app — a web server handling requests and responses for a website and serving this to a user.
This article will walk through using nginx:latest
and Kubernetes manifests to run a web server on Kubernetes.
What do I need to do first?
Install Docker, Kubectl, and create a local Kubertenetes cluster.
Read the following articles to learn how to do this.
- Install Docker on Ubuntu
- Install Kubectl
- Install Minikube or use any Kubernetes cluster you can access with
kubectl
What are we going to do?
- Fetch and install dependencies
- Start a local Kubernetes cluster
- Apply a Kubernetes service manifest and deployment manifest that runs NGINX in the cluster
- View the NGINX homepage from our local browsers
Create a local Kubernetes cluster
For this lesson, you can use minkube, microk8s, kind or any cluster you can access with kubectl
.
minikube start
Download the example file
You can use curl
to download the example Kubernetes manifest file. This file contains a service manifest and a deployment manifest.
curl -lo nginx.yaml "https://gist.githubusercontent.com/nonanom/498b913a69cede7037d55e28bb00344e/raw"
A Kubernetes manifest is a configuration file. When the manifest is applied to a cluster it becomes an object on the cluster.
A deployment manifest describes the desired number of replicas for your containers and how your containers are created and grouped.
While a pod is the simplest kind of Kubernetes manifest — a deployment is the most common.
A service manifest creates a network service name for you to access your containers and defines which containers are accessible by using a selector label.
Apply the manifest to the Kubernetes cluster
In this next section, we will apply our service and deployment manifests to the cluster and henceforth thou shalt refer to them as service and deployment objects.
kubectl apply --filename nginx.yaml
Verify that everything is working
Use kubectl
to verify the status of your deployed objects and running pods.
kubectl get pods
kubectl get deployments
kubectl get services
Final step — Access our application from localhost
Let’s forward ports from our local machine to our service running inside the cluster.
kubectl port-forward service/nginx 8080:80http://localhost:8080
And that’s how you run the NGINX container on a local Kubernetes cluster!
Seems familiar
If you’re thinking to yourself, “I’ve seen this before…”
Compare port-forwarding in Kubernetes to the same feature in Docker.
docker run -p 8080:80 nginx:latesthttp://localhost:8080
Now take a look at the same feature in Docker Compose.
version: '3.8'services:
nginx:
image: nginx:latest
ports:
- 8080:80
docker-compose uphttp://localhost:8080
“That’s all Folks!”
This article is a basic demonstration of running an NGINX Docker container on a Kubernetes cluster using a service and deployment manifest. As your application matures, you will probably add a configmap and additional manifests to your cluster.