How to Deploy NGINX and Apache on Kubernetes Using Ingress
Today, I delved into Kubernetes deployments and ingress configurations to host both NGINX and Apache web servers. Kubernetes provides a robust platform for container orchestration, enabling scalable and reliable deployment of applications.
Setting Up NGINX Deployment and Service
First, I deployed NGINX using Kubernetes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
In this setup:
Deployment : Manages two replicas of NGINX pods, ensuring high availability.
Service : Provides stable network access to NGINX pods within the Kubernetes cluster.
Deploying Apache Server
Next, I deployed Apache HTTP Server with similar Kubernetes resources:
apiVersion: apps/v1
kind: Deployment
metadata:
name: apache-deployment
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: apache
template:
metadata:
labels:
app: apache
spec:
containers:
- name: my-apache-site
image: httpd:2.4
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: apache-service
namespace: default
spec:
selector:
app: apache
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
Here:
Deployment : Ensures two replicas of Apache pods are running, facilitating load balancing and fault tolerance.
Service : Enables access to Apache pods within the Kubernetes cluster.
Configuring Ingress for Routing Traffic
To expose both NGINX and Apache services externally and manage traffic routing based on paths, I set up an Ingress resource:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-apps-ingress
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: your-domain.com
http:
paths:
- path: /nginx
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
- path: /apache
pathType: Prefix
backend:
service:
name: apache-service
port:
number: 80
In this configuration:
Ingress : Manages external access to services based on HTTP routing rules.
Annotations: Use annotation to ensure proper path rewriting for NGINX.
Rules: Define routing rules based on the host your-domain.com and paths
as you wish .
Update your local hosts entry /etc/hosts such that your-domain.com points to ip address of your load balancer
Conclusion
Today’s journey into Kubernetes deployments and Ingress configuration has equipped me with the skills to deploy and manage multiple web services effectively. Kubernetes’ flexibility in orchestrating containers and managing traffic routing through Ingress simplifies scalability and accessibility for applications.
With NGINX and Apache up and running in Kubernetes, I look forward to exploring more advanced features and optimizations in future projects.
Follow my blogs if you are also learning k8s let's support each other in this journey🤝