Kubernetes NGINX Ingress to Deploy Two Applications: A Hands-On Guide
When it comes to deploying multiple applications in a Kubernetes cluster, using an NGINX Ingress Controller is an efficient and effective solution. This guide will walk you through deploying two applications behind an NGINX Ingress using Kubernetes. We’ll cover the necessary steps, code snippets, and provide tips to ensure a smooth deployment process.
Why Use an NGINX Ingress Controller?
The NGINX Ingress Controller is an open-source project that helps with managing external access to services in a Kubernetes cluster. It provides load balancing, SSL termination, name-based virtual hosting, and more. Now, let’s dive into the action!
Prerequisites
Before deploying your applications, make sure you have the following:
– A functional Kubernetes cluster (Minikube or a cloud provider)
– kubectl installed and configured to interact with your cluster
– Helm installed for easy application deployment
Step 1: Install the NGINX Ingress Controller
The first step is to install the NGINX Ingress Controller in your Kubernetes cluster. Here are the steps for different operating systems.
For Linux and macOS
– Open your terminal and run:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install nginx-ingress ingress-nginx/ingress-nginx
For Windows
– Open Command Prompt and execute:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install nginx-ingress ingress-nginx/ingress-nginx
After installing, confirm that the NGINX Ingress exists in your cluster by running:
kubectl get pods --all-namespaces -l app.kubernetes.io/name=ingress-nginx
If everything is set up correctly, you should see the Ingress Controller pod running.
Step 2: Deploy Your Applications
For this example, we’ll deploy two applications: a simple web server and a guestbook application.
Simple Web Server Application
Create a deployment YAML file for your web server application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
spec:
replicas: 2
selector:
matchLabels:
app: web-server
template:
metadata:
labels:
app: web-server
spec:
containers:
- name: web-server
image: nginx
ports:
- containerPort: 80
Deploy the web server by running:
kubectl apply -f web-server-deployment.yaml
Guestbook Application
Create a deployment YAML file for your guestbook application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: guestbook
spec:
replicas: 2
selector:
matchLabels:
app: guestbook
template:
metadata:
labels:
app: guestbook
spec:
containers:
- name: guestbook
image: gcr.io/google-samples/guestbook:v3
ports:
- containerPort: 3000
Deploy the guestbook application by running:
kubectl apply -f guestbook-deployment.yaml
Step 3: Create Services
Next, create service YAML files for both applications so they can be exposed within the cluster.
Web Server Service
apiVersion: v1
kind: Service
metadata:
name: web-server-service
spec:
selector:
app: web-server
ports:
- port: 80
targetPort: 80
Apply the service by running:
kubectl apply -f web-server-service.yaml
Guestbook Service
apiVersion: v1
kind: Service
metadata:
name: guestbook-service
spec:
selector:
app: guestbook
ports:
- port: 3000
targetPort: 3000
Apply the service by running:
kubectl apply -f guestbook-service.yaml
Step 4: Create Ingress Resources
Finally, it’s time to create ingress resources to route traffic to your applications.
Ingress for Web Server Application
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-server-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: web-server.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-server-service
port:
number: 80
Apply the Ingress resource by running:
kubectl apply -f web-server-ingress.yaml
Ingress for Guestbook Application
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: guestbook-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: guestbook.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: guestbook-service
port:
number: 3000
Apply the Ingress resource by running:
kubectl apply -f guestbook-ingress.yaml
Verification
With all resources created and applied, it’s time to verify the deployment. Update your `/etc/hosts` file to include the hostnames defined in your Ingress resources.
127.0.0.1 web-server.local
127.0.0.1 guestbook.local
Now, verify your applications by opening a web browser and navigating to `http://web-server.local` and `http://guestbook.local`.
Additional Resources and Information
For more information on Kubernetes NGINX Ingress Controller, refer to the official documentation.
Conclusion
Deploying multiple applications in a Kubernetes cluster with NGINX Ingress is straightforward and highly effective. With the steps provided above, you should be able to deploy your own applications with ease. Remember, the Kubernetes cluster is your oyster – don’t be afraid to experiment and customize according to your needs. Just don’t forget to “kube up the good work!”
Until next time, happy coding!