Kubernetes Ingress
To switch between namespaces:
kubectl get ns
kubens <namespace-name>
Ingress (Path Based Routing)
1. Create a Docker Image:
In your project directory, create a Dockerfile (as shown earlier) and organize your HTML content in separate folders (app1
and app2
). Your directory structure might look like this:
project-directory/
├── Dockerfile
├── app1/
│ └── index.html (content for /app1)
├── app2/
│ └── index.html (content for /app2)
Your Dockerfile should copy these folders into the Nginx default HTML directory (/usr/share/nginx/html
).
2. Build and Push Docker Image:
Build the Docker image and push it to a container registry of your choice (e.g., Docker Hub, Google Container Registry, or a private registry). Replace [YOUR_REGISTRY]
, [YOUR_IMAGE_NAME]
, and [TAG]
with appropriate values:
docker build -t [YOUR_REGISTRY]/[YOUR_IMAGE_NAME]:[TAG] .
docker push [YOUR_REGISTRY]/[YOUR_IMAGE_NAME]:[TAG]
3. Create Kubernetes Deployment and Service:
Create a Kubernetes Deployment and Service for your Nginx application. Here's an example YAML file (nginx-app-deployment-service.yaml
):
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
spec:
replicas: 3
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginx-app
image: [YOUR_REGISTRY]/[YOUR_IMAGE_NAME]:[TAG]
---
apiVersion: v1
kind: Service
metadata:
name: nginx-app-service
spec:
selector:
app: nginx-app
ports:
- protocol: TCP
port: 80
targetPort: 8
Apply this configuration to your cluster:
kubectl apply -f nginx-app-deployment-service.yaml
4. Create an Ingress Resource for Path-Based Routing:
Create an Ingress resource (nginx-app-ingress.yaml
) to configure path-based routing:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: path-based-ingress
spec:
rules:
- http:
paths:
- path: /app1
pathType: Prefix
backend:
service:
name: nginx-app-service
port:
number: 80
- path: /app2
pathType: Prefix
backend:
service:
name: nginx-app-service
port:
number: 80
Apply the Ingress configuration to your cluster:
kubectl apply -f nginx-app-ingress.yaml
5. DNS Configuration:
Ensure that the DNS records for your domains or subdomains point to the IP address or LoadBalancer of your Kubernetes cluster.
6. Access Your Application:
After applying the Ingress configuration and ensuring proper DNS setup, you should be able to access your application using the paths /app1
and /app2
based on the path-based routing rules defined in the Ingress.
For example:
http://your-domain/app1
should serve content from the/usr/share/nginx/html/app1
directory.http://your-domain/app2
should serve content from the/usr/share/nginx/html/app2
directory.