Nebul Docs

Creating a New Ingress Rule

Since setting up ingress rules is a common operation, provide detailed instructions on how to define and apply these rules to route external traffic to the appropriate services within your Kubernetes cluster. This should include examples and best practices. ​Kubernetes

Prerequisites

A running Kubernetes cluster kubectl installed and configured NGINX Ingress Controller installed If not installed, you can install it quickly with:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.10.0/deploy/static/provider/cloud/deploy.yaml 

Step-by-Step: Creating a New Ingress Rule

1. Create a Sample Application (Deployment + Service)

Let’s create a simple Hello World app that responds on port 80:

# hello-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: hashicorp/http-echo
        args:
        - "-text=Hello, Kubernetes!"
        ports:
        - containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  selector:
    app: hello-world
  ports:
    - protocol: TCP
      port: 80
      targetPort: 5678

Apply it:

kubectl apply -f hello-deployment.yaml

2. Create an Ingress Resource

Now let’s expose this service using an Ingress rule.

# hello-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: hello.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hello-world
            port:
              number: 80

Apply it:

kubectl apply -f hello-ingress.yaml

3. Test It

Add a line to your /etc/hosts (on your local machine):

127.0.0.1 hello.local 

Then use curl on your browser:

curl http://hello.local # Output: Hello, Kubernetes!

On this page